Hello,

There appears to be extremely minimal documentation on how floats are
formatted on output.  All I really see is that float.__str__() is
float.__repr__().  So that means that float->str->float does not
result in a different value.

It would be nice if the output format for float was documented, to the
extent this is possible.  #python suggested that I propose a patch,
but I see no way to write a documentation patch without having any
clue about what Python promises, whether in the CPython implementation
or as part of a specification for Python.

What are the promises Python makes about the str() of a float?  Will
it produce 1.0 today and 1.0e0 or +1.0 tomorrow?  When is the result
in exponential notation and when not?  Does any of this depend on the
underlying OS or hardware or Python implementation?  Etc.

I'm guessing that Python is consistent with an IEEE 754
"external character sequence", but don't know what the IEEE
specification says or whether python conforms.

I don't really care whether there's documentation for __str__() or
__repr__() or something else.  I'm just thinking that there should be
some way to guarantee a well defined "useful" float output formatting.
By "useful" I mean in exponential notation when non-exponential
notation is over-long.

I am writing a program that sometimes prints python floats and want to
be able to document what is printed.  Right now I can't truly
guarantee anything, other than the nan and inf and -inf
representations.  (I feel comfortable with nan and the like because I
don't see it likely that their representations will change.)  Of
course I could always re-implement Python's float.__repr__() in Python
so as to have full control, but this should be pointless.  Python's
output representation is unlikely to change and Python should be able
to make sufficient promises about its existing float representation.

I suppose there are similar issues with integers, but the varieties of
floating point number implementations and the existence of both
exponential and non-exponential representations make float
particularly problematic and representations potentially mercurial.

I also don't know if documentation changes with regard to external
representations would require a PEP.

I have found the following related information:

Use shorter float repr when possible
https://bugs.python.org/issue1580

https://github.com/python/cpython/blob/master/Python/pystrtod.c#L831

String conversion and formatting
https://docs.python.org/3/c-api/conversion.html

sys.float_repr_style
https://docs.python.org/3/library/sys.html#sys.float_repr_style

object.__str__(self)
https://docs.python.org/3/reference/datamodel.html#object.__str__

At the end of the day I don't _really_ care.  But having put thought
into the matter I care enough to write this email and ask the
question.

Regards,

Karl <k...@karlpinc.com>
Free Software:  "You don't pay back, you pay forward."
                 -- Robert A. Heinlein
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/FV22TKT3S2Q3P7PNN6MCXI6IX3HRRNAL/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to