What's the max integer a variable can hold?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting What's the max integer a variable can hold?
# 1  
Old 07-22-2010
What's the max integer a variable can hold?

I would like to know the maximum integer that a variable can hold. Actually one of my variable holds value 2231599773 and hence the script fails to process it.Do we have any other data type or options available to handle this long integers?
# 2  
Old 07-22-2010
As long as you do not attempt arithmetic operations, a string of numbers can be huge.
Shell arithmetic is limited to signed long 32 bit - +/- 2147483648

awk does double arithmetic - DBL_MAX DBL_MIN in limits.h, usually 15 digits of precision

bc can deal with up to 20 digits of precision.
# 3  
Old 07-23-2010
Thanks.
But when i run the below script im able to process values which are longer than the 32 bit value.Pls advise.


Code:
D=12345678912332232
echo $D
let DD=$D*20
echo DD id $DD
if [[ $DD -gt 0 ]]
then
  echo PASS  # prints PASS successfully as it intend to be.
else
  echo FAILLLLLLLL
fi


Last edited by Scott; 07-23-2010 at 09:08 AM.. Reason: Code tags, please...
# 4  
Old 07-23-2010
You did run the script didn't you.

I get the following output:
Code:
12345678912332232
DD id 1503437728
PASS

The number in variable $DD is clearly corrupted during the arithmetic operation. This is because of binary overflow.
The largest positive number you can hold in a shell integer variable is:
+2147483647 .
-which is exactly one less than (1024 x 1024 x 1024 x 2).


To work in Shell with larger numbers you must use and external program such as "bc" and store intermediate numbers in text variables.
Code:
ANSWER=""
ANSWER=`echo "12345678912332232 * 20" | bc`
echo "${ANSWER}"

246913578246644640

# 5  
Old 07-23-2010
oh!! I get the output as below. im using ksh.

12345678912332232
DD id 246913578246644640
PASS
# 6  
Old 07-23-2010
Are you by any chance on a modern 64-bit Operating System with ksh93 ?
# 7  
Old 07-23-2010
ksh93 on 32-bit Vista SUA
Code:
#!/bin/ksh93

integer D=12345678912332232
printf "%ld %ld\n" $D $D*20

gives
Code:
$ ./test
12345678912332232 246913578246644640

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Variable hold in UNIX job

In log directory file contain log files and files contain some unique job name which is dynamically created (example=55555 ),if job get 55555 then send alert message, but after next run if it find ---55555 then no need send alert message While checking the first job name you are capturing the Job... (3 Replies)
Discussion started by: Kalia
3 Replies

2. Shell Programming and Scripting

Date/Time parts to variable as integer

Hi Guys, i guess there is a several ways to grub the strings from date and time like THISMONTH='/bin/date +%m' but the hard part is to add or sub that string to a variable i tried to use let command TWOMONTHSAGO=$THISMONTH declare -i TWOMONTHSAGO let TWOMONTHSAGO-=2 but there... (1 Reply)
Discussion started by: CyR0iz4l1v3
1 Replies

3. Solaris

How to Use a Variable as Integer?

hello, i am writing a script that takes the UID from the PASSWD and then i want to increse the Number by one. for the Next user. i cannot get this to work that a variable is as interger example: set i = 0 set $i = $+1 it's in tcsh if it's mather (10 Replies)
Discussion started by: shatztal
10 Replies

4. Shell Programming and Scripting

Read Variable From Fille And Convert it to Integer

I read 3 variables from from Inputfile.txt the third one "startnumber" is a number when i compare it with 9 ($startnumber -le 9) it give's me a "unary operator expected", i know that -le is for number comparison. What i need is to convert $startnumber to integer (i have try to do it with expr but... (8 Replies)
Discussion started by: marios
8 Replies

5. UNIX for Dummies Questions & Answers

built in variable to hold the name of therunning script...

Hi, I have multiple functions that can be called by any shell script. These functions have inbuilt echo statements to logs their activity into a log file. When I run multiple shell scripts that call these functions, they all log into the same log file and I am not able to differentiate which... (2 Replies)
Discussion started by: new_learner
2 Replies

6. UNIX for Dummies Questions & Answers

Subtracting an Integer from a Variable

Hello, I am in following situation.- COUNT=`ls -l | wc -l` echo $COUNT ---> 26 NO_OF_FILES=$COUNT-1 echo $NO_OF_FILES ---> 26-1 Here, I want the output to be 25. How could I do this. It seems simple, but I am not getting it. Please help me. (2 Replies)
Discussion started by: The Observer
2 Replies

7. UNIX for Dummies Questions & Answers

Converting a String variable into Integer

Hi, I am passing a variable to a unix function. However when I try to assign the value to another variable like typeset -i I_CACHE_VAL=$2 Is this because of String to Integer conversion? I get an error. Please help me with thsi. Thanks (2 Replies)
Discussion started by: neeto
2 Replies

8. UNIX for Advanced & Expert Users

MAX SIZE ARRAY Can Hold it

Hi, Do anyone know what's the max size of array (in awk) can be store before hit any memory issue. Regards (3 Replies)
Discussion started by: epall
3 Replies

9. Shell Programming and Scripting

Max size of variable

What is the maximum amount of characters that you can have in a varible name in the ksh shell? (1 Reply)
Discussion started by: lesstjm
1 Replies

10. UNIX for Dummies Questions & Answers

capturing the output of grep as integer variable

Hi, I have an expression using grep and nawk that captures the ID number of a given Unix process. It gets printed to screen but I don't know how to declare a variable to this returned value! For example, ps -ef|grep $project | grep -v grep | nawk '{print $2}' This returns my number. How... (2 Replies)
Discussion started by: babariba
2 Replies
Login or Register to Ask a Question