Fortran - Having to add integer command

In summary: However, it seems like the integer value for sqrt(i) is not needed in the second program. Can you explain why this is the case?
  • #1
Nugso
Gold Member
170
10
Hello everyone. Today I learned how to write a program which calculates and prints integer squares roots from 1 to 1000. Here is what I wrote:
Code:
program example
real::i
do i=1,1000
if(sqrt(i)**2==i) then
print*,i
end if
end do
end program example

After compling this, 25, 49 etcs can't be seen. It lists like;
1.0000
4.0000
9.0000

and so on. However, if I write it like this;

Code:
program example
real::i
integer::physicsforum
do i=1,1000
physicsforum=sqrt(i)
if(physicsforum**2=i)then
print*,i
end if
end do
end program example

then I do see 25.0000, 49.0000 etc too. What is confusing me is why do I have to add integer for sqrt(i) to see 25.0000, 49.0000?

Thanks.
 
Technology news on Phys.org
  • #2
You should never compare 'real' numbers for exact equality, because arithmetic inevitably introduces small errors because of the finite number of binary bits used. Your first program probably compares 25.00000 with 25.00001 or 24.99999 or something like that.

When you need to do a comparision like this, you must allow for small deviations from equality by doing something like this:

if (abs(a - b) < epsilon)

where 'epsilon' is a suitably small number.

Or you can try to set up the comparision so that it uses integers instead, like you did in the second program. Here you need to be careful because when you assign a real value to an integer variable, it is truncated to the nearest lower integer, not rounded (which could go either up or down). So 24.99999 would become 24, not 25.
 
  • #3
By assigning it to an integer you've thrown away the small decimal remainder that is common to floating pt numbers.

This is why programmers never do equals testing like you did but instead do this

if ( abs(sqrt(i)**2 - i) < 0.000001) ...

to determine if they are equal to within some small range
 
  • #5
because they have some stray bits that the other values don't have.

you'd need to print the values out with maximal decimal formatting to see what the stray bits may be.

For example you might print 1.000,000 but the number is really 1.000,000,001 (commas added for clarity) in memory.

There are also even smaller guard bits that aren't shown normally but can be teased out be multiplying the number up a few orders of magnitude up.

see wikipedia for something on guard bits:

http://en.wikipedia.org/wiki/Guard_digit
 
  • #6
jedishrfu said:
because they have some stray bits that the other values don't have.

you'd need to print the values out with maximal decimal formatting to see what the stray bits may be.

For example you might print 1.000,000 but the number is really 1.000,000,001 (commas added for clarity) in memory.

There are also even smaller guard bits that aren't shown normally but can be teased out be multiplying the number up a few orders of magnitude up.

see wikipedia for something on guard bits:

http://en.wikipedia.org/wiki/Guard_digit

Thank you very much sir.
 

Related to Fortran - Having to add integer command

1. What is the purpose of the integer command in Fortran?

The integer command in Fortran is used to declare variables as integers, which are whole numbers without decimal points. This allows the computer to allocate memory more efficiently and perform calculations more accurately.

2. How do I add an integer command to my Fortran program?

To add an integer command in Fortran, you can use the "integer" keyword followed by the variable name and an optional initialization value. For example: "integer :: num = 5" would declare the variable "num" as an integer with an initial value of 5.

3. Can I use the integer command to declare multiple variables at once?

Yes, you can use the integer command to declare multiple variables at once by separating each variable name with a comma. For example: "integer :: num1, num2, num3" would declare three integer variables named "num1", "num2", and "num3".

4. Is the integer command necessary for all variables in a Fortran program?

No, the integer command is only necessary for variables that you want to be declared as integers. If you do not use the integer command, Fortran will assume that the variable is a real number, which allows for decimal points.

5. Can I change the value of an integer variable after declaring it?

Yes, you can change the value of an integer variable after declaring it by assigning a new value to the variable. However, the new value must also be an integer, otherwise it will result in an error.

Similar threads

  • Programming and Computer Science
Replies
4
Views
759
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
8
Views
2K
Back
Top