bash "let" barfs on addition
Matthew Flaschen
matthew.flaschen at gatech.edu
Thu Mar 12 23:34:56 UTC 2009
Evuraan::ഏവൂരാന് wrote:
> I know, I can use bc,dc,awk, python and even a desk calculator wide
> enough :) - but, I am curious as to why is "let" letting us down?
First of all, it is nothing specific to let, as you can see from:
echo $((7540113804746346429+4660046610375530309))
Second, if you use the source (apt-get source, or just look at
http://www.opensource.apple.com/darwinsource/Current/bash-76.2/bash/expr.c)
you can see:
"All arithmetic is done as intmax_t integers with no checking for
overflow (though division by 0 is caught and flagged as an error)."
In this case, intmax_t is obviously a 64-bit signed two's complement
integer. Your some exceeds that bound, hence the overflow into a
negative. Now, if you're wondering "How come that's the limit? Where
is it specified?" it /isn't/. Math is not in the POSIX shell
specification
(http://www.unixinfo.org/books/ProUnixShellProg/ProUnixShellProg-Posix.html),
so bash considers itself free to provide as much or as little as it
likes.
You are right to use bc if you crave more. It is /documented/
(http://www.opengroup.org/onlinepubs/000095399/utilities/bc.html) to
support arbitrary precision.
Matt Flaschen
More information about the ubuntu-users
mailing list