What is an integer? (168 Views)
Posts: 560
Registered: ‎06-06-2003
Message 1 of 7 (168 Views)

# What is an integer?

Hi all,

can anyone tell me the true definition of an integer? My understanding is that is a whole number derived from a mathematical sum - info I found of the web:

"An integer is what is more commonly known as a whole number. It may be positive, negative, or the number zero, but it must be whole. In some cases the definition of whole number will exclude the number zero, or even the set of negative numbers, but this is not as common as the more inclusive use of the term. Integers are the numbers people are most familiar with, and they serve a crucial role in virtually all mathematics."

The reason I ask this question is because I want to know what is the correct syntax when using a test statement ie:

is set MyVar=0 and to test this I would:

if [ \${MyVar} = "0" ]; the
true
etc etc

however if I created a sum or a count:

count=0

while [ \${count} -lt 10 ]; do
etc etc
(( count =+ ))

I understand that using "= != < >" is for testing a string so substituting MyVar=0 does "0" become a string.

Thanks

Chris
hello
Honored Contributor
Posts: 1,007
Registered: ‎03-15-2009
Message 2 of 7 (168 Views)

# Re: What is an integer?

simply put an integer is a whole number, including zero. are you asking it in relation to a shell? what shell do you use?

if you have difficulties with expressions, consult the man page:

# man test

or

# man ksh

can also help you, search for the keywords "while", "for" or "if". If you are using other shell look at its man page.
****
Unix operates with beer.
Honored Contributor
Posts: 1,007
Registered: ‎03-15-2009
Message 3 of 7 (168 Views)

# Re: What is an integer?

> substituting MyVar=0 does "0" become a string.

that's because the shell is auto-converting the value.
****
Unix operates with beer.
Acclaimed Contributor
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 7 (168 Views)

# Re: What is an integer?

Hi Chris:

> I understand that using "= != < >" is for testing a string so substituting MyVar=0 does "0" become a string.

Yes, I believe so.

In the shell, you can declare an integer (as opposed to a string) with the 'typeset -i' declaration. This speeds up arithmetic operations.

In 'awk' programs you will sometimes see someone do:

# awk '{\$1+0 ...

...which prevents you from doing silly arithmetic with non-numeric data. Compare these results:

# echo FF | awk '{SUM=(\$0+0);print SUM}'

# echo FF | awk '{SUM=(\$0);print SUM}'

Regards!

...JRF...
Posts: 560
Registered: ‎06-06-2003
Message 5 of 7 (168 Views)

# Re: What is an integer?

Thanks both for the quick response

As I thought but wanted the experts to confirm

8-)

root:/scratch #echo FF | awk '{SUM=\$0+0);print SUM}'
0
root:/scratch #echo FF | awk '{SUM=(\$0);print SUM}'
FF
hello
Acclaimed Contributor
Posts: 26,213
Registered: ‎03-06-2006
Message 6 of 7 (168 Views)

# Re: What is an integer?

>can anyone tell me the true definition of an integer?

In the shell, an integer is something that's not a string. (And not a floating point number for shells that support those types.)

>if [ \${MyVar} = "0" ]; then

This is a string comparison. If you know MyVar is an integer or numeric you should do:
if [ \$MyVar -eq 0 ]; then
Or even:
if (( Myvar == 0 )); then

>while [ \${count} -lt 10 ]; do

Or:
while (( count < 10 )); do
Honored Contributor
Posts: 1,085
Registered: ‎11-06-2003
Message 7 of 7 (168 Views)

# Re: What is an integer?

use typeset

typeset -i MyVar=0

if (( MyVar = 0 )); then
true
etc etc

typeset -i count=0

while (( count < 10 )); do
etc etc
(( count =+ ))
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.