I have a question about std :: round with a signature:double round (double x);
double round (double x);
Let's say I have this code:
int i = std::round(0.9);
In this case, I std::roundshould return 1.00000000000, but this is inconveniently close to 0.9999999999999, and I am worried that the floating point errors end in rounding.
std::round
1.00000000000
0.9999999999999
I hope so i == 1, but is it guaranteed?
i == 1
The function std::roundreturns a floating point value, "rounding half the cases from zero." As with any implicit conversion doubleto int, the compiler will generate a warning:
double
int
conversion from 'double' to 'int', possible data loss
std:: lround std:: lrint, .
. .
: std::round(0.9) 1.0 i == 1
std::round(0.9)
1.0
, double.
, IEEE754 double 52- 2 ! , , no-op. , , , std::round(4503599627370496.5) 4503599627370496, , 4503599627370496.5 double .
std::round(4503599627370496.5)
4503599627370496
4503599627370496.5
, std::round , - , a.5 ( ) , . , 0.5 , , , -.
a.5
0.5
++ C. N1570 (~ C11) round :
round
round , , .
, , , lrint, , .
lrint
cppreference:
prvalue . , .. . , undefined ( type is unsigned, ). - bool, (. ).
0.5, int ()
if(x < 0) { //number is negative return (int) x - 0.5; } else { return (int) x + 0.5;//number is positive }
1 1.49999999999, int, 1
Source: https://habr.com/ru/post/1688852/More articles:Filtering nested lists using lambda - javaThe command to execute the script later is linuxDetecting user change time after Force Stop application - javaThe stylesheet does not load because the MIME type is text / html - javascriptCreating a multidimensional associative array in php mysql - phpWhy does ASP.NET terminate asynchronous requests? - c #Updating a nested list using a lambda expression: Java 8 - javaIs it bad practice to maintain two copies of the same data if they are in different data structures? - c ++Why does Swift String.Index keep the index value 4 times the real? - stringCloud Functions for Firebase HTTP Timeout - javascriptAll Articles