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

    struct on WiPy gives different output than on PC

    Scheduled Pinned Locked Moved Discussion
    wipystruct
    4 Posts 2 Posters 532 Views 2 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.
    • A Offline
      alexpul
      last edited by

      I'm sending serial messages that consist of float values encoded using struct between my WiPy and PC (python 2.7). When I encode a decimal value, like 3.4, on the WiPy and then send it to the PC and decode it I get 3.4000000953674316. I noticed that the output of struct.pack('<d',3.4) is slightly different on the WiPy and on the PC.

      Code on WiPy:

      x_wipy= struct.pack('d', 3.4)      #encode value
      print(binascii.hexlify(x_wipy))    #display encoding
      >>> b'0000004033330b40'
      

      Code on PC (python 2.7):

      x_pc = struct.pack('d', 3.4)     #encode value
      print(binascii.hexlify(x_pc))    #display encoding
      >>> 3333333333330b40
      

      Does anyone know why they differ? Could it be different versions of python?

      Other observations:

      • WiPy decodes both x_wipy and x_pc correctly to 3.4
      • PC decodes x_pc to 3.4 and x_wipy to 3.4000000953674316
      • WiPy returns bytes, PC returns str -> this is due to different python versions
      robert-hhR 1 Reply Last reply Reply Quote 0
      • robert-hhR Offline
        robert-hh @alexpul
        last edited by

        @alexpul Micropython does supports only 32 bit floats with, giving a ~7 digit precision, and 3.4 being not a binary fraction cannot be encoded w/o precision loss into a float. That is different for numbers, which can be represented as binary fraction, like 3.5. PC Python uses 64 bit floats.
        So what you see are rounding errors in different occurences.

        1 Reply Last reply Reply Quote 2
        • A Offline
          alexpul
          last edited by

          Thanks for the great reply @robert-hh!

          For any other readers that may be having similar problems: I'm sending the encoded data from the wipy to my PC and I found that even if I encoded it using a single precision float ('f') the value still showed in python as a 64 bit number (hence the really long decimal value). I found that if I forced python to see the number as 32 bit using numpy.float32(x_wipy) then the decimal was correctly represented.

          robert-hhR 1 Reply Last reply Reply Quote 0
          • robert-hhR Offline
            robert-hh @alexpul
            last edited by

            @alexpul Indeed, but both pack to the same bytes object.
            PC:

            >>> struct.pack("f", 3.4)
            b'\x9a\x99Y@'
            >>> struct.unpack("f", b'\x9a\x99Y@')
            (3.4000000953674316,)
            >>> 
            

            Pycom:

            >>> struct.pack("f", 3.4)
            b'\x9a\x99Y@'
            
            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