From: Colin Law <c.law@elec.gla.ac.uk>
To: gcc-help@gcc.gnu.org
Subject: Casting and Optimization Flags
Date: Wed, 11 Dec 2002 04:31:00 -0000 [thread overview]
Message-ID: <3DF72E47.2080806@elec.gla.ac.uk> (raw)
Hello,
I am having trouble with a section of code that runs fine when compiled
with no optimization flags, however when I add optimization,flags, I get
different results.
In the middle of a for loop, and a fairly heavily recursive procedure i
has this;
double d = (std::log10(static_cast<double>(width))) /
(std::log10(static_cast<double>(2))) ;
unsigned int level = static_cast<unsigned int>(d) ;
Which will (should) always return me an integer value for my values of
width (always 2^n values). When i sprinf both d and level i can see that
the value is cast fine with no optimization, however when i compile with
-O2 or -O1, after the cast the int doesn't always match the double,
casting double 3 to an unsigned int =2 !!!
It appears that my double value is so close to an int that it is
regarded as a whole number,however during the cast the decimal values
are simpley chopped so 2.9999..... cast to unsinged int is 2. sprinf
(%.20f) tells me the value of d is 3.0000... not 2.9999...
I am unable to reprodude this problem in a trivial case which suggests
it is something i am missing in the main code, however, as i am
assigning to new variables, i cannot see where this could possible be.
If I use long double instead of double during the calculation. then
cast, I appear to get the correct results even with optimization!
Any suggestions as to what is causing this ??
I am using g++-3.2 and am compiling the above as part of a shared
library with -fPIC -Wall -O2 if thats any help.
Regards
~Colin.
next reply other threads:[~2002-12-11 12:31 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-12-11 4:31 Colin Law [this message]
2002-12-11 5:24 ` John Love-Jensen
2002-12-11 4:43 Rob Shearer
2002-12-11 5:47 ` Colin Law
2002-12-11 11:14 ` Joseph D. Wagner
2002-12-11 9:06 Hillel (Sabba) Markowitz
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=3DF72E47.2080806@elec.gla.ac.uk \
--to=c.law@elec.gla.ac.uk \
--cc=gcc-help@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).