Skip to content

Modern Math AddWrapping

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

Home › Math Library › Math Library › AddWrapping

AddWrapping

Adds two integers and wraps around on overflow, on purpose.

Syntax

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

Notes

  • Exactly what a + b already does. It exists so that deliberate wrapping -- a hash mix, a checksum, a ring index -- can be spelled as deliberate, and so a reader does not have to guess whether the author knew.
  • No cost: it compiles to the same instruction as the operator.
  • AddChecked is the one that reports overflow and AddSaturating the one that clamps.

Examples

#import "std/numeric"
using numeric

dim as int8 a = 100, b = 100

print AddWrapping( a, b )        '' -56 -- wrapped, and that is the point

'' The value of this over the bare operator is that the INTENT is on
'' the page: a reader does not have to work out whether the overflow
'' was known about

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

Clone this wiki locally