# latex representation for expresions like 1.42531324e-41

6 views

### ma...@mendelu.cz

Oct 30, 2009, 3:23:02 AM10/30/09
to sage-devel
Hello all

The latex representation of numbers in scientific notation works as
excepted, unless we have these numbers as results from numerical
integral. Compare the last two outputs i nthe session below. Why is

sage: latex(A[1])
1.66533453694e-14

and not

sage: latex(A[1])
1.66533453694 \times 10^{-14}

?

[marik@um-bc107 /opt/sage-4.2]\$ ./sage
----------------------------------------------------------------------
| Sage Version 4.2, Release Date: 2009-10-24 |
| Type notebook() for the GUI, and license() for information. |
----------------------------------------------------------------------
sage: A=integral_numerical(x,2,1)
sage: A
(-1.5, 1.6653345369377348e-14)
sage: latex(A[1])
1.66533453694e-14
sage: latex(1.4565421654e-15)
1.45654216540000 \times 10^{-15}
sage:

### William Stein

Oct 30, 2009, 3:32:54 AM10/30/09
On Fri, Oct 30, 2009 at 12:23 AM, ma...@mendelu.cz <ma...@mendelu.cz> wrote:
>
> Hello all
>
> The latex representation of numbers in scientific notation works as
> excepted, unless we have these numbers as results from numerical
> integral. Compare the last two outputs i nthe session below. Why is
>
> sage: latex(A[1])
> 1.66533453694e-14
>
> and not
>
> sage: latex(A[1])
> 1.66533453694 \times 10^{-14}
>
> ?

It's because one is a Python float and one is a Sage real number:

sage: latex(1.5e-20)
1.50000000000000 \times 10^{-20}
sage: latex(float(1.5e-20))
1.5e-20
sage: type(1.5e-20)
<type 'sage.rings.real_mpfr.RealLiteral'>

You can get around this as follows:

sage: A=integral_numerical(x,2,1)
sage: latex(RR(A[1]))
1.66533453693773 \times 10^{-14}

Obviously, it would be nice if the latex command were improved so it
is aware of Python floats. That would be a nice enhancement you
can contribute to sage.

-- William

### ma...@mendelu.cz

Oct 30, 2009, 3:43:51 AM10/30/09
to sage-devel

On 30 říj, 08:32, William Stein <wst...@gmail.com> wrote:
>
> Obviously, it would be nice if the latex command were improved so it
> is aware of Python floats. That would be a nice enhancement you
> can contribute to sage.

Thank you for quick answer. Can you give me few pointers where to fix
it?

Robert

>
> -- William

Oct 30, 2009, 3:49:34 AM10/30/09

Typing latex? at the prompt shows that the function is defined in sage/
misc/latex.py . Edit this file (inside devel/sage-xxx), and then type
sage -br to rebuild.

- Robert

### William Stein

Oct 30, 2009, 3:49:39 AM10/30/09

Look in the file

SAGE_ROOT/devel/sage/sage/misc/latex.py

There is a table in there called latex_table. You have to
add an entry of the form

float:float_function

where you might first try something like this just to get it to work:

def float_function(x):
from sage.rings.all import RR
return RR(x)._latex_()

You might decide to write your own direct float function too (that
doesn't use RR), which might be slightly faster or more accurate in
some way. I don't know.

William

--
William Stein
Associate Professor of Mathematics
University of Washington
http://wstein.org

### ma...@mendelu.cz

Oct 30, 2009, 4:58:13 AM10/30/09
to sage-devel
On 30 říj, 08:49, William Stein <wst...@gmail.com> wrote:
>
> There is a table in there called latex_table. You have to
> add an entry of the form
>
> float:float_function
>
> where you might first try something like this just to get it to work:
>
> def float_function(x):
> from sage.rings.all import RR
> return RR(x)._latex_()
>

Thanks, I fixed this, running tests now and then I'll open trac and
submit a patch.

Your solution produces output like 1.00000000\times 10^{32} for input
like latex(float(1e+32)) and hence I choosed slightly different
solution (essentially the method _latex_() from the file which defines
latex representation for real numbers but without calling RR).

Thank you very much
Robert M.

### kcrisman

Oct 30, 2009, 9:57:23 AM10/30/09
to sage-devel
Hold up, this is already IN trac and Jason Grout submitted a patch

- kcrisman

### Jason Grout

Oct 30, 2009, 10:49:37 AM10/30/09
ma...@mendelu.cz wrote:
> Hello all
>
> The latex representation of numbers in scientific notation works as
> excepted, unless we have these numbers as results from numerical
> integral. Compare the last two outputs i nthe session below. Why is
>
> sage: latex(A[1])
> 1.66533453694e-14
>
> and not
>
> sage: latex(A[1])
> 1.66533453694 \times 10^{-14}
>
> ?
>

There is a positive review for a patch to fix this. It should be in the
next version of Sage.

Thanks,

Jason