Always false float comparisons
Ola Fosheim Grøstad via Digitalmars-d
digitalmars-d at puremagic.com
Mon May 16 01:25:13 PDT 2016
On Monday, 16 May 2016 at 07:54:32 UTC, Iain Buclaw wrote:
> On 16 May 2016 at 09:22, Ola Fosheim Grøstad via Digitalmars-d
> <digitalmars-d at puremagic.com> wrote:
>> On Monday, 16 May 2016 at 06:34:04 UTC, Iain Buclaw wrote:
>>
>> No, promoting to double is ok.
>
> Your own example:
>
> const float value = 1.30;
> float copy = value;
> assert(value*0.5 == copy*0.5);
>
> Versus how you'd expect it to work.
>
> const float value = 1.30;
> float copy = value;
> assert(cast(float)(value*0.5) == cast(float)(copy*0.5)); //
> Compare
> as float, not double.
Promoting to double is sound. That is not the issue. Just replace
the assert with a function like "check(real x, real y)". The
programmer shouldn't have to look it up. Using "check(real x,
real y)" should _not_ give a different result than using
"check(float x, float y)". If you have implemented the former,
you shouldn't have to implement the latter to get reasonable
results.
Binding to "const float" or "float" should not change the outcome.
The problem is not the promotion, the problem is that you don't
get the coercion to 32 bit floats where you requested it. This
violates the principle of "least surprise".
More information about the Digitalmars-d
mailing list