Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

conversation float to int on MCU #4035

Closed
ondratu opened this issue Aug 8, 2018 · 3 comments
Closed

conversation float to int on MCU #4035

ondratu opened this issue Aug 8, 2018 · 3 comments

Comments

@ondratu
Copy link

ondratu commented Aug 8, 2018

Hi, i see some magic int / float operations on MCU. I want to get timestamp aligned to minutes on ESP8266. Here is output from console:

>>> now = time.time()
>>> int(now/60)*60, now
(587021520, 587021509)

I test in on your web unicorn and here is output:

>>> now = 587021509                                                            
>>> int(now/60)*60, now                                                        
(587021520, 587021509)

Unix port works OK:

now = 587021509 
>>> int(now/60)*60, now
(587021460, 587021509)

When i do this more-time on ESP, the first number is still same:

>>> now = time.time()
>>> int(now/60)*60, now
(587021760, 587021738)
>>> now = time.time()
>>> int(now/60)*60, now
(587021760, 587021759)
>>> now = time.time()
>>> int(now/60)*60, now
(587021760, 587021767)
>>> int(now/60)*60, now
(587021760, 587021767)
>>> now = time.time()
>>> int(now/60)*60, now
(587021760, 587021837)
>>> now = time.time()
>>> int(now/60)*60, now
(587021760, 587021901)

firmware on ESP8266: esp8266-ota-20180511-v1.9.4.bin

>>> sys.version
'3.4.0'
>>> sys.implementation
(name='micropython', version=(1, 9, 4))
>>> sys.platform
'esp8266'

Later i would to test it on STM32F4 Discover and on ESP32.

@dpgeorge
Copy link
Member

dpgeorge commented Aug 8, 2018

The esp8266 only has 30 bits of precision for a float, so there is loss of precision here. Instead, try to use truncating integer division: int(now // 60) * 60

@robert-hh
Copy link
Contributor

It's most likely caused by the limited significant digits in a float operation. You should get the expected result by (now // 60) * 60 instead of int(now / 60) * 60.

@ondratu
Copy link
Author

ondratu commented Aug 8, 2018

Thanks, that works.

This could be documented in differences that it works like C not like CPython. Resp. that not works like C, here we could get overflow number, not still the same.

@dpgeorge dpgeorge closed this as completed Dec 4, 2018
tannewt added a commit to tannewt/circuitpython that referenced this issue Jan 23, 2021
It used to always return None.

Fixes micropython#4035
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants