1753759 Members
5032 Online
108799 Solutions
New Discussion юеВ

Re: KSH Script Questikon

 
SOLVED
Go to solution
Chris Ivey
Advisor

KSH Script Questikon

OK all you KSH experts out there. I have an interesting one, but one I know that someone will know the answer to right away. How is this possible? Here is the text of a test script I wrote:

#!/sbin/ksh

typeset -i a
typeset -i b
typeset -i c
typeset -i d
typeset -i e
typeset -i f
typeset -i g
typeset -i h

((a=159))
((b=256))
((c=256*256))
((d=256*256*256))
((e=$a*$b))
((f=$a*$c))
((g=$a*$d))
((h=$a*$b*$c))

echo "a = $a"
echo "b = $b"
echo "c = $c"
echo "d = $d"
echo "e = $e"
echo "f = $f"
echo "g = $g"
echo "h = $h"

So when I run the script, here are my results:

a = 159
b = 256
c = 65536
d = 16777216
e = 40704
f = 10420224
g = -1627389952
h = -1627389952

Now, I am a pretty smart guy, so I know that multiplying or adding 2 positive numbers is supposed to give you a positive number in return. Why am I getting negative values for g and h? I was told it may have something to do with using 16-bit number representations or something along those lines. Any ideas out there? Thanks for the help!

Chris Ivey

HP OpenView Certified Consultant - NNM 6.x on Unix (0010-01-42093)
MTS - Systems Engineer
Verizon Data Services, ENSS-West WAN Tools Group
Office: (813) 978-4844
Pager: (813) 303-1177
ICQ: 25717924
AIM: IveyAtGTEDS
3 REPLIES 3
Uffe Gavnholt_1
Honored Contributor

Re: KSH Script Questikon

Hi Chris,

You prette much came with your answer yourself.

When using typeset -i you create a signed integer of 32 bit, giving it a range from:

-2147483648 to 2147483648

My numbers are not 100% accurate, but what happends is that if you have an integer of the size 2147483648, adding one, will turn on the last bit in the integer, giving a signed value of:
-2147483648

adding one to this will increase this number by one etc.

So it's simply an integer overflow you are experiencing :)

Good luck

Uffe
Frederic Soriano
Honored Contributor
Solution

Re: KSH Script Questikon

Hi Chris,

Negative output is normal, considering the fact that your variables are 32-bit signed integers (i.e. -2^31 <= integer value <= 2^31 - 1). So when your operation is giving a positive result greater than 2147483647 (2^31 - 1), output will be negative.

You can see this behaviour in ksh with the following commands:

--snip--
> typeset -i a
> a=2147483647
> echo $a
2147483647
> ((a=a+1))
> echo $a
-2147483648
--snip--

If you want positive results instead, you could e.g. use the following workaround:

--snip--
> a=2147483647
> echo "scale=2;$a+1" | bc
2147483648
--snip--

I hope this helps.

Best regards,

Fred.
Joseph A Benaiah_1
Regular Advisor

Re: KSH Script Questikon

Chris,

This is quite normal. When doing arithmetic in a shell, I prefer to pass the arguments to an awk statement, e.g.

echo 159 256 |
awk '{ a = $1
b = $2
c = $2 * $2
d = $2 * $2 * $2
e = a * b
f = a * c
g = a * d
h = a * b * c
print a
print b
print c
print d
print e
print f
printf("%ld\n", g)
printf("%ld\n", h) }'

This should give you the correct results.

Cheers,

Joseph.