std.math.numeric Package

Function Description

The math.numeric package provides extensions for basic types.

For example:

  1. Big integer (BigInt) is supported.
  2. High-precision decimal numbers are supported.
  3. Common mathematical calculation capabilities, including high-precision calculation rules, are provided.

API List

Function

NameDescription
abs(BigInt)Obtains the absolute value of a BigInt instance through calculation.
abs(Decimal)Calculates the absolute value of Decimal.
sqrt(BigInt)Calculates the arithmetic square root of BigInt and rounds down the result.
sqrt(Decimal)Calculates the arithmetic square root of Decimal. If the result is an infinite decimal, IEEE 754-2019 decimal128 is used to round the result by default.
gcd(BigInt, BigInt)Obtains the greatest common divisor of two BigInt instances through calculation. A non-negative number (equivalent to the absolute value of the greatest common divisor) is returned.
lcm(BigInt, BigInt)Obtains the least common multiple of two BigInt instances through calculation. A positive number (equivalent to the least common multiple of the absolute value) is returned. The value 0 is returned only when any input parameter is 0.
max(BigInt, BigInt)Calculates and returns the larger one between two BigInt instances.
min(BigInt, BigInt)Calculates and returns the smaller one between two BigInt instances.
countOne(BigInt)Calculates and returns the number of 1s in the binary supplemental code of BigInt.
round(Decimal, RoundingMode)Calculates the rounded value of Decimal by rounding it to the nearest integer based on the rounding mode.

Enumeration

NameDescription
OverflowStrategySpecifies the enumeration class of overflow policies. A total of three overflow policies are included. When the BigInt and Decimal types are converted to the integer type, different overflow processing policies can be specified.

Struct

NameDescription
BigIntSpecifies a (binary) signed integer of any precision. Cangjie struct BigInt is used for calculation and type conversion of signed integers of any precision.
DecimalDecimal is used to represent signed decimal numbers of any precision. The context, result precision, and rounding rule can be specified during operation. Conversions between basic types (such as Int, UInt, String, and Float) and the BigInt type are supported. Basic attributes of Decimal objects can be queried. Basic mathematical operations are supported. Basic capabilities such as object comparison, hashing, and string printing are provided.