Testing Scientific Code
Tolerance Check
Compare Reals Safely
Scientific tests usually compare real values with a tolerance instead of exact equality.
Program
Play the program to tighten or loosen the tolerance around the same measured value.
tolerance_check.f90
Replay: real traced execution (multi-file project)
program tolerance_check_demo
implicit none
real :: expected
real :: measured
real :: tolerance
real :: error
logical :: passed
character(len=5) :: status
expected = 10.0
measured = 9.98
tolerance = 0.05
error = abs(measured - expected)
passed = error <= tolerance
if (passed) then
status = 'pass'
else
status = 'fail'
end if
print '(A, 1X, F0.3)', trim(status), error
end program tolerance_check_demo
program tolerance_check_demo
implicit none
real :: expected
real :: measured
real :: tolerance
real :: error
logical :: passed
character(len=5) :: status
expected = 10.0
measured = 9.98
tolerance = 0.01
error = abs(measured - expected)
passed = error <= tolerance
if (passed) then
status = 'pass'
else
status = 'fail'
end if
print '(A, 1X, F0.3)', trim(status), error
end program tolerance_check_demo
program tolerance_check_demo
implicit none
real :: expected
real :: measured
real :: tolerance
real :: error
logical :: passed
character(len=5) :: status
expected = 10.0
measured = 9.98
tolerance = 0.10
error = abs(measured - expected)
passed = error <= tolerance
if (passed) then
status = 'pass'
else
status = 'fail'
end if
print '(A, 1X, F0.3)', trim(status), error
end program tolerance_check_demo
expected ← 10.000
10expected = 10.011measured = 9.98values this step10.000expectedmeasured ← 9.980
10expected = 10.011measured = 9.9812tolerance = 0.05values this step9.980measuredtolerance ← 0.050
11measured = 9.9812tolerance = 0.0513error = abs(measured - expected)values this step0.050toleranceerror ← 0.020
12tolerance = 0.0513error = abs(measured - expected)14passed = error <= tolerancevalues this step0.020error9.980measured10.000expectedpassed ← .true.
13error = abs(measured - expected)14passed = error <= tolerance15if (passed) thenvalues this step.true.passed0.020error0.050tolerancestatus ← pass
15if (passed) then16 status = 'pass'17elsevalues this steppassstatusprint '(A, 1X, F0.3)', trim(status), error
19 end if20 print '(A, 1X, F0.3)', trim(status), error21end program tolerance_check_demooutputpass 0.020values this steppassstatus0.020error
expected ← 10.000
10expected = 10.011measured = 9.98values this step10.000expectedmeasured ← 9.980
10expected = 10.011measured = 9.9812tolerance = 0.01values this step9.980measuredtolerance ← 0.010
11measured = 9.9812tolerance = 0.0113error = abs(measured - expected)values this step0.010toleranceerror ← 0.020
12tolerance = 0.0113error = abs(measured - expected)14passed = error <= tolerancevalues this step0.020error9.980measured10.000expectedpassed ← .false.
13error = abs(measured - expected)14passed = error <= tolerance15if (passed) thenvalues this step.false.passed0.020error0.010tolerancestatus ← fail
17else18 status = 'fail'19end ifvalues this stepfailstatusprint '(A, 1X, F0.3)', trim(status), error
19 end if20 print '(A, 1X, F0.3)', trim(status), error21end program tolerance_check_demooutputfail 0.020values this stepfailstatus0.020error
expected ← 10.000
10expected = 10.011measured = 9.98values this step10.000expectedmeasured ← 9.980
10expected = 10.011measured = 9.9812tolerance = 0.10values this step9.980measuredtolerance ← 0.100
11measured = 9.9812tolerance = 0.1013error = abs(measured - expected)values this step0.100toleranceerror ← 0.020
12tolerance = 0.1013error = abs(measured - expected)14passed = error <= tolerancevalues this step0.020error9.980measured10.000expectedpassed ← .true.
13error = abs(measured - expected)14passed = error <= tolerance15if (passed) thenvalues this step.true.passed0.020error0.100tolerancestatus ← pass
15if (passed) then16 status = 'pass'17elsevalues this steppassstatusprint '(A, 1X, F0.3)', trim(status), error
19 end if20 print '(A, 1X, F0.3)', trim(status), error21end program tolerance_check_demooutputpass 0.020values this steppassstatus0.020error
tolerance
A tolerance defines how close a computed real value must be.
abs
`abs(measured - expected)` turns signed difference into magnitude.
status label
The trace carries a small pass/fail label instead of raising a runtime error.