Path: csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c++
Subject: Re: C++ (and some C) quiz questions
Date: Tue, 17 May 2022 04:53:26 -0700
Organization: A noiseless patient Spider
Lines: 38
Message-ID: <86o7zw2x55.fsf@linuxsc.com>
References: <87czgcqxgc.fsf@bsb.me.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="0161277cd888cf27dddddc87a0dfb7f9"; logging-data="22139"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19hyoKn8qMrJ9lOTuJkpgZTqwN//w4+mw0="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:XE3V+i3OjccTehUyOcCSfoa79nM= sha1:+5r9uh0N1GhRt1wAtERNvoF4T2U=
Xref: csiph.com comp.lang.c++:84146
Ben writes:
> Juha Nieminen writes:
>
>> Christian Gollwitzer wrote:
>>
>>>> Re #5, I guess that the problem with
>>>>
>>>> long double value = 0.1;
>>>>
>>>> ... is that when `long double` doesn't have the same representation as
>>>> `double`, one may get a less precise value than with
>>>>
>>>> long double value = 0.1L;
>>>>
>>>> Is that it?
>>>
>>> That's how I see it. 0.1 is a double constant and since 1/10 can't be
>>> represented exactly in binary (assuming binary floats), the value in
>>> "value" is not the closes approximation to 0.1 possible, which is 0.1L.
>>
>> It's easy to make such mistakes, and not just in variable initialization,
>> but also pretty much anywhere where a literal is used, like:
>>
>> x = y * 0.1; // If x and y are long double, precision is lost
>
> 7.4 Usual arithmetic conversions
>
> (1.2) ? If either operand is of type long double, the other shall be
> converted to long double.
But that doesn't change the problem of potential loss of
precision, because the representation (and hence the particular
value) of the constant 0.1 is chosen before that value is
converted to long double. The constant 0.1, being of type
double, doesn't have to have less precision than 0.1L, but
certainly it can.