Builtins

FPy provides a set of builtin functions that can be used in FPy programs. Builtin functions are re-exported from the fpy2 module.

Mathematical functions under rounding contexts.

fpy2.ops.acos(x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes the inverse cosine of x rounded under ctx.

fpy2.ops.acosh(x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes the inverse hyperbolic cosine of x rounded under ctx.

fpy2.ops.add(x: int | float | Float | Fraction, y: int | float | Float | Fraction, ctx: Context = RealContext())

Adds x and y rounded under ctx.

fpy2.ops.asin(x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes the inverse sine of x rounded under ctx.

fpy2.ops.asinh(x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes the inverse hyperbolic sine of x rounded under ctx.

fpy2.ops.atan(x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes the inverse tangent of x rounded under ctx.

fpy2.ops.atan2(y: int | float | Float | Fraction, x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes atan(y / x) taking into account the correct quadrant that the point (x, y) resides in. The result is rounded under ctx.

fpy2.ops.atanh(x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes the inverse hyperbolic tangent of x under ctx.

fpy2.ops.cast(x: int | float | Float | Fraction, ctx: Context = RealContext())

Identity operation that checks if x is representable in the context ctx.

Returns x if it can be represented in ctx, otherwise raises an error. This is useful for asserting that a value fits within a specific rounding context.

fpy2.ops.cbrt(x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes the cube root of x rounded under ctx.

fpy2.ops.ceil(x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes the smallest integer greater than or equal to x that is representable under ctx.

If the context supports overflow, the result may be infinite.

fpy2.ops.const_1_pi(ctx: Context = RealContext()) Float

Creates a Float representing 1/π (one divided by pi). The result is rounded under the given context.

fpy2.ops.const_2_pi(ctx: Context = RealContext()) Float

Creates a Float representing 2/π (two divided by pi). The result is rounded under the given context.

fpy2.ops.const_2_sqrt_pi(ctx: Context = RealContext()) Float

Creates a Float representing 2/sqrt(π) (two divided by the square root of pi). The result is rounded under the given context.

fpy2.ops.const_e(ctx: Context = RealContext()) Float

Creates a Float representing e (Euler’s number). The result is rounded under the given context.

fpy2.ops.const_ln2(ctx: Context = RealContext()) Float

Creates a Float representing ln(2) (the natural logarithm of 2). The result is rounded under the given context.

fpy2.ops.const_log10e(ctx: Context = RealContext()) Float

Creates a Float representing log10(e) (the logarithm of e base 10). The result is rounded under the given context.

fpy2.ops.const_log2e(ctx: Context = RealContext()) Float

Creates a Float representing log2(e) (the logarithm of e base 2). The result is rounded under the given context.

fpy2.ops.const_pi(ctx: Context = RealContext()) Float

Creates a Float representing π (pi). The result is rounded under the given context.

fpy2.ops.const_pi_2(ctx: Context = RealContext()) Float

Creates a Float representing π/2 (pi divided by 2). The result is rounded under the given context.

fpy2.ops.const_pi_4(ctx: Context = RealContext()) Float

Creates a Float representing π/4 (pi divided by 4). The result is rounded under the given context.

fpy2.ops.const_sqrt1_2(ctx: Context = RealContext()) Float

Creates a Float representing sqrt(1/2) (the square root of 1/2). The result is rounded under the given context.

fpy2.ops.const_sqrt2(ctx: Context = RealContext()) Float

Creates a Float representing sqrt(2) (the square root of 2). The result is rounded under the given context.

fpy2.ops.copysign(x: int | float | Float | Fraction, y: int | float | Float | Fraction, ctx: Context = RealContext())

Returns |x| * sign(y) rounded under ctx.

fpy2.ops.cos(x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes the cosine of x rounded under ctx.

fpy2.ops.cosh(x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes the hyperbolic cosine x rounded under ctx.

fpy2.ops.declcontext(x: int | float | Float | Fraction, ctx: Context = RealContext()) Context

Returns the context that x was rounded under.

fpy2.ops.digits(m: int, e: int, b: int, ctx: Context = RealContext()) Float

Creates a Float of the form m * b**e, where m is the significand, e is the exponent, and b is the base.

The result is rounded under the given context.

fpy2.ops.dim(x: list, ctx: Context = RealContext())

Returns the number of dimensions of the tensor x.

Assumes that x is not a ragged tensor.

fpy2.ops.div(x: int | float | Float | Fraction, y: int | float | Float | Fraction, ctx: Context = RealContext())

Computes x / y rounded under ctx.

fpy2.ops.empty(*dims: int | float | Float | Fraction, ctx: Context = RealContext()) list

Initializes an empty nested list with dimensions specified by dims.

Each argument specifies the size of a dimension from outer to inner. For example: - empty(5) creates a 1D list of size 5 - empty(3, 4) creates a 3x4 2D list (3 outer lists, each with 4 elements) - empty(2, 3, 4) creates a 2x3x4 3D list

fpy2.ops.erf(x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes the error function of x rounded under ctx.

fpy2.ops.erfc(x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes 1 - erf(x) rounded under ctx.

fpy2.ops.exp(x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes e ** x rounded under ctx.

fpy2.ops.exp10(x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes 10 *** x rounded under ctx.

fpy2.ops.exp2(x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes 2 ** x rounded under ctx.

fpy2.ops.expm1(x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes exp(x) - 1 rounded under ctx.

fpy2.ops.fabs(x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes |x| rounded under ctx.

fpy2.ops.fdim(x: int | float | Float | Fraction, y: int | float | Float | Fraction, ctx: Context = RealContext())

Computes max(x - y, 0) rounded under ctx.

fpy2.ops.floor(x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes the largest integer less than or equal to x that is representable under ctx.

If the context supports overflow, the result may be infinite.

fpy2.ops.fma(x: int | float | Float | Fraction, y: int | float | Float | Fraction, z: int | float | Float | Fraction, ctx: Context = RealContext())

Computes x * y + z rounded under ctx.

fpy2.ops.fmax(x: int | float | Float | Fraction, y: int | float | Float | Fraction, ctx: Context = RealContext())

Computes max(x, y) rounded under ctx.

fpy2.ops.fmin(x: int | float | Float | Fraction, y: int | float | Float | Fraction, ctx: Context = RealContext())

Computes min(x, y) rounded under ctx.

fpy2.ops.fmod(x: int | float | Float | Fraction, y: int | float | Float | Fraction, ctx: Context = RealContext())

Computes the remainder of x / y rounded under this context.

The remainder has the same sign as x; it is exactly x - iquot * y, where iquot is the x / y with its fractional part truncated.

fpy2.ops.hexfloat(s: str, ctx: Context = RealContext()) Float

Creates a Float from a hexadecimal floating-point string s. The result is rounded under the given context.

fpy2.ops.hypot(x: int | float | Float | Fraction, y: int | float | Float | Fraction, ctx: Context = RealContext())

Computes sqrt(x * x + y * y) rounded under ctx.

fpy2.ops.inf(ctx: Context = RealContext()) Float

Creates a Float representing (positive) infinity. The result is rounded under the given context.

fpy2.ops.isfinite(x: int | float | Float | Fraction, ctx: Context = RealContext()) bool

Checks if x is finite.

fpy2.ops.isinf(x: int | float | Float | Fraction, ctx: Context = RealContext()) bool

Checks if x is infinite.

fpy2.ops.isnan(x: int | float | Float | Fraction, ctx: Context = RealContext()) bool

Checks if x is NaN.

fpy2.ops.isnormal(x: int | float | Float | Fraction, ctx: Context = RealContext()) bool

Checks if x is normal (not subnormal, zero, or NaN).

fpy2.ops.lgamma(x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes the log-gamma of x rounded under ctx.

fpy2.ops.log(x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes log(x) rounded under ctx.

fpy2.ops.log10(x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes log10(x) rounded under ctx.

fpy2.ops.log1p(x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes log1p(x) rounded under ctx.

fpy2.ops.log2(x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes log2(x) rounded under ctx.

fpy2.ops.logb(x: int | float | Float | Fraction, ctx: Context = RealContext()) Float

Extracts the (normalized) exponent of x, rounded under ctx.

  • If the argument is 0, returns -∞.

  • If the argument is +/-∞, returns +∞.

  • If the argument is NaN, returns NaN.

  • If the argument is (-1) * (1 + f) * b^e, returns e.

For non-zero arguments, logb(x) = floor(log_{b}(abs(x))), where b is the base of the floating-point representation

fpy2.ops.mod(x: int | float | Float | Fraction, y: int | float | Float | Fraction, ctx: Context = RealContext())

Computes x % y rounded under ctx.

Implements Python’s modulus operator, defined as:

x % y = x - floor(x / y) * y

fpy2.ops.mul(x: int | float | Float | Fraction, y: int | float | Float | Fraction, ctx: Context = RealContext())

Multiplies x and y rounded under ctx.

fpy2.ops.nan(ctx: Context = RealContext()) Float

Creates a Float representing NaN (Not a Number). The result is rounded under the given context.

fpy2.ops.nearbyint(x: int | float | Float | Fraction, ctx: Context = RealContext())

Rounds x to a representable integer according to the rounding mode of this context.

If the context supports overflow, the result may be infinite.

fpy2.ops.neg(x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes -x rounded under ctx.

fpy2.ops.pow(x: int | float | Float | Fraction, y: int | float | Float | Fraction, ctx: Context = RealContext())

Computes x**y rounded under ctx.

fpy2.ops.rational(n: int, d: int, ctx: Context = RealContext()) Float

Creates a Float from a fraction with the given numerator and denominator. The result is rounded under the given context.

fpy2.ops.remainder(x: int | float | Float | Fraction, y: int | float | Float | Fraction, ctx: Context = RealContext())

Computes the remainder of x / y rounded under ctx.

The remainder is exactly x - quo * y, where quo is the integral value nearest the exact value of x / y.

fpy2.ops.round(x: int | float | Float | Fraction, ctx: Context = RealContext())

Rounds x under the given context ctx.

If ctx is None, this operation is the identity operation.

fpy2.ops.round_at(x: int | float | Float | Fraction, n: int | float | Float | Fraction, ctx: Context = RealContext()) Float

Rounds x with least absolute digit n, the most significant digit that must definitely be rounded off. If ctx has bounded precision, the actual n use may be larger than the one specified.

fpy2.ops.round_exact(x: int | float | Float | Fraction, ctx: Context = RealContext())

Rounds x under the given context ctx.

If ctx is None, this operation is the identity operation. If the operation is not exact, it raises a ValueError.

fpy2.ops.roundint(x: int | float | Float | Fraction, ctx: Context = RealContext())

Rounds x to the nearest representable integer, rounding ties away from zero in halfway cases.

If the context supports overflow, the result may be infinite.

fpy2.ops.signbit(x: int | float | Float | Fraction, ctx: Context = RealContext()) bool

Checks if the sign bit of x is set (i.e., x is negative).

fpy2.ops.sin(x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes the sine of x rounded under ctx.

fpy2.ops.sinh(x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes the hyperbolic sine of x rounded under ctx.

fpy2.ops.size(x: list, dim: int | float | Float | Fraction, ctx: Context = RealContext())

Returns the size of the dimension dim of the tensor x.

Assumes that x is not a ragged tensor.

fpy2.ops.sqrt(x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes square-root of x rounded under ctx.

fpy2.ops.sub(x: int | float | Float | Fraction, y: int | float | Float | Fraction, ctx: Context = RealContext())

Subtracts y from x rounded under ctx.

fpy2.ops.tan(x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes the tangent of x rounded under ctx.

fpy2.ops.tanh(x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes the hyperbolic tangent of x rounded under ctx.

fpy2.ops.tgamma(x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes gamma of x rounded under ctx.

fpy2.ops.trunc(x: int | float | Float | Fraction, ctx: Context = RealContext())

Computes the integer with the largest magnitude whose magnitude is less than or equal to the magnitude of x that is representable under ctx.

If the context supports overflow, the result may be infinite.