Skip to content

Modern Math MulChecked

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

Home › Math Library › Math Library › MulChecked

MulChecked

Multiplies two integers, returning an empty Optional if the result would overflow.

Syntax

MulChecked( byval a as T, byval b as T ) as Optional( of T )

Notes

  • Returns an Optional: empty means the true result does not fit in T. The overflow is reported rather than wrapped, which is what a * b does silently.
  • The check happens before the operation, so nothing undefined occurs on the way to finding out.
  • MulSaturating clamps instead, and MulWrapping wraps on purpose.

Examples

#import "std/outcome"
#import "std/numeric"
using numeric, outcome

dim as int8 a = 100, b = 100

var r = MulChecked( a, b )
print r.HasValue( )          '' false -- 200 does not fit in a BYTE

dim as int8 small = 1
var ok = MulChecked( small, small )
print ok.HasValue( )         '' true
print ok.Value( )

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

Clone this wiki locally