floating point conversion

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 1 22:30:42 PDT 2014


On Sun, 01 Jun 2014 14:42:34 +0000
Famous via Digitalmars-d-learn <digitalmars-d-learn at puremagic.com>
wrote:

> On Sunday, 1 June 2014 at 12:45:26 UTC, bearophile wrote:
> > It's a bad question.
>
> Actually, Martin's question is a good one.
>
> Initializing a variable of type float via a literal or as
> conversion from string should be the same, exacly, always.
> Casting a float to double should be deterministic as well.

Not necessarily, particularly because any floating point operations done at
compile time are generally done at much higher precision than those done at
runtime. So, it's pretty trivial for very similar floating point operations to
end up with slightly different results. In general, expecting any kind of
exactness from floating point values is asking for trouble. Sure, they follow
the rules that they have consistently, but there are so many numbers that
aren't actually representable by a floating point value, the precisions vary
just enough, and slightly different code paths can result in slightly
different results that depending on floating point operations resulting in
any kind of specific values except under very controlled circumstances just
isn't going to work.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list