public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: eool <eoolsson@gmail.com>
To: gcc-help@gcc.gnu.org
Subject: Floating point behavior
Date: Thu, 04 Sep 2008 07:27:00 -0000	[thread overview]
Message-ID: <19304865.post@talk.nabble.com> (raw)


Hi all,

I was reading the C++ FAQ at http://www.parashift.com/c++-faq-lite/ when I
encountered an interesting curiosity in the "newbie questions" section. The
FAQ warns against directly comparing floating point numbers and, in section
29.18, gives an example of code (see below) that produces behavior that
seems unnatural to many (most?) programmers. (Below code copied from
http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.18)

 #include <cmath>
 
 void foo(double x, double y)
 {
   if (cos(x) != cos(y)) {
     std::cout << "Huh?!?\n";  ← you might end up here when x == y!!
   }
 }
 
 int main()
 {
   foo(1.0, 1.0);
   return 0;
 } 

Basically, the reason given in the FAQ is that the result of cos(x)
(intermediate, higher precision value) will be stored in a register and thus
truncated. Then the result of cos(y) is directly compared with the truncated
value located in a register, resulting in this strange (?) behavior. Now, my
question is, why isn't gcc able to tell that it is about to compare two
floating point numbers, one of which has been truncated, and then similarly
truncate the second value before the comparison? (In fact, since the return
type of cos() is double, shouldn't the intermediate value be truncated to
fit a double in any case?)

Second (this question might be even more naive than the previous one), if
you are never, ever, supposed to compare floating point numbers using ==; if
you are always supposed to check the difference of the numbers against an
epsilon value; then how come the compiler can't automatically do this? Is it
because analyzing the source code to track the accumulated floating point
errors (to appropriately adjust epsilon) might be prohibitively
difficult/slow? Or are there actually any situations where you'd want to
compare two floating point numbers directly with each other?

Thanks for taking the time to answer my "newbie questions" :)

/Erik
-- 
View this message in context: http://www.nabble.com/Floating-point-behavior-tp19304865p19304865.html
Sent from the gcc - Help mailing list archive at Nabble.com.

             reply	other threads:[~2008-09-04  7:27 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-09-04  7:27 eool [this message]
2008-09-04  8:58 ` Andrew Haley
2008-09-04  9:01 ` Brian Dessent
2008-09-07 10:01   ` Vincent Lefevre
2008-09-07 10:27     ` Martin Krischik
2008-09-07 12:53       ` Vincent Lefevre

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=19304865.post@talk.nabble.com \
    --to=eoolsson@gmail.com \
    --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).