Accuracy of floating point calculations

Daniel Kozak kozzi11 at gmail.com
Tue Oct 29 16:20:21 UTC 2019


On Tue, Oct 29, 2019 at 5:09 PM Daniel Kozak <kozzi11 at gmail.com> wrote:
>
> On Tue, Oct 29, 2019 at 4:45 PM Twilight via Digitalmars-d-learn
> <digitalmars-d-learn at puremagic.com> wrote:
> >
> > D calculation:
> >mport std.stdio;
import std.math : pow;
import core.stdc.math;

void main()
{
     writefln("%12.3F",log(1-0.9999)/log(1-(1-0.6)^^20));
}
> >    writefln("%12.2F",log(1-0.9999)/log(1-(1-0.6)^^20));
> >
> > 837675572.38
> >
> > C++ calculation:
> >
> >    cout<<setprecision(12)<< (log(1-0.9999)/log(1-pow(1-0.6,20)))
> > <<'\n';
> >
> > 837675573.587
> >
> > As a second data point, changing 0.9999 to 0.75 yields
> > 126082736.96 (Dlang) vs 126082737.142 (C++).
> >
> > The discrepancy stood out as I was ultimately taking the ceil of
> > the results and noticed an off by one anomaly. Testing with
> > octave, www.desmos.com/scientific, and libreoffice(calc) gave
> > results consistent with the C++ result. Is the dlang calculation
> > within the error bound of what double precision should yield?
>
> If you use gdc or ldc you will get same results as c++, or you can use
> C log directly:
>
> import std.stdio;
> import std.math : pow;
> import core.stdc.math;
>
> void main()
> {
>      writefln("%12.3F",log(1-0.9999)/log(1-(1-0.6)^^20));
> }

My fault, for ldc and gdc you will get same result as C++ only when
you use pow not ^^(operator) and use doubles:

import std.stdio;
import std.math;

void main()
{
     writefln("%12.3F",log(1-0.9999)/log((1-pow(1-0.6,20))));
}


More information about the Digitalmars-d-learn mailing list