[Bug c++/43284] New: Explicit casting of double to long long causes value to overflow

2010-03-07 Thread ullner at gmail dot com
When casting a double to a long long causes GCC to overflow the output value.
Imagine; 
#include 
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 
#include 
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



[Bug c++/43284] Explicit casting of double to long long causes value to overflow

2010-03-07 Thread ullner at gmail dot com


--- Comment #2 from ullner at gmail dot com  2010-03-07 22:10 ---
Hm? How does calling std::pow with different types behave differently? The
value can be stored fine if one does "double dValue = std::pow(2.0, 64);long
long llValue = dValue;" // OK


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43284



[Bug c++/43284] Explicit casting of double to long long causes value to overflow

2010-03-08 Thread ullner at gmail dot com


--- Comment #4 from ullner at gmail dot com  2010-03-08 17:59 ---
That still doesn't make sense.
1. Why does enabling -O3 (O1 and O2 does the same) remove this problem?
2. Why does storing the value in an intermediate variable make any change in
what the result is?

Consider without O3:
(long long)std::pow(2.0, 64); // Overflow.
(long long)std::pow(2.0, 64.0); // OK

double d1 = std::pow(2.0, 64);
(long long)d1; // Overflow
double d2 = std::pow(2.0, 64.0);
(long long)d2; // Overflow

long long l1 = d1; // l1 Overflows
long long l2 = d2; // l2 Overflows

Enabling -O3 causes every one of those that overflows to be OK. 


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43284