C++Guns – RoboBlog

13.06.2018

C++ Guns: nächste darstellbare Gleitkommazahl : std::nextafter

Filed under: Allgemein — Tags: — Thomas @ 11:06

std::nextafter(Arithmetic from, Arithmetic to)

Returns the next representable value of from in the direction of to.

Die nächste Zahl nach 1.0f ist dann 1.0000001192092895508. Hab das mal für eine Reihe von Zahlen mir ausgeben lassen. Und nicht nur die nächste Zahl, sondern bis zu 1000 nächst Zahlen.

nextafter

Hier das Programm für C++:

double nextafter2(double x, int anzahl) {
    for(int i=0; i < anzahl; i++) {
        x = std::nextafter(x, x+x);
    }
    return x;
}

void blah(int i) {
    std::cout << i << "     ";
    std::cout << nextafter2(0.001, i)- 0.001 << " ";
    std::cout << nextafter2(0.01, i)  - 0.01 << " ";
    std::cout << nextafter2(0.1, i)    - 0.1 << " ";
    std::cout << nextafter2(1, i)        - 1 << " ";
    std::cout << nextafter2(10, i)      - 10 << " ";
    std::cout << nextafter2(100, i)    - 100 << " ";
    std::cout << nextafter2(1000, i)  - 1000 << "\n";
}

    std::cout << "anzahl   0.001       0.01       0.1       1.0       10      100       1000\n";
    blah(1);
    blah(10);
    blah(100);
    blah(1000);

Und hier das Programm für gnuplot

set title "std::nextafter"
set format y "%E"
set key left
set key autotitle columnhead
set ylabel "relativ next"
set xlabel "ith next number"
set logscale xy
plot for [col=2:8] "nextnumber" using 1:col  w linespoint

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress