DoxigAlpha

sqrt

Returns the square root of x.

Special Cases:

  • sqrt(+inf) = +inf
  • sqrt(+-0) = +-0
  • sqrt(x) = nan if x < 0
  • sqrt(nan) = nan TODO Decide if all this logic should be implemented directly in the @sqrt builtin function.

Function parameters

Parameters

#
x:anytype

Returns the square root of x.

Functions

#
sqrt
Returns the square root of x.
Sqrt
Returns the return type `sqrt` will return given an operand of type `T`.

Source

Implementation

#
pub fn sqrt(x: anytype) Sqrt(@TypeOf(x)) {
    const T = @TypeOf(x);
    switch (@typeInfo(T)) {
        .float, .comptime_float => return @sqrt(x),
        .comptime_int => comptime {
            if (x > maxInt(u128)) {
                @compileError("sqrt not implemented for comptime_int greater than 128 bits");
            }
            if (x < 0) {
                @compileError("sqrt on negative number");
            }
            return @as(T, sqrt_int(u128, x));
        },
        .int => |IntType| switch (IntType.signedness) {
            .signed => @compileError("sqrt not implemented for signed integers"),
            .unsigned => return sqrt_int(T, x),
        },
        else => @compileError("sqrt not implemented for " ++ @typeName(T)),
    }
}