R J Cano on Sat, 25 Dec 2021 03:32:18 +0100


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

Re: Significant figures printing in GP.


Thank you Bill,

Now i see... it is nice idea, gladly adding it sigfig2() to my ~/.gprc.gp

Just in case our other friends over here were interested, i found
instructive also to browse this (in order to understand what Mr.
Allombert suggested already):

https://www.tutorialspoint.com/c_standard_library/c_function_printf.htm

Again, merci...

Cheers,

--Remy



2021-12-24 16:05 GMT-04:00, Bill Allombert <Bill.Allombert@math.u-bordeaux.fr>:
> 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
>
>