Differences in results when using the same function in CTFE and Runtime
Timon Gehr
timon.gehr at gmx.ch
Tue Aug 13 08:33:51 UTC 2024
On 8/13/24 09:56, Abdulhaq wrote:
>
> It's worth mentioning that in the wider field, it is generally held that
> when working with floating point operations we don't expect to see the
> same duplicate results every time.
This is not true at the level of hardware, where results are actually
reproducible on the same processor architecture (and often even across
architectures) with the same sequence of instructions. Treating floats
as "fuzzy" is perhaps common, but it is not correct. Rounding is a pure
function like any other.
> It is a mistake to compare for equality, you should be checking for "almost equals".
That is a different point.
>
> For your particular application it might feel like a "bug" to see different results, but in fact this is expected behaviour and generally not held to be a fault.
> ...
It's annoying anyway, and we should hold programming languages to a
higher standard than that.
> One little piece of anecdata, when working with python I saw differing results in the 17th d.p. when running the exact same calculation in the same python session. The reason turned out to be which registers were being used each time, which could vary. This is not considered to be a bug.
Well, I think the x87 is a bad design as a compilation target. If you
are targeting that with a compiler whose developers by default do not
care about reproducibility, expect weird behavior. These instructions
are now deprecated, so the calculation should change a bit.
More information about the Digitalmars-d
mailing list