First time here? Check out the FAQ!

Ask Your Question
4

how to tell sage to give numeric result.

asked 13 years ago

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.

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
8

answered 13 years ago

DSM gravatar image

updated 13 years ago

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
Preview: (hide)
link

Comments

Great answer, DSM.

kcrisman gravatar imagekcrisman ( 13 years ago )
1

answered 8 years ago

mug896 gravatar image

this style also work

N(sin(2*pi/30), digits=30)
Preview: (hide)
link

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: 13 years ago

Seen: 26,207 times

Last updated: Jun 06 '11