ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 13 May 2016 11:42:24 +0200how to tell sage to give numeric result.https://ask.sagemath.org/question/8150/how-to-tell-sage-to-give-numeric-result/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.Mon, 06 Jun 2011 03:55:23 +0200https://ask.sagemath.org/question/8150/how-to-tell-sage-to-give-numeric-result/Answer by mug896 for <p>If I give sage the following:</p>
<pre><code>sin(2*pi/30)
</code></pre>
<p>It give in return</p>
<pre><code>sin(1/15*pi)
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/8150/how-to-tell-sage-to-give-numeric-result/?answer=33396#post-id-33396this style also work
N(sin(2*pi/30), digits=30)Fri, 13 May 2016 11:42:24 +0200https://ask.sagemath.org/question/8150/how-to-tell-sage-to-give-numeric-result/?answer=33396#post-id-33396Answer by DSM for <p>If I give sage the following:</p>
<pre><code>sin(2*pi/30)
</code></pre>
<p>It give in return</p>
<pre><code>sin(1/15*pi)
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/8150/how-to-tell-sage-to-give-numeric-result/?answer=12419#post-id-12419You 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
Mon, 06 Jun 2011 04:51:33 +0200https://ask.sagemath.org/question/8150/how-to-tell-sage-to-give-numeric-result/?answer=12419#post-id-12419Comment by kcrisman for <p>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:</p>
<pre><code>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
</code></pre>
<p>You can also use</p>
<pre><code>sage: n(q)
0.207911690817759
sage: N(q)
0.207911690817759
</code></pre>
<p>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.</p>
<p>UPDATE:</p>
<p>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:</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/8150/how-to-tell-sage-to-give-numeric-result/?comment=21615#post-id-21615Great answer, DSM.Mon, 06 Jun 2011 12:40:46 +0200https://ask.sagemath.org/question/8150/how-to-tell-sage-to-give-numeric-result/?comment=21615#post-id-21615