What is the precision of floating point calculations in Scilab?

前端 未结 2 1994
离开以前
离开以前 2021-01-16 09:22

Note: I\'ve used the Matlab tag just in case they maintain the same precision. (From what I can tell both programs are very similar.)

As a follow-up to a pr

2条回答
  •  长发绾君心
    2021-01-16 09:49

    Every common language (standard C++, scilab, matlab, ...) use the same format for representing decimal numbers. It's known at IEEE754 and its precise documentation is explained in:

    https://en.wikipedia.org/wiki/Double-precision_floating-point_format

    It implies that the precision remains constant on nearly all common used system. It's a number close to 2^-52 (or equivalently 2.2204e-16). It defines the "distance from 1.0 to the next largest double-precision number".

    When you're using scilab, you can confirm it with the %eps command https://help.scilab.org/docs/5.5.1/fr_FR/percenteps.html. For matlab, it's stored in the eps variable http://nl.mathworks.com/help/matlab/ref/eps.html. For C++, it's a bit harder (see: http://en.cppreference.com/w/cpp/types/numeric_limits/epsilon).

    So, don't worry about precision if you do not use a specific machine (atypic architectures or very very old computers or high precision decimals (64bits double)). The default ones will always follow the same standard (IEEE 754).

    But, don't forgot, even if it seems to be a constant, that error can is not the same between very high numbers and very small (the system is designed to have the best precision for the interval [0, 1[ and for the interval [1, MAXIMUM[).

    It can be shown in the following example:

    >>> 1e100 == 1e100+1
    True
    >>> 1 == 2
    False
    

    To ensure that your code is portable for different languages, I suggest you to explicitly refer to the functions that give the machine precision. For instance, in scipy: print(np.finfo(float).eps). But, generally, well designed algorithms won't become a lot different on machine with slightly different epsilons.

    For instance, if I implement a loop for something that will tends to be 0 (asymptoticly), in matlab, I should write:

    while(val < eps) do
    ...
    end
    

    So, the main advice should be: don't build an algorithm that would try to use too many information from the machine. Either you can use the real value of epsilon, either you can hard-code something like 2e-15. It would work on a lot of different machines.

提交回复
热议问题