Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #13653

Re: comparing floating point numbers

From "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>
Newsgroups comp.lang.c++
Subject Re: comparing floating point numbers
Date 2012-01-29 20:40 +0100
Organization A noiseless patient Spider
Message-ID <jg47b2$de8$1@dont-email.me> (permalink)
References <jg44ad$fti$1@news.icm.edu.pl> <jg4540$n0$1@speranza.aioe.org> <jg46f1$852$1@dont-email.me>

Show all headers | View raw


On 29.01.2012 20:25, MikeWhy wrote:
>
> "BCFD36" <bcfd36@cruzio.com> wrote in message
> news:jg4540$n0$1@speranza.aioe.org...
>> On 1/29/12 10:48 AM, Adam Majewski wrote:
>>> Hi,
>>>
>>> I have seen code that compers like that :
>>>
>>> if (b == 0.0 && (a == 0.25 || a == -0.75))
>>>
>>> so I have checked it :
>>>
>>> #include <iostream>
>>>
>>> using namespace std;
>>>
>>> int main() {
>>> cout << boolalpha
>>> << "The true expression 0.25 == 0.25000000000000001 yields : "
>>> << (0.25 == 0.25000000000000001) << endl
>>> << "The false expression 0.25 == 0.2500000000000001 yields: "
>>> << (0.25 == 0.2500000000000001) << endl;
>>> }
>>>
>>>
>>> Is it operator overloadin ? Is it good code ?
>>>
>>> TIA
>>>
>>> Adam
>>
>>
>> So what was the output from the code? I would think you might get
>> different answers with different compilers.
>
> The representation of a double precision floating point is standardized.
> It has 53 bits of mantissa, roughly 16 decimal digits of precision.

Sorry, no. While there are standards for floating point representation, 
and in particular the one you're referring (namely IEEE 754), how 
floating point values are represented with a given C++ compiler depends 
formally on the compiler, and in practice on the API for the OS that the 
compiler targets. std::numeric_limits<double>::iec something something 
tells you whether IEEE *representation* is used. It was meant to also 
tell you whether the compiler adheres to that standard. Unfortunately, 
in practice it only tells you about the representation.

Cheers & hth.,

- Alf

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

comparing floating point numbers Adam Majewski <adammaj1@o2.pl> - 2012-01-29 19:48 +0100
  Re: comparing floating point numbers BCFD36 <bcfd36@cruzio.com> - 2012-01-29 11:02 -0800
    Re: comparing floating point numbers "MikeWhy" <boat042-nospam@yahoo.com> - 2012-01-29 13:25 -0600
      Re: comparing floating point numbers "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-01-29 20:40 +0100
    Re: comparing floating point numbers Adam Majewski <adammaj1@o2.pl> - 2012-01-30 18:03 +0100
  Re: comparing floating point numbers Marcel Müller <news.5.maazl@spamgourmet.com> - 2012-01-29 21:34 +0100
    Re: comparing floating point numbers BCFD36 <bcfd36@cruzio.com> - 2012-01-29 14:55 -0800
      Re: comparing floating point numbers Miles Bader <miles@gnu.org> - 2012-01-30 11:48 +0900
        Re: comparing floating point numbers Juha Nieminen <nospam@thanks.invalid> - 2012-01-30 14:53 +0000
    Re: comparing floating point numbers scott@slp53.sl.home (Scott Lurndal) - 2012-01-30 17:22 +0000
      Re: comparing floating point numbers Robert Wessel <robertwessel2@yahoo.com> - 2012-01-30 13:55 -0600
      Re: comparing floating point numbers glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-02-13 01:56 +0000
  Re: comparing floating point numbers LR <lruss@superlink.net> - 2012-01-29 18:30 -0500
    Re: comparing floating point numbers Thomas Boell <tboell@domain.invalid> - 2012-01-30 16:29 +0100
  Re: comparing floating point numbers Christian Gollwitzer <auriocus@gmx.de> - 2012-01-30 08:07 +0100
    Re: comparing floating point numbers glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-02-13 01:59 +0000
  Re: comparing floating point numbers Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-01-30 06:26 -0800

csiph-web