Function sqrt [src]
Alias for std.math.sqrt.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.
Prototype
pub fn sqrt(x: anytype) Sqrt(@TypeOf(x))
Source
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)),
}
}