Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++.moderated > #7332
| Message-ID | <55F32514.90506@verizon.net> (permalink) |
|---|---|
| Newsgroups | comp.lang.c++.moderated |
| From | James Kuyper <jameskuyper@verizon.net> |
| Subject | Re: References to const |
| Organization | Self |
| References | <vb9ux.54392$UI6.3334@fx45.am4> <4d7fe207-555b-40a4-83b8-ddae02869d2a@googlegroups.com> <b5649dc6-a487-4f53-8ebb-f6195508a0c4@googlegroups.com> <6c750fdd-ff61-4ecd-b9fb-f02a2c5da8fc@googlegroups.com> |
| Date | 2015-09-12 13:57 -0600 |
On 09/11/2015 02:32 PM, Öö Tiib wrote:
>
> On Friday, 11 September 2015 15:10:17 UTC+3, Martin Bonner wrote:
>> On Friday, 4 September 2015 14:00:16 UTC+1, Jack Adrian Zappa wrote:
>>> (*) Please note that a double is a floating point number and as such
>>> you should never do a equal comparison due to rounding error of
>>> equivalent representations. ALWAYS compare a floating point
>>> against an error range.
>>
>> I think that "ALWAYS" is too strong. In your example (involving 2 *
3.14)
>> it
>> is correct, but I believe the following code is reasonable:
>> double d = 2.0;
>> double d2 = d * d;
>> assert(d2 == 4.0);
>
> He didn't claim that floating point arithmetic always results
> with slightly off results. It just happens frequently enough
> to make 'operator==' unreliable in practice. People who want
> to write reliable code must avoid unreliable constructs.
>
> Your "reasonable" is also too strong. That code will be
> optimized to nothing on most compilers so it feels difficult
> to reason why to write it. ;)
Well, the assert was just a demonstration. In real-world code, the
assert() would be replaced by an if(), and might be widely separated
from the line which calculates d2.
The point is that real-world floating point implementations can produce
reliable results, so long as the final result and all intermediate steps
involve values that have the form of sufficiently small integers
multiplied by small positive or negative powers of two. It's not
entirely unreasonable to count on such things, even though the standard
doesn't provide any support for such assumptions.
>> (Note: The standard doesn't guarantee the assert won't fire, but it
is
>> very, very, unlikely to do so.)
>
> AFAIK even 'assert(d2 == 3.0);' is not required to fire on
> conforming C++ implementation. Floating point is quite totally
> implementation-defined.
assert(DBL_MAX - DBL_MIN > DBL_MIN - DBL_MAX) is not required to
trigger, either. I'd be interested in knowing if there's any real-world
implementation where it doesn't.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to comp.lang.c++.moderated | Previous | Next — Previous in thread | Find similar
References to const Robert <rob1@invalid.net> - 2015-07-29 16:30 -0600
Re: References to const legalize+jeeves@mail.xmission.com (Richard) - 2015-07-29 20:41 -0600
Re: References to const legalize+jeeves@mail.xmission.com (Richard) - 2015-07-31 07:12 -0600
Re: References to const "Alf P. Steinbach" <alf.p.steinbach+usenet@googlemail.com> - 2015-08-01 07:32 -0600
Re: References to const Martin Bonner <martinfrompi@yahoo.co.uk> - 2015-07-30 11:01 -0600
Re: References to const Jack Adrian Zappa <adrianh.bsc@googlemail.com> - 2015-09-04 07:53 -0600
Re: References to const Martin Bonner <martinfrompi@yahoo.co.uk> - 2015-09-11 07:01 -0600
Re: References to const Öö Tiib <ootiib@hot.ee> - 2015-09-11 12:32 -0600
Re: References to const James Kuyper <jameskuyper@verizon.net> - 2015-09-12 13:57 -0600
csiph-web