I mentioned that in my post; however it doesn't satisfy the problems I have (mainly being that eval(repr(x))==x)
I still think unifying it as a constant is better because then the repr of a float gives a string which, if evaluated, gives the float back exactly. Using math.inf or string conversion throws an error if you try to evaluate it On Fri, Sep 4, 2020, 6:05 PM Christopher Barker <python...@gmail.com> wrote: > This is in the math module already, along with NaN: > > In [1]: import math > > > In [2]: math.inf > > Out[2]: inf > > In [3]: math.nan > > Out[3]: nan > > The same value > > In [4]: math.inf == float('inf') > > Out[4]: True > > but not the same object -- i.e. it's not a singleton. > > In [5]: math.inf is float('inf') > > Out[5]: False > > -CHB > > > > On Fri, Sep 4, 2020 at 9:49 AM Cade Brown <brown.c...@gmail.com> wrote: > >> I am positing that Python should contain a constant (similar to True, >> False, None), called Infinity. >> >> It would be equivalent to `float('inf')`, i.e. a floating point value >> representing a non-fininte value. It would be the positive constant; >> negative infinity could retrieved via `-Infinity` >> >> Or, to keep float representation the same, the name `inf` could be used, >> but that does not fit Python's normal choice for such identifiers (but >> indeed, this is what C uses which is the desired behavior of string >> conversion) >> >> I think there are a number of good reasons for this constant. For example: >> * It is also a fundamental constant (similar to True, False, and None), >> and should be representable as such in the language >> * Requiring a cast from float to string is messy, and also obviously >> less efficient (but this performance difference is likely insignificant) >> * Further, having a function call for something that should be a >> constant is a code-smell; in general str -> float conversion may throw an >> error or anything else and I'd rather not worry about that. >> * It would make the useful property that `eval(repr(x)) == x` for >> floating point numbers (currently, `NameError: name 'inf' is not defined >> `) >> >> This makes it difficult to, for example, naively serialize a list of >> floats. For example: >> >> ``` >> >>> x = [1, 2, 3, 4] >> >>> repr(x) >> '[1, 2, 3, 4]' >> >>> eval(repr(x)) == x >> True >> >>> x = [1, 2, 3, float('inf')] >> >>> repr(x) >> '[1, 2, 3, inf]' >> >>> eval(repr(x)) == x >> Traceback (most recent call last): >> File "<stdin>", line 1, in <module> >> File "<string>", line 1, in <module> >> NameError: name 'inf' is not defined >> ``` >> >> To me, this is problematic; I would expect it to work seamlessly as it >> does with other floating point constants. >> >> A few rebuttals/claims against: >> - Creating a new constant (Infinity) which is unassignable may break >> existing code >> - Converting a float to string is not the same as it is in C. Whil >> >> I also realize that there is `math.inf`, but I argue that the constant is >> more fundamental than that, and it still doesn't solve the problem with >> `repr()` I described >> >> Thanks, >> ---- >> *Cade Brown* >> Research Assistant @ ICL (Innovative Computing Laboratory) >> Personal Email: brown.c...@gmail.com >> ICL/College Email: c...@utk.edu >> >> >> _______________________________________________ >> Python-ideas mailing list -- python-ideas@python.org >> To unsubscribe send an email to python-ideas-le...@python.org >> https://mail.python.org/mailman3/lists/python-ideas.python.org/ >> Message archived at >> https://mail.python.org/archives/list/python-ideas@python.org/message/XMA6KOBLPABV7EL5GV2BIRC2ESYKXMVV/ >> Code of Conduct: http://python.org/psf/codeofconduct/ >> > > > -- > Christopher Barker, PhD > > Python Language Consulting > - Teaching > - Scientific Software Development > - Desktop GUI and Web Development > - wxPython, numpy, scipy, Cython >
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/QHOV45S2QN33KLHC5UZH33HA7VJQBXXO/ Code of Conduct: http://python.org/psf/codeofconduct/