Duet3D Logo

    Duet3D

    • Register
    • Login
    • Search
    • Categories
    • Tags
    • Documentation
    • Order

    Output rounding bug

    Gcode meta commands
    3
    6
    113
    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.
    • Schmart
      Schmart last edited by

      It seems I've run into strange behavior where floats are rounded off when used in GCODE or output with echo. It happens under specific conditions only, and I think I have determined which. Please see the isolated code fragments below, and especially the in-line comments:

      Case #1

      Note that the number of decimals when displaying variable 'c' in the output will be limited by the number of decimals of variable 'a'.

      var a = 0.1
      ; The problem seems to be triggered by the division operator
      var b = pi / 42
      var c = var.a + var.b
      
      echo "a: " ^ var.a
      echo "b: " ^ var.b
      
      ; Expected 0.1747998, but getting 0.2
      echo "c: " ^ var.c
      
      ; Shows 1.7, 17.5 and 174.8 respectively, with one decimal.
      ; But the internal precision seems to be there still!
      echo "c * 10: " ^ var.c * 10
      echo "c * 100: " ^ var.c * 100
      echo "c * 1000: " ^ var.c * 1000
      

      Case #2: One more decimal

      Note that this is identical to case #1, however, variable 'd' now has two decimals. Even when the trailing decimal is a zero, this affects the number of decimals in the output of variable 'f'.

      var d = 0.10
      var e = pi / 42
      var f = var.d + var.e
      
      echo "d: " ^ var.d
      echo "e: " ^ var.e
      
      ; Expected 0.1747998 as output, but getting 0.17 now
      echo "f: " ^ var.f
      
      ; Shows 1.75, 17.48 and 174.80 respectively, with two decimals
      echo "f * 10: " ^ var.f * 10
      echo "f * 100: " ^ var.f * 100
      echo "f * 1000: " ^ var.f * 1000
      

      Case #3: Workaround

      Note that there are no problems when the division operator is replaced by a multiplication operator when calculating variable 'h'.

      var g = 0.1
      var h = pi * 0.023809524
      var i = var.g + var.h
      
      echo "g: " ^ var.g
      echo "h: " ^ var.h
      
      ; Expecting 0.1747998 and getting it
      echo "i: " ^ var.i
      

      I hope you can reproduce the problem. I'm on RRF 3.4.0 beta 5.

      Creality CR-20 Pro with a Duet 2 Maestro, 3Dfused X+Z-axis rail kit, HIWIN-based DIY solution for the Y-axis, E3D Hemera, working ST7565 MiniPanel and various other small hardware improvements.

      T3P3Tony 1 Reply Last reply Reply Quote 0
      • T3P3Tony
        T3P3Tony administrators @Schmart last edited by

        @schmart thanks for this report. I have just checked and I can reproduce the issue so will flag it up to @dc42.

        www.duet3d.com

        Schmart 1 Reply Last reply Reply Quote 1
        • Schmart
          Schmart @T3P3Tony last edited by

          @t3p3tony Thanks Tony!

          Creality CR-20 Pro with a Duet 2 Maestro, 3Dfused X+Z-axis rail kit, HIWIN-based DIY solution for the Y-axis, E3D Hemera, working ST7565 MiniPanel and various other small hardware improvements.

          dc42 1 Reply Last reply Reply Quote 0
          • dc42
            dc42 administrators @Schmart last edited by

            @schmart thanks for reporting this. It is now fixed.

            25/10/2021, 18:17:02 	M98 P"0:/macros/Rounding bug 1"
            a: 0.1
            b: 0.0747998
            c: 0.1747998
            c * 10: 1.7479982
            c * 100: 17.4799824
            c * 1000: 174.7998352
            25/10/2021, 18:16:45 	Connection established
            25/10/2021, 18:16:27 	Upload of Duet2CombinedFirmware.bin successful after 1s
            25/10/2021, 18:07:44 	M98 P"0:/macros/Rounding bug 1"
            a: 0.1
            b: 0.0747998
            c: 0.2
            c * 10: 1.7
            c * 100: 17.5
            c * 1000: 174.8
            

            Duet WiFi hardware designer and firmware engineer
            Please do not ask me for Duet support via PM or email, use the forum
            http://www.escher3d.com, https://miscsolutions.wordpress.com

            dc42 Schmart 2 Replies Last reply Reply Quote 1
            • dc42
              dc42 administrators @dc42 last edited by

              I made a further improvement to avoid printing excess decimal places:

              25/10/2021, 18:29:53 	M98 P"0:/macros/Rounding bug 1"
              a: 0.1
              b: 0.0747998
              c: 0.1747998
              c * 10: 1.747998
              c * 100: 17.47998
              c * 1000: 174.7998
              

              Duet WiFi hardware designer and firmware engineer
              Please do not ask me for Duet support via PM or email, use the forum
              http://www.escher3d.com, https://miscsolutions.wordpress.com

              1 Reply Last reply Reply Quote 1
              • Schmart
                Schmart @dc42 last edited by

                @dc42 @T3P3Tony Wow! Fixed in minutes, in spite of all the 3.4 development happening: that's next level. I really appreciate this! 👍

                Creality CR-20 Pro with a Duet 2 Maestro, 3Dfused X+Z-axis rail kit, HIWIN-based DIY solution for the Y-axis, E3D Hemera, working ST7565 MiniPanel and various other small hardware improvements.

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post
                Unless otherwise noted, all forum content is licensed under CC-BY-SA