View Full Version : [SOLVED] Integer limit??
jesushero
September 21st, 2008, 03:56 AM
Ok, this is a pretty stupid question... But why is the maximum integer value that bash can handle 9223372036854775807? Is that signed or not?
What happens in cases of floating point numbers?
Is there a way around this in bash?
When it wraps, in 9223372036854775807 + 1, it goes to -9223372036854775808.. How is this possible and 9223372036854775808 is not?
user@machine:~$ a=9223372036854775807
user@machine:~$ echo $a
9223372036854775807
user@machine:~$ let a++
user@machine:~$ echo $a
-9223372036854775808
user@machine:~$ let a++
user@machine:~$ echo $a
-9223372036854775807
ghostdog74
September 21st, 2008, 04:12 AM
it depends on your machine. bash performs maths in the largest integer size your machine supports (intmax_t).
unutbu
September 21st, 2008, 04:13 AM
According to http://en.wikipedia.org/wiki/Integer_(computer_science)
a 64-bit signed long int has the range you describe.
(See also /usr/include/limits.h). It seems likely based on this that bash implements integers as 64-bit signed long ints.
You can see by looking at the Two's complement chart (http://en.wikipedia.org/wiki/Signed_number_representations) that the Two's complement interpretation of
0111...111 is 9223372036854775807, and
1000...000 is -9223372036854775808
mssever
September 21st, 2008, 04:13 AM
Is there a way around this in bash?
Store numbers as strings and use bc for math:
~:$ a=9223372036854775807
~:$ ((b = $a + 1))
~:$ echo $b
-9223372036854775808
~:$ echo $a + 1 | bc
9223372036854775808(Bash can't handle floating point numbers at all, so you have to use something like bc for that.
slavik
September 21st, 2008, 05:03 AM
I would say that if you are using a two digit number in a bash script, you need to rethink your design (unless this is for educational purpose, then there are exceptions).
mssever
September 21st, 2008, 06:13 AM
I would say that if you are using a two digit number in a bash script, you need to rethink your design (unless this is for educational purpose, then there are exceptions).
Wait. A two digit number is a sign of bad design? Huh?
jesushero
September 21st, 2008, 09:39 PM
I would say that if you are using a two digit number in a bash script, you need to rethink your design (unless this is for educational purpose, then there are exceptions).
Would you recommend Python or Perl for scripting then? Or something else? I use bash if I need to use a lot of system commands and little else.
ghostdog74
September 22nd, 2008, 02:00 AM
what other things do you want to do , beside the calculation?
awk 'BEGIN{printf "%.2f", 9223372036854775807 + 1}'
mssever
September 22nd, 2008, 03:42 AM
Would you recommend Python or Perl for scripting then? Or something else? I use bash if I need to use a lot of system commands and little else.
See this thread: http://ubuntuforums.org/showthread.php?t=910779
slavik
September 22nd, 2008, 04:52 AM
Wait. A two digit number is a sign of bad design? Huh?
You're changing my words and misunderstanding the point. Please re-read my post.
pmasiar
September 22nd, 2008, 05:05 AM
You're changing my words and misunderstanding the point. Please re-read my post.
I did read it again and I am no wiser. How are two digits related to anything? Some background info maybe?
BTW, OP: see wiki in my sig for links to learn Python (excellent as replacement of bash, with unlimited size integers when you need them), including training tasks.
slavik
September 22nd, 2008, 05:13 AM
I did read it again and I am no wiser. How are two digits related to anything? Some background info maybe?
BTW, OP: see wiki in my sig for links to learn Python (excellent as replacement of bash, with unlimited size integers when you need them), including training tasks.
There's a reason the basic datatype in a shell is a string. Shells aren't intended to perform mathematical calculations even if they can.
ghostdog74
September 22nd, 2008, 05:32 AM
There's a reason the basic datatype in a shell is a string. Shells aren't intended to perform mathematical calculations even if they can.
some shells, like zsh, or ksh99, can perform floating point, eg zsh
# echo $(( 1.0 / 2.0 ))
0.5
otherwise, standard utility for the shell is to use bc, or dc.
Powered by vBulletin® Version 4.2.2 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.