std.math.sqrt.expect
别名
别名目标 std.testing.expect
std.math.sqrt.math
别名
别名目标 std.math
std.math.sqrt.maxInt
别名
别名目标 std.math.maxInt
std.math.sqrt.sqrt
函数
返回 x 的平方根。
特殊情况:
- sqrt(+inf) = +inf
- sqrt(+-0) = +-0
- sqrt(x) = nan,如果 x < 0
- sqrt(nan) = nan TODO: 确定是否应直接在 @sqrt 内置函数中实现这些逻辑。
pub fn sqrt(x: anytype) Sqrt(@TypeOf(x))
std.math.sqrt.Sqrt
类型函数
返回当操作数类型为 T 时,sqrt 将返回的类型。
pub fn Sqrt(comptime T: type) type
参数
T: type
std.math.sqrt.sqrt_int
函数
fn sqrt_int(comptime T: type, value: T) Sqrt(T)
参数
T: type
value: T
用法示例
test sqrt_int {
try expect(sqrt_int(u32, 3) == 1);
try expect(sqrt_int(u32, 4) == 2);
try expect(sqrt_int(u32, 5) == 2);
try expect(sqrt_int(u32, 8) == 2);
try expect(sqrt_int(u32, 9) == 3);
try expect(sqrt_int(u32, 10) == 3);
try expect(sqrt_int(u0, 0) == 0);
try expect(sqrt_int(u1, 1) == 1);
try expect(sqrt_int(u2, 3) == 1);
try expect(sqrt_int(u3, 4) == 2);
try expect(sqrt_int(u4, 8) == 2);
try expect(sqrt_int(u4, 9) == 3);
}
std.math.sqrt.std
别名
别名目标 std
std.math.sqrt.TypeId
别名
别名目标 std.lang.TypeId
源码
std/math/sqrt.zig
const std = @import("../std.zig");
const math = std.math;
const expect = std.testing.expect;
const TypeId = std.builtin.TypeId;
const maxInt = std.math.maxInt;
/// 返回 x 的平方根。
///
/// 特殊情况:
/// - sqrt(+inf) = +inf
/// - sqrt(+-0) = +-0
/// - sqrt(x) = nan,如果 x < 0
/// - sqrt(nan) = nan
/// TODO: 确定是否应直接在 @sqrt 内置函数中实现这些逻辑。
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)),
}
}
fn sqrt_int(comptime T: type, value: T) Sqrt(T) {
if (@typeInfo(T).int.bits <= 2) {
return if (value == 0) 0 else 1; // 对少量位的快捷处理,以简化通用情况。
} else {
const bits = @typeInfo(T).int.bits;
const max = math.maxInt(T);
const minustwo = (@as(T, 2) ^ max) + 1; // 无符号整数无法表示 -2
var op = value;
var res: T = 0;
var one: T = 1 << ((bits - 1) & minustwo); // 能够由 T 表示的最大四的幂
// "one" 初始为不大于参数的最大四次幂。
while (one > op) {
one >>= 2;
}
while (one != 0) {
const c = op >= res + one;
if (c) op -= res + one;
res >>= 1;
if (c) res += one;
one >>= 2;
}
return @as(Sqrt(T), @intCast(res));
}
}
test sqrt_int {
try expect(sqrt_int(u32, 3) == 1);
try expect(sqrt_int(u32, 4) == 2);
try expect(sqrt_int(u32, 5) == 2);
try expect(sqrt_int(u32, 8) == 2);
try expect(sqrt_int(u32, 9) == 3);
try expect(sqrt_int(u32, 10) == 3);
try expect(sqrt_int(u0, 0) == 0);
try expect(sqrt_int(u1, 1) == 1);
try expect(sqrt_int(u2, 3) == 1);
try expect(sqrt_int(u3, 4) == 2);
try expect(sqrt_int(u4, 8) == 2);
try expect(sqrt_int(u4, 9) == 3);
}
/// 返回当操作数类型为 `T` 时,`sqrt` 将返回的类型。
pub fn Sqrt(comptime T: type) type {
return switch (@typeInfo(T)) {
.int => |int| @Int(.unsigned, (int.bits + 1) / 2),
else => T,
};
}