Fuzion Logo
fuzion-lang.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 overflow_on_mul checks if a multiplication of two values would overflow. Next, there is a multiplication operator infix *? that performs overflow checking and returns an option 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

last changed: 2024-10-17