Fuzion Logo
flang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.

Idiom # 86: Check if integer multiplication will overflow

See programming-idioms.org:

Code

For all integers with wrap-around semantics (i.e. all the standard types i32, u64, etc.), there are two ways to check for overflow on multiplication. First, the predicate overflowOnMul checks if a multiplication of two values would overflow. Next, there is a multiplication operator infix *? that performs overflow checking and returns a numOption that can be checked afterwards.

Note that infix * would result in a runtime error in case of an overflow if debug checks are enabled. If overflow semantics are desired, operator infix *° has to be used.







Running Example