public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/29597]  New: log expression returns different results when casting
@ 2006-10-25 21:58 brad_atcheson at yahoo dot ca
  2006-10-25 22:05 ` [Bug c++/29597] " pinskia at gcc dot gnu dot org
  2006-10-26  6:39 ` bangerth at dealii dot org
  0 siblings, 2 replies; 3+ messages in thread
From: brad_atcheson at yahoo dot ca @ 2006-10-25 21:58 UTC (permalink / raw)
  To: gcc-bugs

The following code should return the base2 logarithm of 64 (ie 6)

double d = log(64.0) / log(2.0);
int i = (int)d;
cout << i;

And indeed it does output "6". However, if you attempt to cast it on the same
line, then it returns 5.

int i2 = (int)( log(64.0) / log(2.0) );
int i3 = static_cast<int>( log(64.0) / log(2.0) );
cout << i2 << i3;

This outputs "55" instead of the expected "66". Visual Studio 2003 outputs
"66".

gcc 4.1.0, Suse Linux 10.1.


-- 
           Summary: log expression returns different results when casting
           Product: gcc
           Version: 4.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: brad_atcheson at yahoo dot ca


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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c++/29597] log expression returns different results when casting
  2006-10-25 21:58 [Bug c++/29597] New: log expression returns different results when casting brad_atcheson at yahoo dot ca
@ 2006-10-25 22:05 ` pinskia at gcc dot gnu dot org
  2006-10-26  6:39 ` bangerth at dealii dot org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2006-10-25 22:05 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2006-10-25 22:05 -------
This comes from precission of the floating point.

*** This bug has been marked as a duplicate of 323 ***


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |DUPLICATE


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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c++/29597] log expression returns different results when casting
  2006-10-25 21:58 [Bug c++/29597] New: log expression returns different results when casting brad_atcheson at yahoo dot ca
  2006-10-25 22:05 ` [Bug c++/29597] " pinskia at gcc dot gnu dot org
@ 2006-10-26  6:39 ` bangerth at dealii dot org
  1 sibling, 0 replies; 3+ messages in thread
From: bangerth at dealii dot org @ 2006-10-26  6:39 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from bangerth at dealii dot org  2006-10-26 06:39 -------
To be more concrete: (int)(expression) rounds down. So if your quotient
of logarithms happens to be computed to 5.999999999999999, then you
will get a result of 5 after casting to int. You should round results,
not just cast them.

W.


-- 


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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2006-10-26  6:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-10-25 21:58 [Bug c++/29597] New: log expression returns different results when casting brad_atcheson at yahoo dot ca
2006-10-25 22:05 ` [Bug c++/29597] " pinskia at gcc dot gnu dot org
2006-10-26  6:39 ` bangerth at dealii dot org

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).