跳转到内容

std.fmt.parse_float.FloatInfo

标准库Zig 0.17.0官方原文
源码std/fmt/parse_float/FloatInfo.zig

第 1 行

字段

min_exponent_fast_path: comptime_int
max_exponent_fast_path: comptime_int
max_exponent_fast_path_disguised: comptime_int
max_mantissa_fast_path: comptime_int
smallest_power_of_ten: comptime_int
largest_power_of_ten: comptime_int
mantissa_explicit_bits: comptime_int
minimum_exponent: comptime_int
min_exponent_round_to_even: comptime_int
max_exponent_round_to_even: comptime_int
infinite_power: comptime_int

std.fmt.parse_float.FloatInfo.from

函数

第 54 行

pub fn from(comptime T: type) Self

参数

T: type

std.fmt.parse_float.FloatInfo.Self

别名

第 2 行

别名目标 std.fmt.parse_float.FloatInfo

std.fmt.parse_float.FloatInfo.std

别名

第 1 行

别名目标 std

源码

std/fmt/parse_float/FloatInfo.zig

const std = @import("std");
const Self = @This();

// 快速路径情形的最小指数,即 `-⌊(MANTISSA_EXPLICIT_BITS+1)/log2(5)⌋`
min_exponent_fast_path: comptime_int,

// 适用于快速路径情况的最大指数,即 `⌊(MANTISSA_EXPLICIT_BITS+1)/log2(5)⌋`。
max_exponent_fast_path: comptime_int,

// 伪装快速路径情形可表示的最大指数。
// 其值为 `MAX_EXPONENT_FAST_PATH + ⌊(MANTISSA_EXPLICIT_BITS+1)/log2(10)⌋`
max_exponent_fast_path_disguised: comptime_int,

// 快速路径的最大尾数(f64 为 `1 << 53`)。
max_mantissa_fast_path: comptime_int,

// 非零值的最小十进制指数,包括次正规数。
smallest_power_of_ten: comptime_int,

// 非无穷值的最大十进制指数。
largest_power_of_ten: comptime_int,

// 有效数的位数,*不包含*隐含位。
mantissa_explicit_bits: comptime_int,

// 最小指数值为 `-(1 << (EXP_BITS - 1)) + 1`。
minimum_exponent: comptime_int,

// 仅当 q 为负数时才可能发生向偶数舍入,
// 且 64 位情况下要求 q ≥ −4,
// 32 位情况下要求 q ≥ −17。
//
// 当 q ≥ 0 时,有 5^q ≤ 2m+1。64 位情况下,
// 5^q ≤ 2m+1 ≤ 2^54,即 q ≤ 23;32 位情况下,
// 5^q ≤ 2m+1 ≤ 2^25,即 q ≤ 10。
//
// 当 q < 0 时,有 w ≥ (2m+1)×5^−q。又必须满足 w < 2^64,
// 因此 (2m+1)×5^−q < 2^64。64 位情况下有 2m+1 > 2^53,
// 32 位情况下有 2m+1 > 2^24。因此必须满足 2^53×5^−q < 2^64
// (64 位)以及 2^24×5^−q < 2^64(32 位)。所以有 5^−q < 2^11,
// 即 q ≥ −4(64 位);以及 5^−q < 2^40,即 q ≥ −17(32 位)。
//
// 因此,只有在 q ∈ [−4,23](64 位)
// 或 q∈[−17,10](32 位)时,
// 才需要对中点情形向偶数舍入。这两种情况下,五的幂 5^|q|
// 均能放入 64 位机器字。
min_exponent_round_to_even: comptime_int,
max_exponent_round_to_even: comptime_int,

// 最大指数值 `(1 << EXP_BITS) - 1`。
infinite_power: comptime_int,

