Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    micropython round

    Scheduled Pinned Locked Moved Issues & Bugs
    upython on gpyrounding
    7 Posts 4 Posters 786 Views 4 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • K Offline
      kjm
      last edited by

      >>> round(691.69, 1)
      691.7
      

      makes sense

      >>> round(691.49, 1)
      691.5
      

      likewise

      >>> round(691.59, 1)
      691.5999
      

      how come?

      mfalkviddM robert-hhR 2 Replies Last reply Reply Quote 0
      • mfalkviddM Offline
        mfalkvidd @kjm
        last edited by mfalkvidd

        @kjm 691.6 (and many other numbers) can not be representex correctly by a floating point number. Try entering the values into https://www.h-schmidt.net/FloatConverter/IEEE754.html and you'll see that there will be a large error.

        Longer explanation: http://www.phys.uconn.edu/~rozman/Courses/P2200_15F/downloads/floating-point-guide-2015-10-15.pdf#page3

        K 1 Reply Last reply Reply Quote 1
        • robert-hhR Offline
          robert-hh @kjm
          last edited by robert-hh

          @kjm Decimal fractions cannot always be precisely expressed by a binary number. What you see is the attempt to get as close as possible to the value of 691.6. If a number is a binary fraction, like 591.5, it will be shown precisely, otherwise it is not guaranteed. If you use formatted print, the representation is rounded as expected. try print("%.1f" % round(691.59, 1))

          P.S.: if you just enter 591.6 at the REPL, it should also be echoed as 591.5999

          1 Reply Last reply Reply Quote 1
          • C Offline
            crumble
            last edited by

            And it is a good practice to compare values with a delta.
            as you can see, not all numbers can be represented. So instead of

            if val == 191.6:
              #do something
            

            you shall do something like

            delta = 0.1
            target = 691.6
            if  target-delta >= val && val <= target+delta:
              #do something
            

            So you take care that not all decimal values are represented. Which delta you shall choose depends on the precission you need and how your values a calculated. If in this case you multiply the error of 0.0001 by 10,000, you may have to use a huge delta of 1.

            Be careful with zero. On some architectures 0 does not equals -0. Lucky us, on the ESP32 it seems to be equal.

            1 Reply Last reply Reply Quote 1
            • K Offline
              kjm
              last edited by

              This post is deleted!
              1 Reply Last reply Reply Quote 0
              • K Offline
                kjm @mfalkvidd
                last edited by

                @mfalkvidd I miss my raspberry pi

                >>> round(691.59, 1)
                691.6
                
                robert-hhR 1 Reply Last reply Reply Quote 0
                • robert-hhR Offline
                  robert-hh @kjm
                  last edited by robert-hh

                  @kjm This is a recurring discussion in Micropython. See also for instance:
                  https://forum.micropython.org/viewtopic.php?f=2&t=802
                  https://github.com/micropython/micropython/issues/3516

                  But still, the number which the raspi (C-Python) displays is not the number internally stored. However, using a 64 bit float representation, the error is way smaller.

                  Note also, that rounding was improved in the micrpython.org branch:
                  https://github.com/micropython/micropython/commit/b768cc6ca8ed6d7430b86dc7ccb9ee2391b3a251
                  For the esp8266 build, I get:

                  MicroPython v1.9.4-704-g62b4bebf-dirty on 2018-12-04; ESP module with ESP8266
                  Type "help()" for more information.
                  >>> round(691.59, 1)
                  691.6
                  

                  But not so for the ESP32 build. The loboris branch again delivers 691.6
                  But, so sum up: it is not the rounding, that "fails", it's the display of decimal fractions.
                  If you just enter 691.6, REPL will show 691.5999. Similar with other numbers, but not every number.

                  1 Reply Last reply Reply Quote 0

                  Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                  Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                  With your input, this post could be even better 💗

                  Register Login
                  • 1 / 1
                  • First post
                    Last post

                  Pycom on Twitter

                  Pycom