Ship

Book · Numbers

Numbers

Draft

Keep integer values within inclusive bounds with a reusable clamp.

On this page · 2 sections

Clamping values

Utility.Numbers.clamp comes from the mounted utility project. It returns the nearest bound when an Int falls outside an inclusive range and leaves values inside the range unchanged.

Clamp To Inclusive Range

test✓
  let belowbelow: Int = Utility.Numbers.clampclamp: pure fn!(value: Int, minimum: Int, maximum: Int) -> Int(22: Int, 33: Int, 88: Int);
  expect ✓belowbelow: Int == 33: Int;

  let insideinside: Int = Utility.Numbers.clampclamp: pure fn!(value: Int, minimum: Int, maximum: Int) -> Int(55: Int, 33: Int, 88: Int);
  expect ✓insideinside: Int == 55: Int;

  let aboveabove: Int = Utility.Numbers.clampclamp: pure fn!(value: Int, minimum: Int, maximum: Int) -> Int(1111: Int, 33: Int, 88: Int);
  expect ✓aboveabove: Int == 88: Int;

Comparisons

Numeric comparisons return Bool and require compatible operands.

let signsign: fn(value: Int) -> String = fn(valuevalue: Int) {
  if valuevalue: Int < 00: Int {
    return "negative""negative": String;
  }
  if valuevalue: Int == 00: Int {
    return "zero""zero": String;
  }
  "positive""positive": String
};

Numeric Comparisons

test✓
  expect ✓signsign: fn(value: Int) -> String(00: Int - 33: Int) == "negative""negative": String;
  expect ✓signsign: fn(value: Int) -> String(00: Int) == "zero""zero": String;
  expect ✓signsign: fn(value: Int) -> String(77: Int) == "positive""positive": String;
  expect ✓11: Int < 22: Int;
  expect ✓22: Int <= 22: Int;
  expect ✓33: Int > 44: Int == FalseFalse: Bool;
  expect ✓44: Int >= 44: Int;
  expect ✓1.51.5: Float < 2.252.25: Float;
  expect ✓2.52.5: Float >= 2.52.5: Float;