- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: KSH Script Questikon
Operating System - HP-UX
1753759
Members
5032
Online
108799
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-04-2001 07:29 AM
тАО04-04-2001 07:29 AM
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
#!/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
Solved! Go to Solution.
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-04-2001 07:56 AM
тАО04-04-2001 07:56 AM
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-04-2001 08:07 AM
тАО04-04-2001 08:07 AM
Solution
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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-08-2001 03:53 PM
тАО04-08-2001 03:53 PM
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.
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.
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
News and Events
Support
© Copyright 2024 Hewlett Packard Enterprise Development LP