Ask Your Question
1

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

asked 2010-12-09 08:34:00 +0100

shinji3 gravatar image

updated 2011-06-16 15:00:07 +0100

Kelvin Li gravatar image

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 flag offensive close merge delete

Comments

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

kcrisman gravatar imagekcrisman ( 2010-12-09 10:57:45 +0100 )edit

3 Answers

Sort by ยป oldest newest most voted
2

answered 2010-12-09 10:56:58 +0100

kcrisman gravatar image

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!

edit flag offensive delete link more
0

answered 2010-12-09 14:09:21 +0100

shinji3 gravatar image

That helps me. Thank you for your fast reply.

edit flag offensive delete link more
0

answered 2010-12-09 15:01:38 +0100

niles gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2010-12-09 08:34:00 +0100

Seen: 1,679 times

Last updated: Dec 09 '10