math - What is the standard way to maintain accuracy when dealing with incredibly precise floating point calculations in C++? -


i'm in process of converting program c++ scilab (similar matlab) , i'm required maintain same level of precision kept previous code.

note: although maintaining same level of precision ideal. it's acceptable if there some error finished result. problem i'm facing (as i'll show below) due looping, calculation error compounds rather quickly. if final result thousandth or off (e.g. 1/1000 vs 1/1001) won't problem.

i've briefly looked number of different ways including:

int vs float example: instead of using float 12.45, store integer being 124,500. convert when appropriate so. note: i'm not sure how work code i'm working (more detail below).

an example of how program producing incorrect results:

for (int = 0; <= 1000; i++) {     (int j = 0; j <= 10000; j++)     {         // calculation computed less precision in scilab         float1 = (1.0 / 100000.0);          // above error of float2 become significant end of loop         float2 = (float1 + float2);     } } 

my question is:

is there accepted way go retaining accuracy in floating point arithmetic or 1 of above methods suffice?

maintaining precision when porting code difficult do. not because languages have implicitly different perspectives on float is, because of different algorithms or assumptions of accuracy limits are. example, when performing numerical integration in scilab, may use gaussian quadrature method. whereas might try using trapezoidal method. 2 may both working on identical ieee754 single-precision floating point numbers, different answers due convergence characteristics of 2 algorithms. how around this?

well, can go through scilab source code , @ of algorithms uses each thing need. can replicate these algorithms taking care of pre- or post-conditioning of data scilab implicitly (if @ all). that's lot of work. and, frankly, not best way spend time. rather, using interfacing other languages section developer's documentation see how can call scilab functions directly c, c++, java, or fortran code.

of course, second option, have consider how going distribute code (if need to).scilab has gpl-compatible license, can bundle code. however, quite big (~180mb) , may want bundle pieces need (e.g., don't need whole interpreter system). more work in different way, guarantees numerical-compatibility current scilab solutions.