int() Anomaly
int() Anomaly
>>> 5.5*3600
19800.0
>>> int(5.5*3600)
19800
>>> int(3155673600-19800)
3155653800
>>> int(3155673600-19800.0)
3155653888
Can somebody explain this anomaly?
19800.0
>>> int(5.5*3600)
19800
>>> int(3155673600-19800)
3155653800
>>> int(3155673600-19800.0)
3155653888
Can somebody explain this anomaly?
Re: int() Anomaly
I get the right answer on a 64bit machine running Ubuntu 20.04LTS, so I guess I'd be looking at how well float numbers are being represented in a Micropython port. There was some discussion about this recently, sorry can't find the specific thread. Or how a 10 digit number is represented?
Re: int() Anomaly
Limited precision for float values.
For your last example, the int gets converted to a float, the subtraction returns a float, you convert it to int.
For your last example, the int gets converted to a float, the subtraction returns a float, you convert it to int.
A few hours of debugging might save you from minutes of reading the documentation!
My repositories: https://github.com/karfas
My repositories: https://github.com/karfas
Re: int() Anomaly
As I understand, int() is truncating the decimal part and converting the data type to int. I am not an expert, but logic tells me that this process should not cause a deviation of 88.
Re: int() Anomaly
@karfas: Looks like you are right.
3155673600-19800.0 gives me 3.155654e+09
3155673600-19800.0 gives me 3.155654e+09
-
- Posts: 50
- Joined: Thu Jul 07, 2022 7:40 am
Re: int() Anomaly
If you work with float on MicroPython, that's the precision you can expect:
About seven digits.
More info: https://en.wikipedia.org/wiki/IEEE_754
The implicit int to float conversion:
results in:
3155673742.29431 3155673742.294311523437500
which is the nearest representation in float.
Code: Select all
print(f'{1.0/3.0:52.48f}')
# 0.33333334922790527343750000000
More info: https://en.wikipedia.org/wiki/IEEE_754
The implicit int to float conversion:
Code: Select all
x = 3155673600.0; print(f'{x:.15g} {x:30.15f}')
or
x = 3155673600; x = float(x); print(f'{x:.15g} {x:30.15f}')
3155673742.29431 3155673742.294311523437500
which is the nearest representation in float.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: int() Anomaly
This is not strictly correct. Most MP targets support 32 bit floats, but some support 64 bits, notably the Pyboard D SF6W. Other targets can support 64 bits with a compile option.TheSilverBullet wrote: ↑Sat Jul 23, 2022 7:46 amIf you work with float on MicroPython, that's the precision you can expect:
...
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
-
- Posts: 50
- Joined: Thu Jul 07, 2022 7:40 am
Re: int() Anomaly
So, would it'd be safe to assume that ›float‹ is the precision used on most boards while a very few selected ones provide ›double‹ resolution? That information might be a good candidate for the docs.pythoncoder wrote: ↑Sat Jul 23, 2022 8:31 amThis is not strictly correct. Most MP targets support 32 bit floats, but some support 64 bits, notably the Pyboard D SF6W. Other targets can support 64 bits with a compile option.TheSilverBullet wrote: ↑Sat Jul 23, 2022 7:46 amIf you work with float on MicroPython, that's the precision you can expect:
...
I'd imagine that most users just put the uf2 file de jour on the µC board and be done with it.
-
- Posts: 50
- Joined: Thu Jul 07, 2022 7:40 am
Re: int() Anomaly
Peter, I took a closer look at the sourcecode for 1.19.1 (particularly the rp2 port)pythoncoder wrote: ↑Sat Jul 23, 2022 8:31 amOther targets can support 64 bits with a compile option.
Would a change in mpconfigport.h:
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT)
to
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_DOUBLE)
be sufficient? Or did I overlook something important?
I'd be willing to sacrifice some speed in exchange for higher resolution.
Thanks,
Norbert
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: int() Anomaly
I believe this is correct, but I haven't actually tried it.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.