C++Guns – RoboBlog blogging the bot

27.09.2018

C++ Guns: Are floating point numbers sortable?

Filed under: Allgemein — Tags: — Thomas @ 09:09

Sind Gleitkommazahlen sortierbar? Wegen NaN gibt es keine Ordnung und das Sortieren mit NaN im Datensatz schlägt fehlt.
Aber man kann sich schnell selbst eine Ordnung bauen:

#include <vector>
#include <algorithm>
#include <iostream>
#include <limits>
#include <cmath>

using namespace std;
using nl = numeric_limits<double>;

int main() {
  cout << boolalpha;

  vector<double> v { 1.0, 2.0, 0.0, 1.0/3.0, -0.0,
                    nl::quiet_NaN(), nl::infinity(), nl::epsilon(), nl::denorm_min(),
                    nl::max(), nl::min(), nl::lowest()
  };

  sort(v.begin(), v.end());

  for(const auto x : v) {
    cout << x << " ";
  }
  cout << "\nis sorted " << is_sorted(v.begin(), v.end()) << " << offensichtlich falsch\n";

  auto comp = [](const auto lhs, const auto rhs) {
    if(isnan(lhs)) return false;
    if(isnan(rhs)) return true;
    return lhs<rhs;
  };

  sort(v.begin(), v.end(), comp);

  for(const auto x : v) {
    cout << x << " ";
  }
  cout << "\nis sorted " << is_sorted(v.begin(), v.end()) << "\n";
}
-1.79769e+308 0 -0 0.333333 1 2 nan 4.94066e-324 2.22507e-308 2.22045e-16 1.79769e+308 inf 
is sorted true << offensichtlich falsch
-1.79769e+308 0 -0 4.94066e-324 2.22507e-308 2.22045e-16 0.333333 1 2 1.79769e+308 inf nan 
is sorted true << richtig

13.09.2018

FORTRAN: GDB conditional watchpoint

Filed under: Allgemein — Tags: — Thomas @ 13:09

To set a conditional watchpoint on local variable i.
Example Code:

subroutine func
  integer :: i

  do i=1, 100
    write(*,*) i
  end do
end subroutine

program test
  implicit none

  call func()
end program

Compile with -ggdb
Set a breakpoint on subroutine "func". After the debugger stop on this point, the local variable "i" is in scope so one can set a watchpoint.

$ gdb ./example
(gdb) break func
Breakpoint 1 at 0x8bb: file gdb.F90, line 4.

ODER
(gdb) break example.F90:4

(gdb) run
Starting program: /home/kater/example
Breakpoint 1, func () at example.F90:4
4 do i=1, 100
(gdb) watch i if i.gt.10
Hardware watchpoint 2: i
(gdb) c
continuing.
1
2
3
4
5
6
7
8
9
10

Hardware watchpoint 2: i

Old value = 10
New value = 11
0x0000555555554948 in func () at gdb.F90:4
4 do i=1, 100

Powered by WordPress