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

In summary, the programmer ran into a problem where their values for a real(kind=8) variable were not the same as what was inputted into the program. They suspect it has something to do with the kind flag, but are not sure how to fix the problem.
  • #1
Cbuha
2
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
  • #2
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.
 
  • #3
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.
 
  • #4
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
 
  • #5


I can understand your frustration with this problem. It seems like the issue may be related to the kind flag, which determines the precision of the variable. In this case, using the kind flag may have caused the variable to be rounded or truncated, resulting in the slightly off values.

To fix this, you can try using the "precision" option instead of the "kind" flag when declaring the variable. This will allow you to specify the number of digits you want to use for the variable, ensuring that you have the desired precision.

Additionally, it may be helpful to check the documentation for your compiler or programming language to see if there are any specific recommendations for working with high precision values. You may also want to try different approaches for calculating Pi, such as using a pre-defined constant or a built-in function, to see if that makes a difference.

In any case, it is always important to thoroughly test and troubleshoot your code to ensure that the results are accurate and reliable. I hope you are able to find a solution to this problem.
 

What is fortran?

Fortran is a high-level programming language used primarily for scientific and mathematical computing. It was one of the first programming languages and is still widely used today.

What is a variable in fortran?

A variable in fortran is a named storage location that holds a value or data. It can be assigned different values throughout the program and is used to store and manipulate data.

Why might the assigned variable value not match up with the output?

This could be due to a variety of reasons, such as errors in the code, incorrect use of data types, or unexpected interactions between different parts of the program. It is important to carefully check the code and debug any potential issues.

How can I troubleshoot this weird fortran problem?

The first step would be to carefully review the code and check for any errors or potential issues. You can also use debugging tools to track the flow of the program and identify any unexpected behavior. It may also be helpful to consult with other programmers or seek assistance from the fortran community.

Is this a common problem in fortran programming?

Yes, encountering unexpected behavior or errors in programming is a common issue in any language, including fortran. It is important to carefully review and test the code, and seek assistance if needed, to resolve any problems.

Similar threads

  • Programming and Computer Science
Replies
4
Views
615
  • Programming and Computer Science
2
Replies
59
Views
9K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
932
  • Programming and Computer Science
Replies
4
Views
336
Back
Top