// 在可能的情况下,应根据推导计算结果来计算下列值。
pub fn from(comptime T: type) Self {
    return switch (T) {
        f16 => .{
            // 快速路径
            .min_exponent_fast_path = -4,
            .max_exponent_fast_path = 4,
            .max_exponent_fast_path_disguised = 7,
            .max_mantissa_fast_path = 2 << std.math.floatMantissaBits(T),
            // 慢速方法 + Eisel-Lemire
            .mantissa_explicit_bits = std.math.floatFractionalBits(T),
            .infinite_power = 0x1f,
            // Eisel-Lemire
            .smallest_power_of_ten = -26, // TODO: 细化;一个测试失败
            .largest_power_of_ten = 4,
            .minimum_exponent = -15,
            // w >= (2m+1) * 5^-q and w < 2^64
            // => 2m+1 > 2^11
            // => 2^11*5^-q < 2^64
            // => 5^-q < 2^53
            // => q >= -23
            .min_exponent_round_to_even = -22,
            .max_exponent_round_to_even = 5,
        },
        f32 => .{
            // 快速路径
            .min_exponent_fast_path = -10,
            .max_exponent_fast_path = 10,
            .max_exponent_fast_path_disguised = 17,
            .max_mantissa_fast_path = 2 << std.math.floatMantissaBits(T),
            // 慢速方法 + Eisel-Lemire
            .mantissa_explicit_bits = std.math.floatFractionalBits(T),
            .infinite_power = 0xff,
            // Eisel-Lemire
            .smallest_power_of_ten = -65,
            .largest_power_of_ten = 38,
            .minimum_exponent = -127,
            .min_exponent_round_to_even = -17,
            .max_exponent_round_to_even = 10,
        },
        f64 => .{
            // 快速路径
            .min_exponent_fast_path = -22,
            .max_exponent_fast_path = 22,
            .max_exponent_fast_path_disguised = 37,
            .max_mantissa_fast_path = 2 << std.math.floatMantissaBits(T),
            // 慢速方法 + Eisel-Lemire
            .mantissa_explicit_bits = std.math.floatMantissaBits(T),
            .infinite_power = 0x7ff,
            // Eisel-Lemire
            .smallest_power_of_ten = -342,
            .largest_power_of_ten = 308,
            .minimum_exponent = -1023,
            .min_exponent_round_to_even = -4,
            .max_exponent_round_to_even = 23,
        },
        f80 => .{
            // 快速路径
            .min_exponent_fast_path = -27,
            .max_exponent_fast_path = 27,
            .max_exponent_fast_path_disguised = 46,
            .max_mantissa_fast_path = 2 << std.math.floatMantissaBits(T),
            // 慢速方法 + Eisel-Lemire
            .mantissa_explicit_bits = std.math.floatFractionalBits(T),
            .infinite_power = 0x7fff,
            // Eisel-Lemire。
            // 注意:尚未测试(没有 f80 eisel-lemire 实现)。
            .smallest_power_of_ten = -4966,
            .largest_power_of_ten = 4932,
            .minimum_exponent = -16382,
            // 2^65 * 5^-q < 2^80
            // 5^-q < 2^15
            // => q >= -6
            .min_exponent_round_to_even = -6,
            .max_exponent_round_to_even = 28,
        },
        f128 => .{
            // 快速路径
            .min_exponent_fast_path = -48,
            .max_exponent_fast_path = 48,
            .max_exponent_fast_path_disguised = 82,
            .max_mantissa_fast_path = 2 << std.math.floatMantissaBits(T),
            // 慢速方法 + Eisel-Lemire
            .mantissa_explicit_bits = std.math.floatFractionalBits(T),
            .infinite_power = 0x7fff,
            // Eisel-Lemire。
            // 注意:尚未测试(没有 f128 的 Eisel-Lemire 实现)
            .smallest_power_of_ten = -4966,
            .largest_power_of_ten = 4932,
            .minimum_exponent = -16382,
            // 2^113 * 5^-q < 2^128
            // 5^-q < 2^15
            // => q >= -6
            .min_exponent_round_to_even = -6,
            .max_exponent_round_to_even = 49,
        },
        else => unreachable,
    };
}