How to write 10exp(-5) in SAGEMATH

41 views
Skip to first unread message

Fjordforsk A/S

unread,
Jul 3, 2017, 10:39:54 AM7/3/17
to sage-support
Hi, how does one write 10^(-8) ?

Is it as the conventional way 10**(-8) or is it 10exp(-8) ?

Simon King

unread,
Jul 3, 2017, 12:21:34 PM7/3/17
to sage-s...@googlegroups.com
Hi!

On 2017-07-03, Fjordforsk A/S <fjordf...@gmail.com> wrote:
> Hi, how does one write 10^(-8) ?
>
> Is it as the conventional way 10**(-8) or is it 10exp(-8) ?

Sage is based on Python, thus, 10**(-8) definitely works.

In addition, Sage uses a preparser to make the user interface still
a bit nicer than Python. Therefore, 10^(-8) works as well.

I don't know if you use Sage only by its user interface, or if
you also write programs. In the former case, my answer ends here.
In the latter case, I give you a bit more background, that may help
to avoid some pitfalls in programming:

In the user interface, You can also write
f(t) = sin(t^2)
and Sage's preparser will translate it into a sequence of commands that
defines t as a symbolic variable and f as a symbolic function

Here is what the preparser does internally:
sage: preparse('f(t) = sin(t^2)')
'__tmp__=var("t"); f = symbolic_expression(sin(t**Integer(2))).function(t)'

If you write a .py or .pyx module, the preparser would not be invoked,
und thus you have to write 10**(-8), whereas 10^(-8) would be interpreted
as bit-wise XOR.

And since var('t') inserts the new variable t into the global namespace,
to define the function f(t)=sin(t^2), you have to write something like
t = var('t')
f = symbolic_expression(sin(t**2)).function(t)
in your code.

Here, "2" would result in a Python integer, which is not the same
as a Sage Integer. Hence, if it is important for you to use the Sage
types rather than the Python types, you have to replace 2 by Integer(2)
or ZZ(2). And of course, you have to import these things first.

Best regards,
Simon

HG

unread,
Jul 3, 2017, 2:12:49 PM7/3/17
to sage-support
I would to the first or the second
n(10^(-8))
n(10**(-8))
the third I think it's not right but I am an expert

slelievre

unread,
Aug 30, 2017, 7:54:54 AM8/30/17
to sage-support


Le lundi 3 juillet 2017 16:39:54 UTC+2, Fjordforsk A/S a écrit :

> Hi, how does one write 10^(-8) ?
> Is it as the conventional way 10**(-8) or is it 10exp(-8) ?

Typing either 10**(-8) or 10^(-8) will produce a rational.
Note that you don't need the parentheses and could also
type 10**-8 or 10^-8.

If you want a floating point number, you could type 1e-8.

Checking the parents of 10^-8 and 1e-8, you will see that
one lives in Rational Field (also known as QQ) and the other
in Real Field with 53 bits of precision (also known as RR).

sage: 10**(-8)
1/100000000
sage: 10^(-8)
1/100000000
sage: 10**-8
1/100000000
sage: 10^-8
1/100000000
sage: 1e-8
1.00000000000000e-8
sage: (10^-8).parent()
Rational Field
sage: (1e-8).parent()
Real Field with 53 bits of precision

Reply all
Reply to author
Forward
0 new messages