bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: REQUEST - bash floating point math support


From: Koichi Murase
Subject: Re: REQUEST - bash floating point math support
Date: Thu, 6 Jun 2024 00:09:55 +0900

2024年6月5日(水) 21:41 Zachary Santer <zsanter@gmail.com>:
> Bash could potentially detect floating point literals within
> arithmetic expansions and adjust the operations to use floating point
> math in that case. [...]

ksh and zsh are already behaving in that way, and if Bash would
support the floating-point arithmetic, Bash should follow their
behavior.

Actually, there's already a stub for the double-precision
floating-point number for shell-variable values in variables.h [1],
though it seems to be there for more than 20 years since 2.05b.

[1] 
https://git.savannah.gnu.org/cgit/bash.git/tree/variables.h?h=devel&id=dbb48b978671394bcb33c9f34656a9aadf40a318#n75

> For completeness, a floating point variable attribute could
> potentially be added. 'declare -i var' will cause var to be treated as
> an integer, though var can be referenced within an arithmetic
> expansion without this attribute. declare -f and -r (real, anybody?)
> are already taken for other things, so I'm not sure what the natural
> choice of option would be.

ksh uses -E for the double-precision floating-point numbers, and `-E'
is not used by Bash.

By the way, people are writing shell libraries to perform the
floating-point arithmetic in pure Bash, though I've never tried them
and am unsure about their quality, performance, and API experience.

https://github.com/clarity20/shellmath
https://github.com/m1ndflay3r/bash-float

We can also do the fixed-point arithmetic more easily and efficiently
(except for special functions such as sin, cos, tan, exp, log, etc.).
These are examples for specific purposes without needing precisions
(and thus not all the operations are implemented):

https://github.com/aneeshdurg/bash-raytracer/blob/main/math.sh
https://github.com/akinomyoga/blesh-contrib/blob/master/colorglass.bash#L247-L381

--
Koichi



reply via email to

[Prev in Thread] Current Thread [Next in Thread]