FORTRAN 77 input absolute values

Click For Summary
SUMMARY

This discussion focuses on handling absolute values and precision issues in FORTRAN 77, specifically when reading floating-point numbers. The user encountered an issue where inputting 0.01 resulted in an output of 0.00999999978 due to the limitations of real*4 precision. To achieve better precision, the recommendation is to use real*8. Additionally, the user faced a problem calculating the number of intervals (n) for Simpson's 1/3 rule, which returned 9 instead of the expected 10. The NINT() function was identified as a solution to correctly compute n.

PREREQUISITES
  • Understanding of FORTRAN 77 syntax and data types
  • Familiarity with floating-point representation and precision issues
  • Knowledge of numerical methods, specifically Simpson's 1/3 rule
  • Experience with FORTRAN intrinsic functions like NINT()
NEXT STEPS
  • Research the differences between real*4 and real*8 in FORTRAN 77
  • Explore the use of NINT() and other rounding functions in FORTRAN
  • Study numerical methods for integration, focusing on Simpson's 1/3 rule
  • Learn about floating-point arithmetic and its implications in programming
USEFUL FOR

This discussion is beneficial for FORTRAN developers, numerical analysts, and anyone working with precision in floating-point calculations, particularly in scientific computing and numerical methods.

tyogav
Messages
14
Reaction score
0
How to input absolute values in FORTRAN77?

This was the code I used
READ *,H
PRINT *,H

The input I gave was 0.01
But the output I got was 0.00999999978.
 
Technology news on Phys.org
The decimal 0.01 can't be written as an exact floating point binary. The result you got is the closest you can get with a real*4. If you need more precision, try real*8.
 
I m writing a FORTRAN program for Simpson's 1/3 rule. In that I m taking three inputs, upper limit and lower limit of the integral and the interval.

For eg: lower limit is 0, upper limit is 1, interval(h) is 0.1. Naturally the no. of intervals(n) is 10. But it shows 9.

This is the code
Read *,a,b,h
n=(b-a)/h

It seems that it is because of the fact that n is just the integer part of the answer (which according to FORTRAN is 9.999...etc) Any way to solve it?

I tried nint.. It worked. I m looking for other options :confused:
 
The NINT() function is exactly the way I would get the correct value of N in this situation. I can't think of any other reasonable way to do it.
 
The other possibility is to have n instead of h as a parameter.
 

Similar threads

Replies
64
Views
10K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 2 ·
Replies
2
Views
1K
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 29 ·
Replies
29
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K