public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/43284] New: Explicit casting of double to long long causes value to overflow
@ 2010-03-07 21:41 ullner at gmail dot com
2010-03-07 22:07 ` [Bug c++/43284] " schwab at linux-m68k dot org
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: ullner at gmail dot com @ 2010-03-07 21:41 UTC (permalink / raw)
To: gcc-bugs
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c++/43284] Explicit casting of double to long long causes value to overflow
2010-03-07 21:41 [Bug c++/43284] New: Explicit casting of double to long long causes value to overflow ullner at gmail dot com
@ 2010-03-07 22:07 ` schwab at linux-m68k dot org
2010-03-07 22:10 ` ullner at gmail dot com
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: schwab at linux-m68k dot org @ 2010-03-07 22:07 UTC (permalink / raw)
To: gcc-bugs
------- Comment #1 from schwab at linux-m68k dot org 2010-03-07 22:07 -------
9223372036854775807 has more significant bits than what fits in a double, it is
rounded to 9223372036854775808.0, which then overflows when converted to long
long.
--
schwab at linux-m68k dot org changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution| |INVALID
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43284
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c++/43284] Explicit casting of double to long long causes value to overflow
2010-03-07 21:41 [Bug c++/43284] New: Explicit casting of double to long long causes value to overflow ullner at gmail dot com
2010-03-07 22:07 ` [Bug c++/43284] " schwab at linux-m68k dot org
@ 2010-03-07 22:10 ` ullner at gmail dot com
2010-03-08 10:40 ` rguenth at gcc dot gnu dot org
2010-03-08 18:00 ` ullner at gmail dot com
3 siblings, 0 replies; 5+ messages in thread
From: ullner at gmail dot com @ 2010-03-07 22:10 UTC (permalink / raw)
To: gcc-bugs
------- 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
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c++/43284] Explicit casting of double to long long causes value to overflow
2010-03-07 21:41 [Bug c++/43284] New: Explicit casting of double to long long causes value to overflow ullner at gmail dot com
2010-03-07 22:07 ` [Bug c++/43284] " schwab at linux-m68k dot org
2010-03-07 22:10 ` ullner at gmail dot com
@ 2010-03-08 10:40 ` rguenth at gcc dot gnu dot org
2010-03-08 18:00 ` ullner at gmail dot com
3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2010-03-08 10:40 UTC (permalink / raw)
To: gcc-bugs
------- Comment #3 from rguenth at gcc dot gnu dot org 2010-03-08 10:39 -------
(In reply to comment #2)
> 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
Integer exponents have an overload that uses an implementation that does
multiple roundings.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43284
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c++/43284] Explicit casting of double to long long causes value to overflow
2010-03-07 21:41 [Bug c++/43284] New: Explicit casting of double to long long causes value to overflow ullner at gmail dot com
` (2 preceding siblings ...)
2010-03-08 10:40 ` rguenth at gcc dot gnu dot org
@ 2010-03-08 18:00 ` ullner at gmail dot com
3 siblings, 0 replies; 5+ messages in thread
From: ullner at gmail dot com @ 2010-03-08 18:00 UTC (permalink / raw)
To: gcc-bugs
------- 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
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2010-03-08 18:00 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-03-07 21:41 [Bug c++/43284] New: Explicit casting of double to long long causes value to overflow ullner at gmail dot com
2010-03-07 22:07 ` [Bug c++/43284] " schwab at linux-m68k dot org
2010-03-07 22:10 ` ullner at gmail dot com
2010-03-08 10:40 ` rguenth at gcc dot gnu dot org
2010-03-08 18:00 ` ullner at gmail dot com
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).