# How to limit the number digits of constants in displayed real numbers?

hello, when i define a function and let Sage calculate the derivatives, the constants in the output functions have many digits. example:

sage:x=var('x')
sage:f=5*x*exp(-0.1*x^2)
sage:diff(f)
Sage output:-x^2*e^(-0.100000000000000*x^2) + 5*e^(-0.100000000000000*x^2).


Is there a way to limit the number of digits?

edit retag close merge delete

Maybe a mod can change the title to limit the number of digits in displayed real numbers...

( 2010-12-09 03:57:45 -0600 )edit

Sort by » oldest newest most voted

Of course, the problem is really in the expression itself:

sage: f = 5*x*exp(-.1*x^2)
sage: f
5*x*e^(-0.100000000000000*x^2)


One way to avoid this sort of thing is to avoid using decimals at all.

sage: f = 5*x*exp(-1/10*x^2)
sage: diff(f,x)
-x^2*e^(-1/10*x^2) + 5*e^(-1/10*x^2)


(Notice the preferred syntax diff(f,x), since in principle one could be taking a partial derivative with respect to some other variable.)

We used to have a way to display a lot fewer digits, but that got nixed once we switched to Gi/Pynac for basic symbolics. I don't recall what the status of that is. All the ways I can think of to display fewer digits for now also would give you less precision, but I think there may still be a way to do it. In any case, I would not recommend using decimals unless you only have inexact data; you might as well get the benefit of exact symbolics!

more

more

This has also been discussed in answer to a related question: Changing number of decimal digits displayed in output, and there are a few references there.

more