hermann on Thu, 18 Jan 2024 23:48:12 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: print() does output numbers in scientific notation violating JSON number spec


On 2024-01-18 23:09, hermann@stamm-wilbrandt.de wrote:

Is there an option to make PARI/GP produce "0.00001" as output instead
of scientific representation?

I found workaround:

? print(0.00001)
1.0000000000000000000000000000000000000 E-5
? print(strprintf("%f",0.00001))
0.000010000000000000000000000000000000000000
?

I cannot use printf() since the float is arg of wlog() function:

    jscad.wlog=(args[..])->{
        my(nargs=#args);
        if(nargs>0,filewrite1(writer,args[1]);
            foreach(args[2..nargs],arg,
                filewrite1(writer," ");filewrite1(writer,arg)
            )
        );
        filewrite(writer,"");
    };

So I now use

        wlog("eps =", strprintf("%f",eps));


and get output that can be read from JavaScript/nodejs.


I would say this is at least an interoperability bug.
It should be fixed, if there is not a really good reason to output that space.

The issue is discussed in "3.4.12 format" of 2.15.4 GP user guide.
"In style g, nonzero real numbers are printed in f format, except when their decimal exponent
is < −4, in which case they are printed in e format."

"If the conversion style is e, real numbers are printed in scientific format, always with an explicit exponent, e.g. 3.3 E-5." shows the incorrect space in doc as well.


For interoperability, f format output should be fixed to not show space between frac and exp parts.


Regards,

Hermann.