Fortran Weird fortran problem (Assigned variable value doesn't match up with output)

AI Thread Summary
The discussion revolves around a Fortran coding issue where the calculated value of Pi does not match the assigned value due to type mismatches. The user initially attempted to use 2*accos(0.0) to derive Pi but encountered precision errors. It was identified that the problem stemmed from using different types for the variable and the assigned value, which led to inaccuracies. The solution involved declaring the variable with a specific kind and ensuring that constants are assigned with the correct type suffix. Ultimately, the user resolved the issue by understanding the importance of matching variable types for accurate numerical representation.
Cbuha
Messages
2
Reaction score
0
I ran into a kind of strange thing today while coding. I needed Pi in a program, so I used 2*accos(0.0) to pull it out, but my answers were slightly off. I checked the Pi calculated and it was off after a few decimal places.

"Odd", I thought, and just decided to put in Pi as an assigned number. However, this still didn't solve the problem! No matter how I inputted it, it was being altered somehow by the program. I whipped up a really quick program to demonstrate what I'm talking about.
program probs
real(kind=8)::v

v=3.141592653589793238462643
write(*,*)v
end

if you check the output( assuming it's not just my machine or compiler for some reason) you'll see the values for v and the number inputted for it are not the same.

I'm guessing it has something to do with using the kind flag, but I'm not much of a comp sci person, and my searches didn't turn up anything useful. Anyone have any clue how to fix this? If it is related to kind, how can I keep high precision while avoiding this problem?
 
Technology news on Phys.org
The reason being the value you are assigning is one type and the variable on the left is a different type...if you are going to be declaring variables of different types that the default, you need to assign value of different type than the default; in fact, they should be declared of the exact same type...follow? maybe an example:

Code:
program probs
integer, parameter :: rrr=SELECTED_REAL_KIND(15,307)
real(KIND=rrr) :: v

v=3.141592653589793238462643_rrr
write(*,*)v
end

Here, we first declare a "type"...that's the integer variable rrr.
Then, we declare the variable v of that kind
Then, in order to assign a literal constant to that variable with suffix it with the same "type"...with '_rrr'

Now, test this program and you will see.
 
Or, the "traditional" way is to make sure your constants are double precision.
2*accos(0.0d0)
v=3.141592653589793238462643d0

accos(0.0) will use the single precision version of the function because its argument is single precision, so the result will only be accurate to about 5 decimal places. acos(0.0d0) should be accurate to about 15 places.
 
Thanks guys! I was just kind of confused because the output of the variables of real(kind=8) goes to the correct amount of decimal places. I never really thought it through to declare my assigned values as some type, I just assumed it would assign whatever value I placed there the correct type. It's working now tho
 
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 have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
59
Views
11K
Replies
3
Views
2K
Replies
4
Views
2K
Replies
13
Views
3K
Replies
11
Views
2K
Replies
6
Views
3K
Replies
4
Views
1K
Back
Top