Comments (3)
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.
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.
oh, good question. Python says "inf" for the literal 3e106196
, so I'd go with that.
from fastnumbers.
Related Issues (20)
- Request for an option to not ignore underscores in numeric literals HOT 10
- Update documentation and metadata
- Integrate with numpy and pandas HOT 9
- Unit test numeric issues on 32bit arm CPU HOT 3
- Speed not better than Python's int/float HOT 6
- Make most options keyword-only
- Rename "key" option to "on_fail"
- [BUG] FastNumbers can crash with a SystemError due to returning NULL without setting an exception HOT 5
- Fastest way to check is and object is int or float in one pass HOT 14
- Proposal: change behavior of isfloat with respect to treatment of float("nan") HOT 19
- Proposal: change behavior of isfloat function with respect to treatment of strings containing integers HOT 3
- Proposal: Do not raise an exception on None HOT 5
- python3.9 compatibility HOT 5
- Re-write using C++ and pybind11
- Add support to release Linux aarch64 wheels HOT 1
- Broken 3.2.0 installation
- Missing -lm breaks build on armv7hl
- Error: <built-in function isint/isfloat> returned NULL without setting an error HOT 2
- Use fast C++ methods like std::from_chars or fast_float HOT 1
- Improve performance with METH_FASTCALL
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fastnumbers.