Skip to content

Modern Math MulSaturating

Paul Squires edited this page Oct 19, 2026 · 5 revisions

Home › Math Library › Math Library › MulSaturating

MulSaturating

Multiplies two integers, clamping the result to the type's minimum or maximum.

Syntax

MulSaturating( byval a as T, byval b as T ) as T

Notes

  • Saturating: a result past the top comes back as the maximum and one past the bottom as the minimum, which is what signal and audio code wants -- a sample that clips is better than one that wraps to the opposite sign.
  • No Optional, no branch at the call site: it always returns a T.
  • MulChecked reports the overflow instead, and MulWrapping wraps.

Examples

#import "std/numeric"
using numeric

dim as int8 a = 100, b = 100

print MulSaturating( a, b )     '' 127 -- clamped to the maximum, not wrapped

dim as int8 lo = -100, lo2 = -100
print MulSaturating( lo, lo2 )   '' -128 -- clamped at the other end

Library: Math Library
Category: Math Library
Documented under: Members

Clone this wiki locally