Ruud H.G. van Tol on Fri, 24 Dec 2021 21:54:24 +0100


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

Re: Significant figures printing in GP.



On 2021-12-24 21:05, Bill Allombert wrote:
On Fri, Dec 24, 2021 at 03:55:20PM -0400, R J Cano wrote:

[...]
For instance, let's assume certain calculation involving only real
numbers in the final result, yields
x=3.00002575
Then the expected result must be printed:
3.000026

Hello Remy,
What you can do is
sigfig2(x,n)=printf("%.*g\n",n,x);
? sigfig2(x,7)
3.000026
(you can also use Strprintf to the same effect).

Strprintf(fmt,{x}*): deprecated alias for strprintf
:)


A numeric variant:

dround(x, d=1)= {
  my(f=floor(1+log(x)/log(10)));
  1.*round(x*10^(d-f))/10^(d-f)
}

? x
%13 = 3.000025750000000000
? dround(x,7)
%14 = 3.0000260000000000000000000000000000000

-- Ruud