Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to approximate a number to a n number of decimal places?

Tags:

math

maxima

I have started using maxima just a few weeks ago (actually I have just used it a few times) with the wxMaxima interface for OS X.

I have tried to find a solution for this around the web, but maybe because I am blind or maybe because I do not have much experience in searching in the official documentation of maxima, I have not find a concrete solution yet.

How do I approximate a number to a n number of decimal places? For example, if I use float(22/7), it gives me this huge number 3.142857142857143, but I just want for example to approximate it to the 3rd decimal place 3.143.

like image 543
nbro Avatar asked Dec 14 '22 15:12

nbro


2 Answers

fpprec controls the actual number of significant digits in a Maxima bigfloat. That applies only to bigfloats and not to ordinary (fixed precision, IEEE 754) floats.

See also fpprintprec which controls how many digits are printed, which applies to bigfloats and to ordinary floats alike. So another solution for you is:

(%i1) fpprintprec : 4 $
(%i2) float (22/7);
(%o2)                                3.143
like image 164
Robert Dodier Avatar answered Dec 28 '22 07:12

Robert Dodier


rnddp(x,dp):=float((round(x*10^dp)/10^dp));

rndcdp(z,dp):=rnddp(realpart(z),dp)+%i*rnddp(imagpart(z),dp);

rnddp will round its first argument to the number of decimal places provided in the second argument.

rndcdp does the same for complex numbers.

The result(s) are the approximated values, not just displayed values.

like image 23
peter 124 Avatar answered Dec 28 '22 06:12

peter 124