floating point - how to perform high precision calculations in D? -


for universitary work have approximate numbers - euler 1 series. therefore have add small numbers, have problems precision. if number ist small doesn't influence result.

real s;  //sum of previous terms ulong k; //factorial  s += 1.0/ k; 

after each step, k gets smaller, after 10th round result isn't changeing anymore , stuck @ 2.71828

if need solution run using native types should able reasonable results trying add numbers of similar magnitude. 1 way compute first x terms of series, , repeatedly replace 2 smallest numbers there sum:

auto data = real[n]; foreach(i, ref v; data) {   v = fn(i); }  while(data.length > 1) {   data.sort(); // iirc .sort deprecated forget replaced it.   data[1] += data[0];   data = data[1..$]; }  return data[0]; 

(a min heap make bit faster.)


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -