Opened 10 years ago

Closed 10 years ago

#7356 closed enhancement (fixed)

fixed latex representation for floats

Reported by: robert.marik Owned by: AlexGhitza
Priority: minor Milestone: sage-4.3
Component: basic arithmetic Keywords:
Cc: Merged in: sage-4.3.alpha1
Authors: Robert Bradshaw Reviewers: Robert Marik
Report Upstream: Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Description

Floats have no LaTeX representation and are formated using str function. Thus output of latex(float(1e25)) is '1e+25' and not '1 \times 10{25}'.

The solution is to define function to handle this like the function below

def float_function(x):
    r"""
    Returns the LaTeX code for a float ``x``.

    INPUT: ``x`` - float number

    EXAMPLES::

        sage: from sage.misc.latex import float_function
        sage: float_function(float(123.05))
        '123.05'
        sage: float_function(float(3e-15))
        '3 \\times 10^{-15}'
        sage: float_function(float(3.2e25))
        '3.2 \\times 10^{25}'
        sage: float_function(float(3.2e+15))
        '3.2 \\times 10^{15}'

        The output is in some cases shorter than latex method for real numbers.

        sage: float_function(float(1e+15))
        '1 \\times 10^{15}'
    """
    s = str(x)
    parts = s.split('e')
    if len(parts) > 1:
        # scientific notation
        if parts[1][0] == '+':
            parts[1] = parts[1][1:]
        s = "%s \\times 10^{%s}" % (parts[0], parts[1])
    return s

Will post simple patch, provided it passes tests.

Attachments (3)

trac_7356_marik.patch (1.8 KB) - added by robert.marik 10 years ago.
latex-float-4.2.1.patch (1.2 KB) - added by robertwb 10 years ago.
Use instead of other, applies on top of #7328
latex-float-4.2.1-reviewer.patch (702 bytes) - added by robert.marik 10 years ago.
apply on top of latex-float-4.2.1.patch

Download all attachments as: .zip

Change History (12)

Changed 10 years ago by robert.marik

comment:1 Changed 10 years ago by robert.marik

  • Status changed from new to needs_review

The patch for 4.2 is attached. When running tests I got two errors not related to the change in this trac. The first one is solved in #6479.

sage -t  "devel/sage/sage/calculus/desolvers.py"
sage -t  "devel/sage/sage/interfaces/maxima.py"

comment:2 Changed 10 years ago by robert.marik

sage -t  "devel/sage/sage/interfaces/maxima.py"

This test passed as well. (Error has been introduced by my custom settings in maxima-init.lisp file)

comment:3 Changed 10 years ago by robert.marik

According to this thread, there is another patch for this with slightly different output: #7328

comment:4 follow-up: Changed 10 years ago by jhpalmieri

  • Status changed from needs_review to needs_info

Should this be closed as a duplicate since #7328 has been closed with a positive review?

comment:5 in reply to: ↑ 4 Changed 10 years ago by robert.marik

Replying to jhpalmieri:

Should this be closed as a duplicate since #7328 has been closed with a positive review?

Perhaps yes, but the patch in this trac produces shorter output, so I think that this is better. The patch #7328 produces sometimes zeros which are not necessary at the end of decimal number.

comment:6 Changed 10 years ago by robertwb

  • Status changed from needs_info to needs_review

Changed 10 years ago by robertwb

Use instead of other, applies on top of #7328

comment:7 Changed 10 years ago by robertwb

I agree, less digits should be printed. Floats are more like RDF than RR, so I've posted a patch that applies on top of #7328. The attached patch works fine to (though will conflict with 4.2.1).

Changed 10 years ago by robert.marik

apply on top of latex-float-4.2.1.patch

comment:8 Changed 10 years ago by robert.marik

  • Status changed from needs_review to positive_review

Seems good, thanks for fixing. Since one test failed, I fixed it in reviewers patch which should be installed on the top of latex-float-4.2.1.patch

Tests are O.K. now. Positive review.

comment:9 Changed 10 years ago by mhansen

  • Authors set to Robert Bradshaw
  • Merged in set to sage-4.3.alpha1
  • Resolution set to fixed
  • Reviewers set to Robert Marik
  • Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.