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
.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With