How to deal with small numbers in Fortran 95?

  • Context: Fortran 
  • Thread starter Thread starter issacnewton
  • Start date Start date
  • Tags Tags
    Fortran Numbers
Click For Summary
SUMMARY

The discussion focuses on handling small numerical values in Fortran 95 when calculating the tangent of an angle using the gfortran compiler. Users encountered issues with cosine values approaching 4.37E-8, leading to incorrect tangent calculations. The solution involves using double precision data types instead of single precision and ensuring that constants are also defined in double precision to maintain accuracy. Participants emphasized the importance of avoiding mixed precision in calculations to prevent unexpected behavior.

PREREQUISITES
  • Understanding of Fortran 95 syntax and programming concepts
  • Familiarity with gfortran compiler usage
  • Knowledge of numerical precision and data types in programming
  • Basic understanding of trigonometric functions and their implementations
NEXT STEPS
  • Learn about Fortran 95 double precision data types and their implementation
  • Research the use of KIND parameter in Fortran for defining variable precision
  • Explore best practices for managing numerical precision in scientific computing
  • Investigate common pitfalls in mixing single and double precision in Fortran
USEFUL FOR

This discussion is beneficial for Fortran developers, particularly those working on scientific computing or numerical analysis, as well as students learning about precision issues in programming.

issacnewton
Messages
1,035
Reaction score
37
Hi

I am doing a program using gfortran compiler. I feed angle theta in degrees to the program. Program is supposed to calculate the value of tan(theta) using values of sine and cosine of theta. the program should give warning of division-by-zero if magnitude of cosine theta is less than 10-20. I have attached the code in jpg file...

When I feed theta=90, the cosine value takes values like 4.37E-8 , so tan theta is always calculated...how can we rectify this ?

thanks
 

Attachments

  • program.jpg
    program.jpg
    21.4 KB · Views: 760
Technology news on Phys.org
I have never had those kinds of issues since the numbers I deal with are mostly reasonable...but your problem sounds like the type of variable that you are using does not even have the resolution to get that small...any lower than 4e-08 and it may go negative, I guess...so, you need to read up on KIND, I have never used it, but what I remember reading, you should be able to choose the accuracy you want your variables be able to handle.
 
Or instead of using real as the data type, change to double precision.
 
You also need to change your constants to double precision. 3.14159... is a single precision number so the compiler will effectively ignore everything after about 6 significant figures. 3.14159...d0 is double precision (about 16 significant figures).

Actually, if you just changed those "pis" to double precision the code would probably do what you expect, but mixing up single and double precision is usually a bad idea in "real" programs, so learn how to do this example the right way!
 
most modern compilers bump reals to doubles when required and warn when you're going in the opposite direction.
 
IssacNewton said:
Hi

I am doing a program using gfortran compiler. I feed angle theta in degrees to the program. Program is supposed to calculate the value of tan(theta) using values of sine and cosine of theta. the program should give warning of division-by-zero if magnitude of cosine theta is less than 10-20. I have attached the code in jpg file...

When I feed theta=90, the cosine value takes values like 4.37E-8 , so tan theta is always calculated...how can we rectify this ?

thanks

Hey IssacNewton and welcome to the forums.

This kind of thing occurs in computer game engine design and the simple way to deal with this is to basically use a higher-precision data type and then use a routine to check if something is less than a fixed value to zero and if it is, to set it to zero.

So as an example, your threshold is 0.000005d. Then if your value is less than this number, the number becomes zero.

You can do this kind of thing for values in general, but this is a common way that it is handled in things like game engines.
 
ok thanks everyone. I may have to use double precision variables I guess... But the book I am using (Fortran 95/2003 for scientists and engineers- Chapman 3ed.) has not started talking about double precision at this point (chapter 3). And this is a problem from chapter 3...
 
jedishrfu said:
most modern compilers bump reals to doubles when required and warn when you're going in the opposite direction.

If you have a compiler that thinks it knows better that you what your program means, IMO throw it away and get one that follows the language standards!

What you suggest is a very bad idea. How is the compiler supposed to guess whether a cosntant like 1073741824.0 is supposed to be single or double precision? It happens to be a value that is represented exactly in single precision, 230. But if I use that constant in an arithmetic expresssion, the results may be different depending on whether it is treated as single or double precision, and I don't want some "clever" compiler thinking it knows better than me what I meant.

Maybe you are thinking about other languages (e.g. C) where real constants are double precision by default, but Fortran isn't C..
 

Similar threads

  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
8K
  • · Replies 59 ·
2
Replies
59
Views
11K
  • · Replies 8 ·
Replies
8
Views
4K