跳转到内容

std.fmt.parse_float.convert_hex

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

第 1 行

将十六进制浮点表示转换为精确值。

std.fmt.parse_float.convert_hex.common

别名

第 7 行

别名目标 std.fmt.parse_float.common

std.fmt.parse_float.convert_hex.convertHex

函数

第 17 行

pub fn convertHex(comptime T: type, n_: Number(T)) T

参数

T: type
n_: Number(T)

std.fmt.parse_float.convert_hex.floatFromUnsigned

别名

第 9 行

别名目标 std.fmt.parse_float.common.floatFromUnsigned

std.fmt.parse_float.convert_hex.math

别名

第 6 行

别名目标 std.math

std.fmt.parse_float.convert_hex.Number

别名

第 8 行

别名目标 std.fmt.parse_float.common.Number

std.fmt.parse_float.convert_hex.std

别名

第 5 行

别名目标 std

源码

std/fmt/parse_float/convert_hex.zig

//! 将十六进制浮点表示转换为精确值。
//
// 源自 golang strconv/atof.go。

const std = @import("std");
const math = std.math;
const common = @import("common.zig");
const Number = common.Number;
const floatFromUnsigned = common.floatFromUnsigned;

// 转换 0xMMM.NNNpEEE 形式。
//
// MMM.NNN = 尾数
// EEE = 指数
//
// MMM.NNN 存储为整数,指数进行偏移。
pub fn convertHex(comptime T: type, n_: Number(T)) T {
    const MantissaT = common.mantissaType(T);
    var n = n_;

    if (n.mantissa == 0) {
        return if (n.negative) -0.0 else 0.0;
    }

    const max_exp = math.floatExponentMax(T);
    const min_exp = math.floatExponentMin(T);
    const mantissa_bits = math.floatMantissaBits(T);
    const fractional_bits = math.floatFractionalBits(T);
    const exp_bits = math.floatExponentBits(T);
    const exp_bias = min_exp - 1;

    // 尾数现在隐式除以 2^fractional_bits。
    n.exponent += fractional_bits;

    // 移位调整尾数和指数,使表示落入 float 范围。
    // 最终希望尾数以一个值为 1 的位开头,后跟 mantbits 个位。
    // 为舍入还需额外两位,其中最低位表示
    // 该位或其后任意位是否曾非零。
    // 若尾数已丢失非零位,则 trunc 为 true,
    // 适当左移后,在下方按位或入一个 1。
    while (n.mantissa != 0 and n.mantissa >> (mantissa_bits + 2) == 0) {
        n.mantissa <<= 1;
        n.exponent -= 1;
    }
    if (n.many_digits) {
        n.mantissa |= 1;
    }
    while (n.mantissa >> (1 + fractional_bits + 2) != 0) {
        n.mantissa = (n.mantissa >> 1) | (n.mantissa & 1);
        n.exponent += 1;
    }

    // 若指数过小,
    // 则尝试非正规化,使其可表示。
    // 其中 -2 用于舍入位。
    while (n.mantissa > 1 and n.exponent < min_exp - 2) {
        n.mantissa = (n.mantissa >> 1) | (n.mantissa & 1);
        n.exponent += 1;
    }

    // 使用最低两位舍入。
    var round = n.mantissa & 3;
    n.mantissa >>= 2;
    round |= n.mantissa & 1; // 舍入至偶数(尾数为奇数时向上舍入)
    n.exponent += 2;
    if (round == 3) {
        n.mantissa += 1;
        if (n.mantissa == 1 << (1 + fractional_bits)) {
            n.mantissa >>= 1;
            n.exponent += 1;
        }
    }

    // 非正规数或零
    if (n.mantissa >> fractional_bits == 0) {
        n.exponent = exp_bias;
    }

    // 无穷大与范围错误
    if (n.exponent > max_exp) {
        return if (n.negative) -math.inf(T) else math.inf(T);
    }

    var bits = n.mantissa & ((1 << mantissa_bits) - 1);
    bits |= @as(MantissaT, @intCast((n.exponent - exp_bias) & ((1 << exp_bits) - 1))) << mantissa_bits;
    if (n.negative) {
        bits |= 1 << (mantissa_bits + exp_bits);
    }
    return floatFromUnsigned(T, MantissaT, bits);
}