Ask Your Question
4

how to tell sage to give numeric result.

asked 2011-06-06 03:55:23 +0100

yotama9 gravatar image

If I give sage the following:

sin(2*pi/30)

It give in return

sin(1/15*pi)

While I want the numeric value. I know that the most accurate result is the one given by sage by I need to compare it with another result I gut from numerical calculation.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
8

answered 2011-06-06 04:51:33 +0100

DSM gravatar image

updated 2011-06-06 11:05:44 +0100

You can use the numerical_approx functions/methods. Type "help(numerical_approx)" for a full description. It can be called in lots of ways -- too many, probably! -- and you can specify the precision in bits or in decimal digits:

sage: q = sin(2*pi/30)
sage: 
sage: numerical_approx(q)
0.207911690817759
sage: q.numerical_approx()
0.207911690817759
sage: q.n()
0.207911690817759
sage: 
sage: q.n(prec=100) # bits
0.20791169081775933710174228441
sage: q.n(digits=50) # decimal digits
0.20791169081775933710174228440512516621658476062772

You can also use

sage: n(q)
0.207911690817759
sage: N(q)
0.207911690817759

but I'll be honest, I don't like that very much. I use n and N all the time as variables, so these aren't very useful for me.

UPDATE:

I should have mentioned another way -- you can often coerce objects from one form to another by calling the desired ring. So these work too:

sage: RR
Real Field with 53 bits of precision
sage: RR(q)
0.207911690817759
sage: RealField(100)
Real Field with 100 bits of precision
sage: RealField(100)(q)
0.20791169081775933710174228441
edit flag offensive delete link more

Comments

Great answer, DSM.

kcrisman gravatar imagekcrisman ( 2011-06-06 12:40:46 +0100 )edit
1

answered 2016-05-13 11:42:24 +0100

mug896 gravatar image

this style also work

N(sin(2*pi/30), digits=30)
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: 2011-06-06 03:55:23 +0100

Seen: 25,926 times

Last updated: Jun 06 '11