Round variable to nth digit
-
Hi all,
when adding two floating point numbers in RRF, the result is sometimes not "right":
I'm aware that this is due to floats being of limited size, and it's usually not a problem.
But at the moment I need to look at a lot of them and it's just anoying/difficult to read.
I haven't found a round() function, and echo doesn't allow formatting either.
Any ideas how I can make it more readable? -
@nikscha
Found a solution: (but it's a bit tedious)var x = global.variable_to_report set var.x = var.x *10000 set var.x = floor(var.x) set var.x = var.x/10000 echo "global.variable_to_report: ", var.x
Adjust the "10,000" to change the number of digits to be displayed.
Obviously this can be done in a single line too:set var.x = floor(var.x *10000)/10000
Still not perfect as it doesn't do any rounding, but easier to read:
-
-
-
@nikscha it works for me:
This is with a build a little later than 3.5.0-rc.1. Which firmware version are you using?
-
Duet 6HC + SBC
edit:
M122:=== Diagnostics === RepRapFirmware for Duet 3 MB6HC version 3.5.0-rc.1 (2023-08-31 16:19:24) running on Duet 3 MB6HC v1.01 (SBC mode) Board ID: 08DJM-9P63L-DJ3S0-7J1F8-3S86T-TBJZ8 Used output buffers: 1 of 40 (25 max) === RTOS === Static ram: 154852 Dynamic ram: 90580 of which 1388 recycled Never used RAM 96316, free system stack 194 words Tasks: SBC(2,rWait:,3.2%,390) HEAT(3,nWait,0.1%,323) Move(4,nWait,0.0%,216) CanReceiv(6,nWait,0.3%,769) CanSender(5,nWait,0.0%,335) CanClock(7,delaying,0.0%,349) TMC(4,nWait,29.0%,59) MAIN(2,running,66.9%,137) IDLE(0,ready,0.5%,30), total 100.0% Owned mutexes: HTTP(MAIN) === Platform === Last reset 04:25:03 ago, cause: software Last software reset at 2023-09-25 12:01, reason: User, Gcodes spinning, available RAM 96316, slot 2 Software reset code 0x6003 HFSR 0x00000000 CFSR 0x00000000 ICSR 0x0044a000 BFAR 0x00000000 SP 0x00000000 Task SBC Freestk 0 n/a Error status: 0x00 Aux0 errors 0,0,0 MCU temperature: min 36.8, current 38.5, max 38.7 Supply voltage: min 24.1, current 24.1, max 24.2, under voltage events: 0, over voltage events: 0, power good: yes 12V rail voltage: min 12.0, current 12.1, max 12.1, under voltage events: 0 Heap OK, handles allocated/used 99/10, heap memory allocated/used/recyclable 2048/1228/992, gc cycles 14 Events: 0 queued, 0 completed Driver 0: standstill, SG min n/a, mspos 8, reads 373, writes 20 timeouts 0 Driver 1: standstill, SG min n/a, mspos 8, reads 373, writes 20 timeouts 0 Driver 2: standstill, SG min n/a, mspos 8, reads 310, writes 84 timeouts 0 Driver 3: standstill, SG min n/a, mspos 8, reads 368, writes 26 timeouts 0 Driver 4: standstill, SG min n/a, mspos 8, reads 383, writes 11 timeouts 0 Driver 5: standstill, SG min n/a, mspos 8, reads 374, writes 20 timeouts 0 Date/time: 2023-09-25 16:26:27 Slowest loop: 60.73ms; fastest: 0.05ms === Storage === Free file entries: 20 SD card 0 not detected, interface speed: 37.5MBytes/sec SD card longest read time 0.0ms, write time 0.0ms, max retries 0 === Move === DMs created 125, segments created 0, maxWait 4088669ms, bed compensation in use: none, height map offset 0.000, ebfmin 0.00, ebfmax 0.00 no step interrupt scheduled Moves shaped first try 0, on retry 0, too short 0, wrong shape 0, maybepossible 0 === DDARing 0 === Scheduled moves 51, completed 51, hiccups 0, stepErrors 0, LaErrors 0, Underruns [0, 0, 0], CDDA state -1 === DDARing 1 === Scheduled moves 0, completed 0, hiccups 0, stepErrors 0, LaErrors 0, Underruns [0, 0, 0], CDDA state -1 === Heat === Bed heaters 0 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1, chamber heaters -1 -1 -1 -1, ordering errs 0 Heater 4 is on, I-accum = 0.0 === GCodes === Movement locks held by null, null HTTP* is doing "M122" in state(s) 0 Telnet is idle in state(s) 0 File is idle in state(s) 0 USB is idle in state(s) 0 Aux is idle in state(s) 0 Trigger* is idle in state(s) 0 Queue is idle in state(s) 0 LCD is idle in state(s) 0 SBC is idle in state(s) 0 Daemon* is idle in state(s) 0 0, running macro Aux2 is idle in state(s) 0 Autopause is idle in state(s) 0 File2 is idle in state(s) 0 Queue2 is idle in state(s) 0 Q0 segments left 0, axes/extruders owned 0x10000003 Code queue 0 is empty Q1 segments left 0, axes/extruders owned 0x0000000 Code queue 1 is empty === CAN === Messages queued 144316, received 891581, lost 0, boc 0 Longest wait 56ms for reply type 6041, peak Tx sync delay 39028, free buffers 50 (min 49), ts 79516/79515/0 Tx timeouts 0,0,0,0,0,0 === SBC interface === Transfer state: 5, failed transfers: 0, checksum errors: 0 RX/TX seq numbers: 24953/24953 SPI underruns 0, overruns 0 State: 5, disconnects: 0, timeouts: 0 total, 0 by SBC, IAP RAM available 0x25a98 Buffer RX/TX: 0/0-0, open files: 0 === Duet Control Server === Duet Control Server version 3.5.0-rc.1 (2023-09-01 07:16:31) Failed to deserialize the following properties: - MoveSegmentation -> Int32 from 1.0 Daemon: Buffered code: G4 P100 Buffered codes: 32 bytes total >> Doing macro daemon.g, started by system >> Number of flush requests: 1 Code buffer space: 4040 Configured SPI speed: 8000000Hz, TfrRdy pin glitches: 4 Full transfers per second: 38.66, max time between full transfers: 450.0ms, max pin wait times: 70.9ms/12.7ms Codes per second: 6.72 Maximum length of RX/TX data transfers: 7884/1224
Edit2:
SBC is a Raspberry Pi 4 -
@nikscha I'll ask @chrishamm to take a look because I think this may be a difference between SBC and standalone modes.
PS - meanwhile, have you tried using
floor(var.x+0.5)
instead offloor(var.x)
? -
@dc42 Thanks, I'll give that a try!
-