pub fn BiasedFp(comptime T: type) type f: MantissaTThe significant digits.
e: i32The biased, binary exponent.
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45pub fn BiasedFp(comptime T: type) type {
const MantissaT = mantissaType(T);
return struct {
const Self = @This();
/// The significant digits.
f: MantissaT,
/// The biased, binary exponent.
e: i32,
pub fn zero() Self {
return .{ .f = 0, .e = 0 };
}
pub fn zeroPow2(e: i32) Self {
return .{ .f = 0, .e = e };
}
pub fn inf(comptime FloatT: type) Self {
const e = (1 << std.math.floatExponentBits(FloatT)) - 1;
return switch (FloatT) {
f80 => .{ .f = 0x8000000000000000, .e = e },
else => .{ .f = 0, .e = e },
};
}
pub fn eql(self: Self, other: Self) bool {
return self.f == other.f and self.e == other.e;
}
pub fn toFloat(self: Self, comptime FloatT: type, negative: bool) FloatT {
var word = self.f;
word |= @as(MantissaT, @intCast(self.e)) << std.math.floatMantissaBits(FloatT);
var f = floatFromUnsigned(FloatT, MantissaT, word);
if (negative) f = -f;
return f;
}
};
}
A custom N-bit floating point type, representing
f * 2^e. e is biased, so it be directly shifted into the exponent bits. Negative exponent indicates an invalid result.