1748171 Members
4293 Online
108758 Solutions
New Discussion юеВ

Re: What is an integer?

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

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
6 REPLIES 6
Viktor Balogh
Honored Contributor

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.
Viktor Balogh
Honored Contributor

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.
James R. Ferguson
Acclaimed Contributor
Solution

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...
lawrenzo_1
Super Advisor

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
Dennis Handly
Acclaimed Contributor

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
Laurent Menase
Honored Contributor

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 =+ ))