language: C++ 4.7.2 (gcc-4.7.2)
date: 959 days 15 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
    #include <cmath>
    #include <limits>
#include <cstdio>
 
    static double dRound(double d) {
       double inf = copysign(std::numeric_limits<double>::infinity(), d);
       double theNumberAfter = nextafter(d, inf);
       double epsilon = theNumberAfter - d;
 
       int factor = 100;
       d *= factor;
       epsilon *= factor/2;
       d += epsilon;
 
       double returnVal;
       printf("%.20g, ", modf(d, &returnVal));
       printf("%.20g\n", returnVal);
       return returnVal / factor;
    }
 
int main () {
    dRound(0.3);
    dRound(1.3);
    dRound(2.3);
    dRound(3.3);
    dRound(4.3);
    dRound(102.3);
    dRound(5);
    dRound(4.999999999);
    dRound(3.1415926535);
    dRound(2.718281828459045);
 
    dRound(-0.3);
    dRound(-1.3);
    dRound(-2.3);
    dRound(-3.3);
    dRound(-4.3);
    dRound(-102.3);
    dRound(-5);
    dRound(-4.999999999);
    dRound(-3.1415926535);
    dRound(-2.718281828459045);
    return 0;
}