Re: Accuracy of floating point calculations

2019-10-29 Thread Twilight via Digitalmars-d-learn

On Tuesday, 29 October 2019 at 16:11:45 UTC, Daniel Kozak wrote:
On Tue, Oct 29, 2019 at 5:09 PM Daniel Kozak 
 wrote:



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.)/log(1-(1-0.6)^^20));
}


AFAIK dmd use real  for floating point operations instead of 
double


Thanks for the clarification. It appears then that because of 
dmd's real calculations, it produces more accurate results, but 
maybe slower. (Calculating the result with the high precision 
calculator at https://keisan.casio.com/calculator agrees with 
dmd.)


Accuracy of floating point calculations

2019-10-29 Thread Twilight via Digitalmars-d-learn

D calculation:

  writefln("%12.2F",log(1-0.)/log(1-(1-0.6)^^20));

837675572.38

C++ calculation:

  cout<<<'\n';


837675573.587

As a second data point, changing 0. 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?