How to deal with small numbers in Fortran 95?

In summary, using a higher precision data type will solve your problem, but you should be aware that some compilers may change the value of constants to double precision when they are used.
  • #1
issacnewton
1,026
36
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: 699
Technology news on Phys.org
  • #2
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.
 
  • #3
Or instead of using real as the data type, change to double precision.
 
  • #4
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!
 
  • #5
most modern compilers bump reals to doubles when required and warn when you're going in the opposite direction.
 
  • #6
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.
 
  • #7
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...
 
  • #8
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..
 

Related to How to deal with small numbers in Fortran 95?

1. What are small numbers in Fortran 95?

Small numbers in Fortran 95 refer to real numbers that are very close to zero. These numbers are typically used in scientific and mathematical calculations where precision is important.

2. How are small numbers represented in Fortran 95?

In Fortran 95, small numbers are represented using single precision or double precision floating-point formats. These formats allow for numbers to be stored with a high degree of precision and a wide range of values, including very small numbers.

3. What is the difference between single and double precision in Fortran 95?

The main difference between single and double precision in Fortran 95 is the amount of memory used to store a number. Single precision numbers use 32 bits of memory, while double precision numbers use 64 bits. This means that double precision numbers can represent a wider range of values and have a higher degree of precision compared to single precision numbers.

4. How do I declare and use small numbers in Fortran 95?

To declare a small number in Fortran 95, you can use the real data type and specify the desired precision (either single or double). For example, to declare a single precision small number, you would use "real(4) :: small_num". To use small numbers in calculations, you can simply include them in your equations and Fortran 95 will handle the precision automatically.

5. What are some common errors to watch out for when dealing with small numbers in Fortran 95?

One common error to watch out for when dealing with small numbers in Fortran 95 is underflow, which occurs when a number is too small to be represented accurately and is instead rounded to zero. This can lead to incorrect results in calculations. Another potential error is overflow, which occurs when a number is too large to be represented and is instead rounded to infinity. It is important to monitor for these errors and adjust the precision or use special functions to handle them appropriately.

Similar threads

  • Programming and Computer Science
Replies
25
Views
992
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
2
Replies
59
Views
9K
  • Programming and Computer Science
Replies
2
Views
7K
Back
Top