You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Floating Point Number section of Fortran Best Practices could be extended with 2 subsections:
comparison of two real numbers;
undesirability of using real as iterator within do loops (it was deprecated in Fortran95 standard).
The motivation.
It's common problem for all programming languages when beginner programmer tries to compare fooating points numbers especially using if statement. It would be usefull to demonstrate more accurate approaches (with use of intrinsic procedures?) of comparing numbers to avoid accuracy and computational issues.
The second problem comes from first one and could results in computational issues.
The text was updated successfully, but these errors were encountered:
band-a-prend
changed the title
Fortran Best Practices: Floating Point Numbers section extension
Fortran Best Practices: Floating Point Numbers comparison
Nov 21, 2022
real(dp) :: tol
tol = 10 * epsilon(1.0_dp)
real(dp), parameter :: tol = 1.0e-10_dp
if (abs(x - y) < tol) then
print *, "Effectively Equal"
else
print *, "Not Equal"
end if
Floating-point numbers like 0.1 cannot be represented exactly in binary because their binary form is infinite and repeating. Due to the limited precision of floating-point formats (like 32-bit or 64-bit), these numbers are rounded off to fit within the constrained number of bits. This rounding can lead to small errors in representation and calculations. so it is better to add tolerance and check whether the numbers are close enough.
for second
Use integer iterators and convert them to floating point within the loop:
integer :: i
real(dp) :: step, value
step = 0.1_dp
do i = 0, 10
value = i * step
print *, value
end do
The Floating Point Number section of Fortran Best Practices could be extended with 2 subsections:
real
numbers;real
as iterator withindo
loops (it was deprecated in Fortran95 standard).The motivation.
It's common problem for all programming languages when beginner programmer tries to compare fooating points numbers especially using
if
statement. It would be usefull to demonstrate more accurate approaches (with use of intrinsic procedures?) of comparing numbers to avoid accuracy and computational issues.The second problem comes from first one and could results in computational issues.
The text was updated successfully, but these errors were encountered: