Bill Allombert on Fri, 24 Dec 2021 21:05:27 +0100


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

Re: Significant figures printing in GP.


On Fri, Dec 24, 2021 at 03:55:20PM -0400, R J Cano wrote:
> Dear PARI users.
> 
> Please.
> 
> Is this code a neat way of displaying real numbers rounded up to
> certain quantity of significant figures?,
> 
> sigfig(a,{b=%})={if(type(b)==type(3.141592),my(x=digits(truncate(b*10^default(realprecision)))[1..a+1]);x[a]+=(x[a+1]>4);return(x[1..a]))}
> 
> 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).

Cheers,
Bill