Wow!
I started a thread several years ago about doing maths in the shell (or scripted) - and I mentioned some stuff about using awk to do floating point, in the shell (or a shell script).
But I just learned today (someone shared a table on FB - about features of each shell - but - when I just went back there - Zuckerberg decided to refresh my feed and I’ve NO IDEA how to get that table back!) that ZSH can do floating point and I just tested it :
╭─x@titan /mnt/BARGEARSE/home/x/X/10base2/2023/12-December/002
╰─➤ echo $((0.5*10.0))
5.
╭─x@titan /mnt/BARGEARSE/home/x/X/10base2/2023/12-December/002
╰─➤ echo $((0.5*61.0))
30.5
╭─x@titan /mnt/BARGEARSE/home/x/X/10base2/2023/12-December/002
╰─➤ echo $((0.6*35))
21.
It’s not perfect - but my main use case of floating point in the shell is to convert hours / minutes to decimal values (and vice versa).
Which is a kinda coincidence - as I noticed @nevj 's timely suggestion for calculating if / when Xmas eve falls on a Sunday…
I had a shell script I wrote 5+ years ago to suggest the next train I could get (allowing for a 15 min walk to the station) to get home at the end of the day… I used “scale” and “bc” to do floating point (awk wasn’t cutting the mustard) arithmetic on time values…
So - I might - if I ever have the need, to write a shell script that calculates hours and minutes - write an actual ZSH script instead of a BASH script (or I guess I could just call ZSH in a BASH script ?).
But then again - I’m guessing Python probably has those features built in (e.g. “import datetime
”).
And - ZSH’s floating point can leave kinda ugly results :
╭─x@titan ~/bin ‹main*›
╰─➤ echo $((21.0/60.0)) 130 ↵
0.34999999999999998
I’d need to investigate whether I can round that up to the next integer … Hmmm - but this kinda does it for me :
╭─x@titan ~/bin ‹main*›
╰─➤ echo $((21.0/60.0*100))
35.
The reason I chose “21” and 0.35, that’s 'cause in the 80’s and early 90’s I worked for the Commonwealth government, and our working day was 7.35 hours, or 7’21" - was it like that for you at the CSIRO @nevj ? Seemed like a strange, almost arbitrary value, but it was 'cause we were on a 73.5 hour fortnight - no idea where that idea came from… I think at least the Australian Tax Office, is still on those hours - it was part of “flexi time” - i.e. you’d build up enough extra hours (e.g. working from 9-5 with an hour for lunch) to maybe take an afternoon off, or go to the pub for lunch on a Friday and NOT COME BACK . After that job - I worked for the WA state government (both jobs were in hospitals) and we were on 7.5 hour days, would work 8 hours, and have an RDO (rostered day off) every fortnight - that’s how it was supposed to work, but I never took my RDO’s…