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: Wed, 18 May 2022 06:57:34 -0700 Organization: A noiseless patient Spider Lines: 53 Message-ID: <8635h72bap.fsf@linuxsc.com> References: <87czgcqxgc.fsf@bsb.me.uk> <86o7zw2x55.fsf@linuxsc.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="949c0842145859edcf085e16a5bb4ce2"; logging-data="18180"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+zqye1j3KoZYT1tsoIK7JpX/0Y+IiUe14=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:MV2re7NApfnSCZZQJPEkOURoXYc= sha1:E61gmo489CIEqquxeNLp+TSQpas= Xref: csiph.com comp.lang.c++:84167 Manfred writes: > On 5/18/2022 6:40 AM, Juha Nieminen wrote: > >> Tim Rentsch wrote: >> >>> 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. >> >> AFAIK no C/C++ compiler will second-guess the programmer and assume that >> the literal was meant to be a long double literal. If you specify a >> literal of type double, the compiler will assume you meant it (and will >> just convert it to double without adding any precision to the resulting >> value.) >> >> With double vs. long double the loss in precision isn't extremely >> drastic (although in some calculations it could accumulate to significant >> proportions). However, people often make the mistake when they are using >> some third-party multiple-precision libraries that support floating point >> values of arbitrary size. If you are calculating with eg. 1024-bit >> floating point, make sure you initialize them properly (ie. do not >> initialize such a value with eg. the literal 0.1). > > I believe Tim was referring to the fact that the standard does not > mandate for long double to have more precision than double. That is one possibility, but I didn't mean just that. Here is the original context: long double value = 0.1; I'm not sure what C++ allows or doesn't allow for the value of the literal 0.1. For C, my understanding is that the current C standard allows the constant 0.1 to be represented in the format, and precision, of a long double even though its type is double. (The rules for floating constants in C has changed over time so I'm not sure if that allowance might be different for earlier C standards.) The next C standard apparently will be explicit on this point - in the n2731 draft of the C standard, section 6.4.4.2 paragraph 6 says this in part: The values of floating constants may be represented in greater range and precision than that required by the type (determined by the suffix); the types are not changed thereby.