Core Library

The core library provides essential numerical functions including splitting operations, predicates, exponent manipulation, and context queries.

Splitting Functions

fpy2.libraries.core.split(x, n)

Splits x into two parts:

  • all digits of x that are above the n th digit

  • all digits of x that are at or below the n th digit

The operation is performed exactly.

Parameters:
  • x (Float) – Value to split

  • n (Float) – Digit position (must be an integer)

Returns:

Tuple of (high_part, low_part)

Return type:

tuple[Float, Float]

Raises:

ValueError – if n is not an integer

Special cases:

  • if x is NaN, the result is (NaN, NaN)

  • if x is infinite, the result is (x, x)

Primitive: This is an FPy primitive with context parameter ‘R’ and return context (‘R’, ‘R’).

fpy2.libraries.core.modf(x)

Decomposes x into its integral and fractional parts. The operation is performed exactly.

Parameters:

x (Float) – Value to decompose

Returns:

Tuple of (fractional_part, integral_part)

Return type:

tuple[Float, Float]

Special cases (mirroring C/C++ modf):

  • if x is +/-0, the result is (+/-0, +/-0)

  • if x is +/-Inf, the result is (+/-0, +/-Inf)

  • if x is NaN, the result is (NaN, NaN)

Primitive: This is an FPy primitive with context parameter ‘R’ and return context (‘R’, ‘R’).

fpy2.libraries.core.frexp(x)

Decomposes x into its mantissa and exponent. The computation is performed exactly.

Parameters:

x (Float) – Value to decompose

Returns:

Tuple of (mantissa, exponent)

Return type:

tuple[Float, Float]

Special cases (mirroring C/C++ frexp):

  • if x is NaN, the result is (NaN, NaN)

  • if x is infinity, the result is (x, NaN)

  • if x is zero, the result is (x, 0)

Primitive: This is an FPy primitive with context parameter ‘R’ and return context (‘R’, ‘R’).

Predicates

fpy2.libraries.core.isinteger(x)

Checks if x is an integer.

Parameters:

x (Real) – Value to check

Returns:

True if x is an integer, False otherwise

Return type:

bool

fpy2.libraries.core.isnar(x)

Checks if x is either NaN or infinity (Not-a-Real).

Parameters:

x (Real) – Value to check

Returns:

True if x is NaN or infinity, False otherwise

Return type:

bool

Exponent Functions

fpy2.libraries.core.logb(x)

Returns the normalized exponent of x.

Parameters:

x (Float) – Input value

Returns:

Normalized exponent

Return type:

Float

Special cases:

  • If x == 0, the result is -INFINITY

  • If x is NaN, the result is NaN

  • If x is infinite, the result is INFINITY

Primitive: This is an FPy primitive with context parameter ‘R’ and return context ‘R’.

fpy2.libraries.core.ldexp(x, n)

Computes x * 2**n with correct rounding.

Parameters:
  • x (Float) – Base value

  • n (Float) – Exponent (must be an integer)

Returns:

Result of x * 2**n

Return type:

Float

Raises:

ValueError – if n is not an integer

Special cases:

  • If x is NaN, the result is NaN

  • If x is infinite, the result is infinite

Primitive: This is an FPy primitive with context parameter ‘R’ and return context ‘R’.

fpy2.libraries.core.max_e(xs)

Computes the largest (normalized) exponent of the subset of finite, non-zero elements of xs.

Parameters:

xs (list[Real]) – List of values

Returns:

Tuple of (largest_exponent, exists_non_zero)

Return type:

tuple[Real, bool]

Returns the largest exponent and whether any such element exists. If all elements are zero, infinite, or NaN, the exponent is 0.

Function context: Uses INTEGER context.

Context Operations

fpy2.libraries.core.max_p()

Returns the maximum precision of the current context. This is a no-op for the RealContext.

Returns:

Maximum precision

Return type:

Float

Raises:

ValueError – if the context does not have a maximum precision

Primitive: This is an FPy primitive with context parameter ‘R’ and return context ‘R’.

fpy2.libraries.core.min_n()

Returns the least absolute digit of the current context. This is the position of the most significant digit that can never be represented.

Returns:

Least absolute digit position

Return type:

Float

Raises:

ValueError – if the context does not have a least absolute digit

Primitive: This is an FPy primitive with context parameter ‘R’ and return context ‘R’.