You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
math command can't do floating point division #1643
It's a shame there's no easy way to get bc to print more sensible numbers - passing -l means that all results are printed at precision 20, without any truncation of integers.
I've modified my math function to format the numbers correctly :
functionmath --description 'Perform math calculations in bc'if count $argv> /dev/null
switch $argv[1]
case -h --h --he --hel --help
__fish_print_help math
return 0
end
set -l out (env LC_NUMERIC=C printf'%g' (echo $argv|env BC_LINE_LENGTH=0 bc))
test -z "$out"
and return 1
echo$out
switch $outcase 0
return 1
end
return 0
end
return 2
end
It's probably slower, but it works :-/
BTW, Is there a way to deactivate * expansion for the math command ? Its annoying to have to escape it for multiplications.
@zanchey It also worked without overriding it, but it used coma instead of dots on some locales.
This was problematic, because it disallowed reusing the result of the math command.
I like calc much more than bc because it can also calculate complex expressions. Calc is part of each distribution I have used. The debian package is called apcalc. If you could live without posix-bc, this math function would probably solve these issues:
function math --description 'Perform math calculations in calc'
if count $argv >/dev/null
switch $argv[1]
case -h --h --he --hel --help
__fish_print_help math
return 0
end
set -l out (calc $argv| tr -d '\t')
test -z "$out"; and return 1
echo $out
switch $out
case 0
return 1
end
return 0
end
return 2
end
If you are still looking for bc replacement, try awk. It should be installed everywhere, because it's POSIX. Floating point calculations work too. Just wrap it with something like this:
I'm rather concerned about using a complete programming language for this purpose, as this allows running anything as math. While I don't think that anyone should be using math as a externally accessible calculator, the change to awk would introduce a security issue (hint: AWK can run code like system("ls")). Sandbox mode of gawk is gawk specific.
Basically nobody should be doing complex math in the shell, that said floating point division is common. I suggest aliasing math to begin with "scale=4;". Which provides four decimal places of precision only during division. As a result:
Urgh... the current behavior is actually quite difficult to replace and at least umask depends on the fact that it returns no trailing zeroes (set oct (math $oct - 4)). This would need a python-esque "if there's no dot, it's an int" scan.
@faho posted a question about this issue on Gitter.im. My response was: Do we even need to add out-of-the-box support for floating point arithmetic? The main problem I have with this issue, not just the original comment, is that I see no justification for adding floating point arithmetic to fish.
FWIW, I don’t know what it’s formal semantics are but informally ksh performs integer arithmetic unless at least one operand has a decimal point. Too, a bare period in bc means the result of the previous expression. We don’t explicitly support multiple expressions so we could simply test whether a period appears anywhere in the expression and pass the -l flag if we see one. The risk, obviously, is that someone might currently be passing floating point values and relying on the integer arithmetic behavior.
So @faho's suggestion to add a math -f flag to explicitly request floating point semantics from bc is the safest path forward. Assuming there is a good reason to add this feature.
My original use case when I opened this issue was for formatting the CMD_DURATION variable on long-running commands. I ended up creating an external command that formats the duration exactly how I want it. So my single use case is no longer relevant. Furthermore, even if I had implemented it in fish, only having integer precision really wouldn't be a problem for displaying a human-friendly duration. That being said, it seems like you should be able to do simple floating point arithmetic in the shell, but the preceding discussion makes it obvious that this isn't as simple as it sounds.
The math -f suggestion seems fine to me, although it's still unfortunate that you get so many digits of precision.
I've decided this can be trivially implemented by adding a -sN flag to set the scale of the result with the default being zero to match existing behavior. This way you can ask for N digits after the decimal point. Pull-request forthcoming.
Note that this isn't perfect because bc has weird behavior: it floors the result rather than rounding it regardless of whether integer or floating-point (scaled) mode is in effect. But since we explicitly say we're using bc people have to deal with its somewhat odd behavior.
P.S., The reason an absurd number of digits after the decimal point were being printed by previous solutions is they ran bc -l which implicitly does scale=20.
Activity
zanchey commentedon Sep 15, 2014
It's a shame there's no easy way to get
bcto print more sensible numbers - passing-lmeans that all results are printed at precision 20, without any truncation of integers.m-r-r commentedon Sep 20, 2014
Hello,
I've modified my
mathfunction to format the numbers correctly :It's probably slower, but it works :-/
BTW, Is there a way to deactivate
*expansion for themathcommand ? Its annoying to have to escape it for multiplications.zanchey commentedon Sep 22, 2014
Using
printfis smart. I don't think overriding the locale is a good idea, though. I'll merge something similar.sugar700 commentedon Sep 23, 2014
It may be a good idea to avoid using
env, if feasible (instead use scopedset -lx).envis external program, whilesetis not.m-r-r commentedon Sep 25, 2014
@zanchey It also worked without overriding it, but it used coma instead of dots on some locales.
This was problematic, because it disallowed reusing the result of the math command.
@xfix Thanks for pointing that, i had no idea :-p
Here is new version without
env: https://gist.github.com/m-r-r/87459042708bd605bbbdFeel free to copy it.
zanchey commentedon Sep 27, 2014
The fact that bc doesn't use the locale to take input is pretty awesome.
Closed with thanks!
zanchey commentedon Sep 27, 2014
3ac28f2 reverts this, due to #1723. Our options appear to be either easy floating-point arithmetic or easy modulo arithmetic, but not both.
tannhuber commentedon Sep 27, 2014
I like calc much more than bc because it can also calculate complex expressions. Calc is part of each distribution I have used. The debian package is called apcalc. If you could live without posix-bc, this math function would probably solve these issues:
I haven't tested it in detail.
14 remaining items
shamrin commentedon Dec 6, 2014
If you are still looking for
bcreplacement, tryawk. It should be installed everywhere, because it's POSIX. Floating point calculations work too. Just wrap it with something like this:Minimally supported operators:
+ - * / % ^(exponentiation).sugar700 commentedon Dec 8, 2014
I'm rather concerned about using a complete programming language for this purpose, as this allows running anything as math. While I don't think that anyone should be using
mathas a externally accessible calculator, the change toawkwould introduce a security issue (hint: AWK can run code likesystem("ls")). Sandbox mode of gawk is gawk specific.cdosborn commentedon May 11, 2015
Basically nobody should be doing complex math in the shell, that said floating point division is common. I suggest aliasing math to begin with "scale=4;". Which provides four decimal places of precision only during division. As a result:
zanchey commentedon May 11, 2015
Unfortunately, setting
scalestops modulo arithmetic from working, which at least some existing code relies on.faho commentedon Oct 12, 2015
Why don't we just check if there's a "%" in the arguments, and turn on float-mode if it's not (maybe splitting on ";")?
faho commentedon Oct 12, 2015
Urgh... the current behavior is actually quite difficult to replace and at least umask depends on the fact that it returns no trailing zeroes (
set oct (math $oct - 4)). This would need a python-esque "if there's no dot, it's an int" scan.anordal commentedon Feb 23, 2016
Omg, I'll never use
mathagain. It's got python3-style explicit integer division!krader1961 commentedon May 2, 2016
@faho posted a question about this issue on Gitter.im. My response was: Do we even need to add out-of-the-box support for floating point arithmetic? The main problem I have with this issue, not just the original comment, is that I see no justification for adding floating point arithmetic to fish.
FWIW, I don’t know what it’s formal semantics are but informally ksh performs integer arithmetic unless at least one operand has a decimal point. Too, a bare period in bc means the result of the previous expression. We don’t explicitly support multiple expressions so we could simply test whether a period appears anywhere in the expression and pass the -l flag if we see one. The risk, obviously, is that someone might currently be passing floating point values and relying on the integer arithmetic behavior.
So @faho's suggestion to add a
math -fflag to explicitly request floating point semantics frombcis the safest path forward. Assuming there is a good reason to add this feature.jcelliott commentedon May 2, 2016
My original use case when I opened this issue was for formatting the
CMD_DURATIONvariable on long-running commands. I ended up creating an external command that formats the duration exactly how I want it. So my single use case is no longer relevant. Furthermore, even if I had implemented it in fish, only having integer precision really wouldn't be a problem for displaying a human-friendly duration. That being said, it seems like you should be able to do simple floating point arithmetic in the shell, but the preceding discussion makes it obvious that this isn't as simple as it sounds.The
math -fsuggestion seems fine to me, although it's still unfortunate that you get so many digits of precision.krader1961 commentedon May 3, 2016
I've decided this can be trivially implemented by adding a
-sNflag to set the scale of the result with the default being zero to match existing behavior. This way you can ask forNdigits after the decimal point. Pull-request forthcoming.Note that this isn't perfect because bc has weird behavior: it floors the result rather than rounding it regardless of whether integer or floating-point (scaled) mode is in effect. But since we explicitly say we're using
bcpeople have to deal with its somewhat odd behavior.P.S., The reason an absurd number of digits after the decimal point were being printed by previous solutions is they ran
bc -lwhich implicitly doesscale=20.floam commentedon Sep 17, 2016
Why is this listed for 2.4.0? Didn't this get addressed a while back?
faho commentedon Sep 17, 2016
@floam: Because it's not in 2.3.1. It'll be released with 2.4.0, so it's marked as such.