Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Small numbers in Fortran 95

  1. Jul 5, 2012 #1
    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
     

    Attached Files:

  2. jcsd
  3. Jul 5, 2012 #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.
     
  4. Jul 5, 2012 #3

    Mark44

    Staff: Mentor

    Or instead of using real as the data type, change to double precision.
     
  5. Jul 5, 2012 #4

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    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!
     
  6. Jul 5, 2012 #5

    jedishrfu

    Staff: Mentor

    most modern compilers bump reals to doubles when required and warn when you're going in the opposite direction.
     
  7. Jul 5, 2012 #6

    chiro

    User Avatar
    Science Advisor

    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.
     
  8. Jul 6, 2012 #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....
     
  9. Jul 6, 2012 #8

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    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..
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Small numbers in Fortran 95
  1. Fortran 95 (Replies: 12)

Loading...