Skip to content

Modern Math AddChecked

Paul Squires edited this page Sep 30, 2026 · 5 revisions

Home › Math Library › Math Library › AddChecked

AddChecked

A + b, or nothing if the result would overflow.

Syntax

AddChecked( 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.
  • AddSaturating clamps instead, and AddWrapping wraps on purpose.

Examples

#import "std/numeric"
using FB

dim as int8 a = 100, b = 100

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

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

Library: Math Library
Category: Math Library
Documented under: Members
Source: docs/math/math.txt in the FBC-Modern repository.

Clone this wiki locally