FORTRAN 77 input absolute values

Click For Summary

Discussion Overview

The discussion revolves around inputting absolute values in FORTRAN 77 and handling floating-point precision issues. It includes technical explanations related to floating-point representation and specific programming challenges encountered while implementing numerical methods like Simpson's 1/3 rule.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant notes that the decimal 0.01 cannot be represented exactly in floating-point binary, leading to an output of 0.00999999978 when using real*4.
  • Another participant suggests using real*8 for increased precision in floating-point representation.
  • A participant describes a problem in calculating the number of intervals (n) in Simpson's 1/3 rule, where the calculation yields 9 instead of 10 due to integer truncation.
  • One participant confirms that using the NINT() function correctly resolves the issue of obtaining the integer value of n.
  • Another participant proposes considering n as a parameter instead of h as an alternative approach.

Areas of Agreement / Disagreement

Participants generally agree on the use of the NINT() function to address the integer calculation issue, but there are multiple perspectives on handling floating-point precision and alternative solutions for the interval calculation.

Contextual Notes

The discussion highlights limitations related to floating-point representation and integer truncation in FORTRAN, as well as the dependency on specific data types for precision.

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
11K
  • · Replies 25 ·
Replies
25
Views
4K
  • · 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
4K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K