When casting a double to a long long causes GCC to overflow the output value. Imagine; #include <iostream> double foo() { return 9223372036854775807.0; } int main() { std::wcout << (long long)foo(); // Prints -9223372036854775808 }
Additionally, std::pow(double, int) and std::pow(double, double) seem to behave differently, presumably for the same reason; #include <iostream> #include <cmath> int main() { std::wcout << (long long)std::pow(2.0, 64); // -9223372036854775808 std::wcout << (long long)std::pow(2.0, 64.0); // 9223372036854775807 } Note that this behaviour is not the same if the return value of foo(), std::pow() (in both cases) are stored in variables first. Compiling with "g++ -Wall -ansi -std=gnu++0x -pedantic test.cpp", with GCC 4.4.1. Note that compiling with "g++ -Wall -ansi -std=gnu++0x -pedantic -O3 test.cpp" does NOT exhibit this behaviour; foo() and both std::pow() results in 9223372036854775807. Note that (unsigned long long)std::pow(2.0, 64) == 0, and (unsigned long long)std::pow(2.0, 64.0) == 18446744073709551615. -- Summary: Explicit casting of double to long long causes value to overflow Product: gcc Version: 4.4.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: ullner at gmail dot com http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43284