#include <iostream>
#include <cmath>
usingnamespace 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<shortint>(std::floor(0.5 + x1)) << std::endl;
cout << x2 << ":: after addition of 0.5:: " << (x2 + 0.5) << " ::and after short int casting:: " <<static_cast<shortint>(std::floor(0.5 + x2)) << std::endl;
cout << x3 << ":: after addition of 0.5:: " << (x3 + 0.5) << " ::and after short int casting:: " <<static_cast<shortint>(std::floor(0.5 + x3)) << std::endl;
cout << x4 << ":: after addition of 0.5:: " << (x4 + 0.5) << " ::and after short int casting:: " <<static_cast<shortint>(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 shortint casting:: 2
2.5:: after addition of 0.5:: 3 ::and after shortint casting:: 2
2.51:: after addition of 0.5:: 3.01 ::and after shortint casting:: 3
3:: after addition of 0.5:: 3.5 ::and after shortint 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.
2.29999999999999982236 :: after addition of 0.5 :: 2.79999999999999982236
and after shortint casting:: 2
2.49999999000000006077 :: after addition of 0.5 :: 2.99999999000000006077
and after shortint casting:: 2
2.50999999999999978684 :: after addition of 0.5 :: 3.00999999999999978684
and after shortint casting:: 3
2.99999999989999999173 :: after addition of 0.5 :: 3.49999999989999999173
and after shortint casting:: 3