Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

force floating point arithmetics



Hiya,

this is a follow up on
http://unix.stackexchange.com/a/66755/22565

I was trying to find a way for $((...)) to always do floating
point arithmetics as in $((1/3)) not returning 0, and the best I
could come up with was appending . to any sequence of decimal
digits that is not otherwise part of a hex number (or number in
another base) or of a variable name or 12e-20 type numbers like:

setopt extendedglob
calc() printf '%.6g\n' $((${*//(#bm)(([0-9.]##[eE][-+][0-9]##|[[:alnum:]_#]#[.#_[:alpha:]][[:alnum:]_#]#)|([0-9]##))/$MATCH${match[3]:+.}}))
alias 'calc=noglob calc'

calc 1/3

...

Like the OP there, I'm surprised there's no easier way to do it,
but maybe I've not looked hard enough.

So, is there a way to force floating point arithmetics there
even when entering integer constants?

Thanks,
Stephane



Messages sorted by: Reverse Date, Date, Thread, Author