Fun with floating point
via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Feb 8 01:38:55 PST 2015
On Sunday, 8 February 2015 at 09:19:08 UTC, Kenny wrote:
>> For example, according to IEEE-754 specification if we work
>> with 32bit floating point numbers (floats in D) then the
>> following pseudo code prints 'Works'.
>>
>> F32 f = 16777216.0f;
>> F32 f2 = f + 0.1f;
>> if is_the_same_binary_presentation(f, f2)
>> Print("Works");
>>
>> As I understand D does not guarantee this. Please confirm if
>> it's correct.
>
> One clarification:
>
> In D the following should always print 'Works'
> float f = 16777216.0f;
> float f2 = f + 1.0f;
> if (f == f2)
> writeln("Works");
>
> I asked more about this case:
> float f = 16777216.0f;
> if (f == f + 1.0f)
> writeln("Works");
>
> Although all operands are 32bit FP the result is not guaranteed
> to be equal to the result for FP32 computations as specified by
> IEEE-754.
«Algorithms should be written to work based on the minimum
precision of the calculation. They should not degrade or fail if
the actual precision is greater.»
http://dlang.org/float.html
:-/
More information about the Digitalmars-d-learn
mailing list