Search code examples
floating-accuracyscilab

Is there a way to display more than 25 digits when outputting in Scilab?


I'm working with Scilab 5.5.2 and when using the format command I can display at most 25 digits of a number. Is there a way to somehow display more than this number?


Solution

  • Scilab operates with double precision floating point numbers; it does not support variable-precision arithmetics. Double precision means relative error of %eps, which is 2-52, approximately 2e-16.

    This means you can't even get 25 correct decimal digits: when using format(25) you get garbage at the end. For example,

    format(25); sqrt(3)
    

    returns 1.732050807568877 1931766

    I separated the last 7 digits here because they are wrong; the correct value of sqrt(3) begins with

          1.732050807568877 2935274
    

    Of course, if you don't mind the digits being wrong, you can have as many as you want:

     strcat([sprintf('%.15f', sqrt(3)), "1111111111111111111111111111111"])
    

    returns 1.7320508075688771111111111111111111111111111111.

    But if you want to have arbitrary exceeding of real numbers, Scilab is not the right tool for the job (correction: phuclv pointed out Multiple Precision Arithmetic Toolbox which might work for you). Out of free software packages, mpmath Python library implements arbitrary precision of real numbers: it can be used directly or via Sagemath or SymPy. Commercial packages (Matlab, Maple, Mathematica) support variable precision too.

    As for Scilab, I recommend using formatted print commands such as fprintf or sprintf, because they actually care about the output being meaningful. Example: printf('%.25f', sqrt(3)) returns

    1.7320508075688772000000000 
    

    with garbage replaced by zeros. The last nonzero digit is still off by 1, but at least it's not meaningless.