Fortran Fortran - Having to add integer command

  • Thread starter Thread starter Nugso
  • Start date Start date
  • Tags Tags
    Fortran Integer
AI Thread Summary
The discussion revolves around a programming issue related to calculating and printing integer square roots from 1 to 1000. The initial program fails to display certain perfect squares, such as 25 and 49, due to the comparison of real numbers for exact equality, which can lead to inaccuracies because of floating-point arithmetic errors. The solution involves using an integer variable to store the square root, which avoids these precision issues. It is emphasized that comparing real numbers directly can result in missed values due to small discrepancies, and a better approach is to use a tolerance level for comparisons, such as checking if the absolute difference is less than a small epsilon value. Additionally, the concept of guard bits in floating-point representation is introduced, explaining how they can affect the displayed values and suggesting that printing with maximal decimal formatting can reveal these discrepancies.
Nugso
Gold Member
Messages
170
Reaction score
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
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.
 
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
 
Last edited:
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
 
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
12
Views
1K
Replies
4
Views
2K
Replies
19
Views
2K
Replies
13
Views
3K
Replies
12
Views
3K
Replies
5
Views
5K
Back
Top