Why Doesn't Fortran Recognize Small Numbers?

In summary, the conversation discusses a problem with a Fortran code where the compiler is displaying incorrect results when using a small coefficient. The issue may be due to using a low precision data type or mismatched types. The solution is to use a double precision statement. The conversation also mentions the possibility of redefining a function as the program runs.
  • #1
womfalcs3
62
5
We're not talking very small. Is it the compiler? I'm using gfortran.

An example would be:

f(y)=1E-5*(y**3)

For f(40), that should be 0.64. When I compile and execute, however, it displays 0's.

If I make the coefficient 1, the result does come out to be correct.


I have an old Fortran IV (I'm using F77) book that says for that version of the code, I can't do 1.E-76.

This is well closer to 1 than that. They even used an example where the power of 10 was -21. So is this right? Is it a problem with the compiler?
 
Technology news on Phys.org
  • #2
It sounds like your problem is one of the following things:
You are using too low a precision data type, perhaps real (4 bytes) and not double...
Your types are mismatched and it's demoting types...
There's some other problem.

I think you'd get more feedback with more context.
 
  • #3
Ah double precision statements. I didn't think of that. I was trying to use periods, which weren't working, but I've seen people use them for that purpose.

Thank you. I read up on double precision statements and it was simple enough to incorporate.

That worked.
 
  • #4
Rather than make a new thread, I figured I'd ask here.

Is there anyway to define functions after the input type statements?

For example, f(x)=sin(x). I want to redefine f(x) as I go along the program.

Thanks.
 

Related to Why Doesn't Fortran Recognize Small Numbers?

1. Why does Fortran have trouble recognizing small numbers?

Fortran is a programming language that was originally designed for scientific and engineering applications, where large numbers are more commonly used. As a result, it is optimized for working with large numbers and may have difficulty accurately representing and manipulating very small numbers.

2. How does Fortran handle small numbers?

Fortran uses a fixed-precision system, where the number of digits used to represent a number is predetermined. This means that very small numbers may be rounded or truncated, leading to potential loss of accuracy. Additionally, Fortran may also use different data types for representing numbers, which can also affect its ability to handle small numbers.

3. Can Fortran be used for calculations involving small numbers?

Yes, Fortran can still be used for calculations involving small numbers. However, it is important to be aware of its limitations and potential for loss of accuracy. It is also recommended to use appropriate data types and precision settings to ensure accurate representation and manipulation of small numbers.

4. Are there any alternative programming languages that are better suited for working with small numbers?

Yes, there are other programming languages that are specifically designed for working with small numbers, such as Python and MATLAB. These languages have built-in support for arbitrary precision arithmetic, which allows for more accurate representation and manipulation of small numbers.

5. How can I improve Fortran's handling of small numbers?

To improve Fortran's handling of small numbers, you can try using appropriate data types and precision settings, as well as avoiding operations that may lead to loss of accuracy (such as multiplying or dividing very small numbers). Additionally, there are also libraries and modules available that can extend Fortran's capabilities for working with small numbers.

Similar threads

  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
20
Views
3K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
8
Views
5K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
8
Views
7K
Back
Top