literals

Fawzi Mohamed fawzi at gmx.ch
Sun Mar 28 15:17:39 PDT 2010


On 29-mar-10, at 00:04, Walter Bright wrote:

> Don wrote:
>> (1) Converting a floating point literal into a double literal is  
>> usually not lossless.
>> 0.5f, 0.5, and 0.5L are all exactly the same number, since they are  
>> exactly representable.
>> But 0.1 is not the same as 0.1L.
>
> It depends. The D compiler internally stores all floating point  
> constants, regardless of type, in full 80 bit precision. Constant  
> folding and CTFE are all carried out in 80 bit precision regardless  
> of type. The only time it is actually truncated to the shorter  
> formats is when writing it out to the object file.

ok good that is actually how I though, so that cast(T)x is indeed the  
way to write a generic float literal.
I thought that was the case, but seeing Don implying something  
different I got worried that my coding was based on wrong assumptions.

> The central idea is that more precision == better. If your floating  
> point algorithm breaks if precision increases, then the algorithm is  
> a bad one.

I fully agree.

> The only time I've seen code that relied on roundoff errors is in  
> test suites that specifically tested for it.




More information about the Digitalmars-d mailing list