Instant properties: uabs

The instant property uabs represents the absolute value of the given int8, int16, int32 or int64 operand.

Declaration

uint8  int8. uabs

uint16 int16.uabs

uint32 int32.uabs

uint64 int64.uabs

Discussion

The instant property uabs returns the absolute value of the operand in context of which it has been evaluated. The resulting data type corresponds to the unsigned version of the original operand's type. For example, if used in context of an int8 operand, the result of the operation is uint8 (unsigned 8-bit integer). For example:

var int8 n1 = -65; var int8 n2 = 65; var uint8 result1 = n1.uabs; // result1 = 65 var uint8 result2 = n2.uabs; // result2 = 65

Since the instant property uabs results in the unsigned version of the original operand's data type, the property avoids overflow errors with operands containing the maximum negative value (e.g. -128 in case of int8). In such case the instant property uabs returns the correct positive value. This distinguishes this property from its cognate version abs, which returns the signed version of the operand's data type accepting the possible overflow errors. For example:

var int8 n1 = -127; var int8 n2 = -128; var int8 result1 = n1.abs; // result1 = 127 var int8 result2 = n2.abs; // result2 = -128 OVERFLOW ERROR var uint8 result3 = n2.uabs; // result3 = 128

Please note, the instant property uabs is read-only. The write access to this property is not allowed.