std::floor() behavior

Hello forum,

I tried some snippet with the function std::floor over the double value type. Here goes the snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cmath>

using namespace std;

int main()
{

  double x1 = 2.3;
  double x2 = 2.49999999;
  double x3 = 2.51;
  double x4 = 2.9999999999;

  cout << x1 << ":: after addition of 0.5:: " << (x1 + 0.5) << " ::and after short int casting:: "  <<static_cast<short int>(std::floor(0.5 + x1)) << std::endl;
  cout << x2 << ":: after addition of 0.5:: " << (x2 + 0.5) << " ::and after short int casting:: "  <<static_cast<short int>(std::floor(0.5 + x2)) << std::endl;  
  cout << x3 << ":: after addition of 0.5:: " << (x3 + 0.5) << " ::and after short int casting:: "  <<static_cast<short int>(std::floor(0.5 + x3)) << std::endl;
  cout << x4 << ":: after addition of 0.5:: " << (x4 + 0.5) << " ::and after short int casting:: "  <<static_cast<short int>(std::floor(0.5 + x4)) << std::endl;  
  
  return 0;
}



And the output I got is as follows:

1
2
3
4
5
2.3:: after addition of 0.5:: 2.8 ::and after short int casting:: 2
2.5:: after addition of 0.5:: 3 ::and after short int casting:: 2
2.51:: after addition of 0.5:: 3.01 ::and after short int casting:: 3
3:: after addition of 0.5:: 3.5 ::and after short int casting:: 3



Check the second value, after the addition of 0.5 , the value is already is 3, then when I cast it to floor, it is decremented to 2. Should it be like it ?
I expected to remain at 2 even after the floor() call.

What do you think?


Thanks
Last edited on
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
#include <iostream>
#include <cmath>
#include <iomanip>

int main()
{
    const double a = 2.49999999 ;
    std::cout << std::fixed << std::setprecision(64)
              << "2.49999999\n"
              << "nearest representable double: " << a << '\n'
              << "\n------------------\n\n" ;

    const double b = 0.5 ;
    std::cout << "0.5\n"
              << "nearest representable double: " << b << '\n'
              << "\n------------------\n\n" ;

    const double c = a + b ;
    std::cout << "2.49999999 + 0.5\n"
              << "nearest representable double: " << c << '\n'
              << "                       floor: " << std::floor(c) << '\n'
              << "rounded to default precision: " << std::defaultfloat << std::setprecision(6) << c << '\n'
              << "         narrowed to integer: " << int(c) << '\n'
              << "\n------------------\n\n" ;
}

Typical output:
2.49999999
nearest representable double: 2.4999999900000000607747097092214971780776977539062500000000000000

------------------

0.5
nearest representable double: 0.5000000000000000000000000000000000000000000000000000000000000000

------------------

2.49999999 + 0.5
nearest representable double: 2.9999999900000000607747097092214971780776977539062500000000000000
                       floor: 2.0000000000000000000000000000000000000000000000000000000000000000
rounded to default precision: 3
         narrowed to integer: 2

------------------

http://coliru.stacked-crooked.com/a/42ea9743438c7ace
Check the second value, after the addition of 0.5 , the value is already is 3


No it is 2.499999 + .5 = 2.9999999, I suggest you add some decimal points to your printout.

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
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

void printout(double x1);

int main()
{

    double x1 = 2.3;
    double x2 = 2.49999999;
    double x3 = 2.51;
    double x4 = 2.9999999999;

    printout(x1);
    printout(x2);
    printout(x3);
    printout(x4);

    return 0;
}

void printout(double x1)
{
    cout << fixed << setprecision(20);
    cout << setw(21) << x1 << " :: after addition of 0.5 :: " << setw(21) << (x1 + 0.5)
         << "\n           and after short int casting:: "
         << setw(3) << static_cast<short int>(std::floor(0.5 + x1)) << endl << endl;

}


1
2
3
4
5
6
7
8
9
10
11
2.29999999999999982236 :: after addition of 0.5 :: 2.79999999999999982236
           and after short int casting::   2

2.49999999000000006077 :: after addition of 0.5 :: 2.99999999000000006077
           and after short int casting::   2

2.50999999999999978684 :: after addition of 0.5 :: 3.00999999999999978684
           and after short int casting::   3

2.99999999989999999173 :: after addition of 0.5 :: 3.49999999989999999173
           and after short int casting::   3
Topic archived. No new replies allowed.