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

Discussion Overview

The discussion centers around handling small numerical values in Fortran 95, particularly in the context of calculating the tangent of an angle using sine and cosine functions. Participants explore issues related to precision and the potential for division by zero when cosine values approach very small magnitudes.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes a problem with calculating tan(theta) when theta is 90 degrees, noting that the cosine value becomes very small (e.g., 4.37E-8), leading to division by zero issues.
  • Another participant suggests that the issue may stem from the variable type used, proposing that using a higher precision type (e.g., double precision) could resolve the problem.
  • It is mentioned that constants in the code should also be defined in double precision to avoid losing significant figures, as single precision may not capture the necessary accuracy.
  • A participant shares an approach used in game engine design, recommending a routine to set values below a certain threshold to zero to handle small numbers effectively.
  • One participant expresses concern about modern compilers automatically converting real numbers to double precision, arguing that this could lead to unintended consequences and that programmers should have control over precision types.
  • A participant notes that their reference book has not yet covered double precision, indicating a potential gap in their understanding of the topic.

Areas of Agreement / Disagreement

Participants express differing views on the handling of precision in Fortran, with some advocating for the use of double precision while others raise concerns about compiler behavior and the implications of automatic type conversion. The discussion remains unresolved regarding the best approach to manage small numbers in this context.

Contextual Notes

There are limitations regarding the understanding of double precision in the context of the referenced textbook, which has not yet introduced the concept. Additionally, there is uncertainty about the behavior of different compilers in relation to precision handling.

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: 770
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
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 2 ·
Replies
2
Views
8K