Ticket #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: | Work issues: | ||
| Report Upstream: | Reviewers: | Robert Marik | |
| Authors: | Robert Bradshaw | Merged in: | sage-4.3.alpha1 |
| 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
Change History
comment:1 Changed 4 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 4 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:4 follow-up: ↓ 5 Changed 4 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 4 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.
Changed 4 years ago by robertwb
-
attachment
latex-float-4.2.1.patch
added
Use instead of other, applies on top of #7328
comment:7 Changed 4 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 4 years ago by robert.marik
-
attachment
latex-float-4.2.1-reviewer.patch
added
apply on top of latex-float-4.2.1.patch
