Data Validation Pipelines
Range Clamp
Keep Values Inside Bounds
Validation pipelines often bound values before the next calculation step. max and min make that clamp explicit.
Program
Play the program to choose the upper bound and see which readings are clipped.
range_clamp.f90
Replay: real traced execution (multi-file project)
program range_clamp_demo
implicit none
integer :: raw(4)
integer :: lower_bound
integer :: upper_bound
integer :: clipped(4)
integer :: clipped_total
raw = [-2, 4, 11, 15]
lower_bound = 0
upper_bound = 10
clipped = min(max(raw, lower_bound), upper_bound)
clipped_total = sum(clipped)
print '(I0, 1X, I0)', clipped(4), clipped_total
end program range_clamp_demo
program range_clamp_demo
implicit none
integer :: raw(4)
integer :: lower_bound
integer :: upper_bound
integer :: clipped(4)
integer :: clipped_total
raw = [-2, 4, 11, 15]
lower_bound = 0
upper_bound = 8
clipped = min(max(raw, lower_bound), upper_bound)
clipped_total = sum(clipped)
print '(I0, 1X, I0)', clipped(4), clipped_total
end program range_clamp_demo
program range_clamp_demo
implicit none
integer :: raw(4)
integer :: lower_bound
integer :: upper_bound
integer :: clipped(4)
integer :: clipped_total
raw = [-2, 4, 11, 15]
lower_bound = 0
upper_bound = 20
clipped = min(max(raw, lower_bound), upper_bound)
clipped_total = sum(clipped)
print '(I0, 1X, I0)', clipped(4), clipped_total
end program range_clamp_demo
raw ← [-2, 4, 11, 15]
9raw = [-2, 4, 11, 15]10lower_bound = 0values this step[-2, 4, 11, 15]rawlower_bound ← 0
9raw = [-2, 4, 11, 15]10lower_bound = 011upper_bound = 10values this step0lower_boundupper_bound ← 10
10lower_bound = 011upper_bound = 1012clipped = min(max(raw, lower_bound), upper_bound)values this step10upper_boundclipped ← [0, 4, 10, 10]
11upper_bound = 1012clipped = min(max(raw, lower_bound), upper_bound)13clipped_total = sum(clipped)values this step[0, 4, 10, 10]clipped[-2, 4, 11, 15]raw0lower_bound10upper_boundclipped_total ← 24
12clipped = min(max(raw, lower_bound), upper_bound)13clipped_total = sum(clipped)14print '(I0, 1X, I0)', clipped(4), clipped_totalvalues this step24clipped_total[0, 4, 10, 10]clippedprint '(I0, 1X, I0)', clipped(4), clipped_total
13 clipped_total = sum(clipped)14 print '(I0, 1X, I0)', clipped(4), clipped_total15end program range_clamp_demooutput10 24values this step10clipped(4)24clipped_total
raw ← [-2, 4, 11, 15]
9raw = [-2, 4, 11, 15]10lower_bound = 0values this step[-2, 4, 11, 15]rawlower_bound ← 0
9raw = [-2, 4, 11, 15]10lower_bound = 011upper_bound = 8values this step0lower_boundupper_bound ← 8
10lower_bound = 011upper_bound = 812clipped = min(max(raw, lower_bound), upper_bound)values this step8upper_boundclipped ← [0, 4, 8, 8]
11upper_bound = 812clipped = min(max(raw, lower_bound), upper_bound)13clipped_total = sum(clipped)values this step[0, 4, 8, 8]clipped[-2, 4, 11, 15]raw0lower_bound8upper_boundclipped_total ← 20
12clipped = min(max(raw, lower_bound), upper_bound)13clipped_total = sum(clipped)14print '(I0, 1X, I0)', clipped(4), clipped_totalvalues this step20clipped_total[0, 4, 8, 8]clippedprint '(I0, 1X, I0)', clipped(4), clipped_total
13 clipped_total = sum(clipped)14 print '(I0, 1X, I0)', clipped(4), clipped_total15end program range_clamp_demooutput8 20values this step8clipped(4)20clipped_total
raw ← [-2, 4, 11, 15]
9raw = [-2, 4, 11, 15]10lower_bound = 0values this step[-2, 4, 11, 15]rawlower_bound ← 0
9raw = [-2, 4, 11, 15]10lower_bound = 011upper_bound = 20values this step0lower_boundupper_bound ← 20
10lower_bound = 011upper_bound = 2012clipped = min(max(raw, lower_bound), upper_bound)values this step20upper_boundclipped ← [0, 4, 11, 15]
11upper_bound = 2012clipped = min(max(raw, lower_bound), upper_bound)13clipped_total = sum(clipped)values this step[0, 4, 11, 15]clipped[-2, 4, 11, 15]raw0lower_bound20upper_boundclipped_total ← 30
12clipped = min(max(raw, lower_bound), upper_bound)13clipped_total = sum(clipped)14print '(I0, 1X, I0)', clipped(4), clipped_totalvalues this step30clipped_total[0, 4, 11, 15]clippedprint '(I0, 1X, I0)', clipped(4), clipped_total
13 clipped_total = sum(clipped)14 print '(I0, 1X, I0)', clipped(4), clipped_total15end program range_clamp_demooutput15 30values this step15clipped(4)30clipped_total
bounds
Lower and upper bounds define the accepted numeric interval.
elemental max/min
`max` and `min` apply element by element to the array.
clipped total
A summary after clamping can be compared across bound choices.