Fortran FORTRAN 77 input absolute values

Click For Summary
In FORTRAN 77, inputting absolute values can lead to precision issues, as floating-point numbers like 0.01 cannot be represented exactly in binary, resulting in outputs like 0.00999999978. To achieve greater precision, using real*8 instead of real*4 is recommended. When calculating the number of intervals for Simpson's 1/3 rule, the integer division can yield unexpected results, such as showing 9 instead of 10 due to truncation. The NINT() function effectively resolves this issue by rounding to the nearest integer. Alternative solutions may involve adjusting parameters, but NINT() is the most straightforward approach.
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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

Replies
64
Views
9K
  • · 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
1K