跳转到内容

std.fmt.parse_float.common

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

第 1 行

std.fmt.parse_float.common.BiasedFp

类型函数

第 6 行

自定义 N 位浮点类型,表示 f * 2^e。 e 带偏置,因此可直接移入指数位。 负指数表示无效结果。

pub fn BiasedFp(comptime T: type) type

参数

T: type

字段

f: MantissaT

有效数字。

e: i32

带偏置的二进制指数。

std.fmt.parse_float.common.BiasedFp.eql

函数

第 33 行

pub fn eql(self: Self, other: Self) bool

参数

self: Self
other: Self

std.fmt.parse_float.common.BiasedFp.inf

函数

第 25 行

pub fn inf(comptime FloatT: type) Self

参数

FloatT: type

std.fmt.parse_float.common.BiasedFp.Self

别名

第 10 行

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

std.fmt.parse_float.common.BiasedFp.toFloat

函数

第 37 行

pub fn toFloat(self: Self, comptime FloatT: type, negative: bool) FloatT

参数

self: Self
FloatT: type
negative: bool

std.fmt.parse_float.common.BiasedFp.zero

函数

第 17 行

pub fn zero() Self

std.fmt.parse_float.common.BiasedFp.zeroPow2

函数

第 21 行

pub fn zeroPow2(e: i32) Self

参数

e: i32

std.fmt.parse_float.common.floatFromUnsigned

函数

第 47 行

pub fn floatFromUnsigned(comptime T: type, comptime MantissaT: type, v: MantissaT) T

参数

T: type
MantissaT: type
v: MantissaT

std.fmt.parse_float.common.isDigit

函数

第 79 行

pub fn isDigit(c: u8, comptime base: u8) bool

参数

c: u8
base: u8

std.fmt.parse_float.common.isEightDigits

函数

第 73 行

判断 8 个字节是否全部为十进制数字。 不关心字节加载时的顺序。

pub fn isEightDigits(v: u64) bool

参数

v: u64

std.fmt.parse_float.common.mantissaType

类型函数

第 90 行

返回用于浮点类型尾数的底层存储类型。 输出无符号类型的位数必须至少与输入浮点类型相同。

pub fn mantissaType(comptime T: type) type

参数

T: type

std.fmt.parse_float.common.Number

类型函数

第 59 行

以各组成部分表示解析后的浮点值。

pub fn Number(comptime T: type) type

参数

T: type

字段

exponent: i64
mantissa: mantissaType(T)
negative: bool
many_digits: bool

解析期间发现超过 max_mantissa 个数字。

hex: bool

该数字为十六进制浮点数(例如 0x1.234p567)

std.fmt.parse_float.common.std

别名

第 1 行

别名目标 std

源码

std/fmt/parse_float/common.zig

const std = @import("std");

/// 自定义 N 位浮点类型,表示 `f * 2^e`。
/// e 带偏置,因此可直接移入指数位。
/// 负指数表示无效结果。
pub fn BiasedFp(comptime T: type) type {
    const MantissaT = mantissaType(T);

    return struct {
        const Self = @This();

        /// 有效数字。
        f: MantissaT,
        /// 带偏置的二进制指数。
        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;
        }
    };
}

pub fn floatFromUnsigned(comptime T: type, comptime MantissaT: type, v: MantissaT) T {
    return switch (T) {
        f16 => @as(f16, @bitCast(@as(u16, @truncate(v)))),
        f32 => @as(f32, @bitCast(@as(u32, @truncate(v)))),
        f64 => @as(f64, @bitCast(@as(u64, @truncate(v)))),
        f80 => @as(f80, @bitCast(@as(u80, @truncate(v)))),
        f128 => @as(f128, @bitCast(v)),
        else => unreachable,
    };
}

/// 以各组成部分表示解析后的浮点值。
pub fn Number(comptime T: type) type {
    return struct {
        exponent: i64,
        mantissa: mantissaType(T),
        negative: bool,
        /// 解析期间发现超过 max_mantissa 个数字。
        many_digits: bool,
        /// 该数字为十六进制浮点数(例如 0x1.234p567)
        hex: bool,
    };
}

/// 判断 8 个字节是否全部为十进制数字。
/// 不关心字节加载时的顺序。
pub fn isEightDigits(v: u64) bool {
    const a = v +% 0x4646_4646_4646_4646;
    const b = v -% 0x3030_3030_3030_3030;
    return ((a | b) & 0x8080_8080_8080_8080) == 0;
}

pub fn isDigit(c: u8, comptime base: u8) bool {
    std.debug.assert(base == 10 or base == 16);

    return if (base == 10)
        '0' <= c and c <= '9'
    else
        '0' <= c and c <= '9' or 'a' <= c and c <= 'f' or 'A' <= c and c <= 'F';
}

/// 返回用于浮点类型尾数的底层存储类型。
/// 输出无符号类型的位数必须至少与输入浮点类型相同。
pub fn mantissaType(comptime T: type) type {
    return switch (T) {
        f16, f32, f64 => u64,
        f80, f128 => u128,
        else => unreachable,
    };
}