# Ticket #7356(closed enhancement: fixed)

Opened 4 years ago

## fixed latex representation for floats

Reported by: Owned by: robert.marik AlexGhitza minor sage-4.3 basic arithmetic Robert Marik Robert Bradshaw sage-4.3.alpha1

### 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.

## 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:3 Changed 4 years ago by robert.marik

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

### 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

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 4 years ago by robertwb

• Status changed from needs_info to needs_review

### Changed 4 years ago by robertwb

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

apply on top of latex-float-4.2.1.patch

### comment:8 Changed 4 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 3 years ago by mhansen

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