Output rounding bug
-
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.
-
-
@t3p3tony Thanks Tony!
-
@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
-
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
-