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.

upper_bound
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
  1. raw ← [-2, 4, 11, 15]

    9raw = [-2, 4, 11, 15]10lower_bound = 0
    values this step[-2, 4, 11, 15]raw
  2. lower_bound ← 0

    9raw = [-2, 4, 11, 15]10lower_bound = 011upper_bound = 10
    values this step0lower_bound
  3. upper_bound ← 10

    10lower_bound = 011upper_bound = 1012clipped = min(max(raw, lower_bound), upper_bound)
    values this step10upper_bound
  4. clipped ← [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_bound
  5. clipped_total ← 24

    12clipped = min(max(raw, lower_bound), upper_bound)13clipped_total = sum(clipped)14print '(I0, 1X, I0)', clipped(4), clipped_total
    values this step24clipped_total[0, 4, 10, 10]clipped
  6. print '(I0, 1X, I0)', clipped(4), clipped_total

    13    clipped_total = sum(clipped)14    print '(I0, 1X, I0)', clipped(4), clipped_total15end program range_clamp_demo
    output10 24
    values this step10clipped(4)24clipped_total
  1. raw ← [-2, 4, 11, 15]

    9raw = [-2, 4, 11, 15]10lower_bound = 0
    values this step[-2, 4, 11, 15]raw
  2. lower_bound ← 0

    9raw = [-2, 4, 11, 15]10lower_bound = 011upper_bound = 8
    values this step0lower_bound
  3. upper_bound ← 8

    10lower_bound = 011upper_bound = 812clipped = min(max(raw, lower_bound), upper_bound)
    values this step8upper_bound
  4. clipped ← [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_bound
  5. clipped_total ← 20

    12clipped = min(max(raw, lower_bound), upper_bound)13clipped_total = sum(clipped)14print '(I0, 1X, I0)', clipped(4), clipped_total
    values this step20clipped_total[0, 4, 8, 8]clipped
  6. print '(I0, 1X, I0)', clipped(4), clipped_total

    13    clipped_total = sum(clipped)14    print '(I0, 1X, I0)', clipped(4), clipped_total15end program range_clamp_demo
    output8 20
    values this step8clipped(4)20clipped_total
  1. raw ← [-2, 4, 11, 15]

    9raw = [-2, 4, 11, 15]10lower_bound = 0
    values this step[-2, 4, 11, 15]raw
  2. lower_bound ← 0

    9raw = [-2, 4, 11, 15]10lower_bound = 011upper_bound = 20
    values this step0lower_bound
  3. upper_bound ← 20

    10lower_bound = 011upper_bound = 2012clipped = min(max(raw, lower_bound), upper_bound)
    values this step20upper_bound
  4. clipped ← [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_bound
  5. clipped_total ← 30

    12clipped = min(max(raw, lower_bound), upper_bound)13clipped_total = sum(clipped)14print '(I0, 1X, I0)', clipped(4), clipped_total
    values this step30clipped_total[0, 4, 11, 15]clipped
  6. print '(I0, 1X, I0)', clipped(4), clipped_total

    13    clipped_total = sum(clipped)14    print '(I0, 1X, I0)', clipped(4), clipped_total15end program range_clamp_demo
    output15 30
    values 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.