Is (long long) x the same as (long long) gender (x) in C ++?

Suppose I have a double data type, a variable named "x". Does casting a double or long long data type need to produce the same result as casting to a long long floor (x).

+4
source share
1 answer

No, this is not the same.

Cast truncations (rounds to zero), the function is floorrounded down.

Demo: http://ideone.com/k8JuA9

#include <iostream>
#include <math.h>

int main()
{
    double x = -1.4;
    std::cout << "(long long)x        = " << ((long long)x) << "\n";
    std::cout << "(long long)floor(x) = " << ((long long)floor(x)) << "\n";
    return 0;
}
+6
source

Source: https://habr.com/ru/post/1543897/


All Articles