GithubHelp home page GithubHelp logo

Comments (3)

SethMMorton avatar SethMMorton commented on May 28, 2024

This is a good find!

If this were written as an int (i.e. 3 * 10**106196) then Python would be able to return it as an int just fine without overflow. In this sense, string_contains_intlike_float isn't wrong to return true.

On the other hand, the algorithm being used to return an int from fast_real if parsing the input as an integer fails is to parse as a float, and then convert float output to int. In this case, float("03e106196") is inf, which creates an OverflowError when passed to int.

I think the bug is not in string_contains_intlike_float, but rather in str_to_PyInt_or_PyFloat. Consider the following:

In [2]: import fastnumbers

In [3]: fastnumbers.fast_float("03e106196")
Out[3]: inf

In [4]: fastnumbers.fast_real("03e106196")
Out[4]: '03e106196'

In [5]: fastnumbers.fast_real("03e106196", coerce=False)
Out[5]: inf

In [6]: fastnumbers.fast_real("03e106196", raise_on_invalid=True, coerce=False)
Out[6]: inf

In [7]: fastnumbers.fast_real("03e106196", raise_on_invalid=True)
---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
<ipython-input-7-64eb4b6453ff> in <module>()
----> 1 fastnumbers.fast_real("03e106196", raise_on_invalid=True)

OverflowError: cannot convert float infinity to integer

In [8]: fastnumbers.__version__
Out[8]: '2.0.4'

If fast_float can return a number, fast_real should always be able to return a number, but this is not the case with coerce=True (the default). This is because the string looks like it should be an int but is too big to be stored as a float.

The correct thing to do would be to detect if inf was returned, and in that case do some special handling on the string to ensure an int is returned. I'll give this some thought.

from fastnumbers.

SethMMorton avatar SethMMorton commented on May 28, 2024

In your opinion, what should be the correct behavior?

>>> fastnumbers.fast_real("03e106196") == inf  # coerce=True

or

>>> fastnumbers.fast_real("03e106196") == 3 * 10**106196  # coerce=True

I would expect the same result whether raise_on_invalid is True or False.

from fastnumbers.

davidsmf avatar davidsmf commented on May 28, 2024

oh, good question. Python says "inf" for the literal 3e106196, so I'd go with that.

from fastnumbers.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.