Skip to content

Modern Math NegChecked

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

Home › Math Library › Math Library › NegChecked

NegChecked

Negates an integer, returning an empty Optional for the most negative value.

Syntax

NegChecked( byval a as T ) as Optional( of T )

Notes

  • One failing case, and it is the same one AbsChecked has: the most negative value of a two's-complement type has no positive counterpart. On a BYTE there is no +128.
  • Every other input negates cleanly.
  • NegSaturating clamps that case and NegWrapping returns it unchanged, which is what the bare - already does.

Examples

#import "std/numeric"
using numeric

dim as int8 worst = -128

var r = NegChecked( worst )
print r.HasValue( )          '' false -- there is no +128 in a BYTE

'' The bare operator does something DIFFERENT again: it promotes the
'' BYTE to an integer first, so the answer is a correct 128 that no
'' longer fits back in the type it came from
print -worst                 '' 128

dim as int8 ordinary = 100
print NegChecked( ordinary ).Value( )         '' -100

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

Clone this wiki locally