Always false float comparisons
Joe Duarte via Digitalmars-d
digitalmars-d at puremagic.com
Mon May 9 16:30:10 PDT 2016
On Monday, 9 May 2016 at 09:10:19 UTC, Walter Bright wrote:
> Don Clugston pointed out in his DConf 2016 talk that:
>
> float f = 1.30;
> assert(f == 1.30);
>
> will always be false since 1.30 is not representable as a
> float. However,
>
> float f = 1.30;
> assert(f == cast(float)1.30);
>
> will be true.
>
> So, should the compiler emit a warning for the former case?
I think it really depends on what the warning actually says. I
think people have different expectations for what that warning
would be.
When you say 1.30 is not representable as a float, when is the
"not representable" enforced? Because it looks like the
programmer just represented it in the assignment of the literal –
but that's not where the warning would be right? I mean I assume
so because people need nonrational literals all the time, and
this is the only way they can do it, which means it's a hole in
the type system right? There should be a decimal type to cover
all these cases, like some databases have.
Would the warning say that you can't compare 1.30 to a float
because 1.30 is not representable as a float? Or would it say
that f was rounded upon assignment and is no longer 1.30?
Short of a decimal type, I think it would be nice to have a
"float equality" operator that covered this whole class of cases,
where floats that started their lives as nonrational literals and
floats that have been rounded with loss of precision can be
treated as equal if they're within something like .0000001% of
each other (well a percentage that can actually be represented
as a float...) Basically equality that covers the known
mutational properties of fp arithmetic.
There's no way to do this right now without ranges right? I know
that ~ is for concat. I saw ~= is an operator. What does that do?
The Unicode ≈ would be nice for this.
I assume IEEE 754 or ISO 10967 don't cover this? I was just
reading the latter (zip here:
http://standards.iso.org/ittf/PubliclyAvailableStandards/c051317_ISO_IEC_10967-1_2012.zip)
More information about the Digitalmars-d
mailing list