internal/decimal_kernel API

用途

internal/decimal_kernel 承载 decimal(IEEE 754)和 decimal_gda(General Decimal Arithmetic)共同需要的十进制系数整数运算:一个持久的系数类型、加法、减法、比较、按十的幂移位、位数计数、去除末尾零、按 limb 的除法,以及带各目标平台阈值的乘法和除法算法。每个函数都是其参数的精确函数。本包不引发任何条件,也没有舍入模式、上下文或标志的概念;这些都属于两个十进制包各自的、与标准相关的最终化例程。它是内部包:Luna-Flow/floating 之外的代码不能导入它,两个十进制包也都不公开它的类型。参见教程和设计页。

导入

在模块内部,将该包添加到 moon.pkg 中:

import {
  "Luna-Flow/floating/internal/decimal_kernel" @decimal_kernel,
}

然后把包用到的名字一次性引入作用域,例如放在单独的文件里:

///|
using @decimal_kernel {
  type DecCoeff,
  dec_coeff_from_int,
  dec_coeff_magnitude_add_repr,
}

本手册的示例检查器没有内部子包的别名,所以这些示例标为 nocheck。

系数类型

DecCoeff

一个非负整数,要么是用于小于 101810^{18} 的值的 Small(UInt64),要么是 Limbs(units, digits):以 10910^9 为基数的小端 limb,没有前导零 limb,并缓存位数。该值是持久的,没有任何函数会修改操作数的 limb。构造器是公开的,因为两个十进制包在少数快速路径中会对它们做模式匹配;请优先使用下面的函数。

DecCoeffRounding

八种舍入方向(HalfEven、HalfUp、HalfDown、Down、Ceiling、Floor、Up、ZeroFiveUp),表示为一个普通枚举。它是一个方向,而不是状态:每个十进制包用一个全函数的 match 把自己的舍入模式类型转换成它,舍入族中的函数只回答保留的数值是否在最后一位上增加一个单位。

SignedDecCoeff

符号加一个 DecCoeff 绝对值,用于中间值可能为负的算法(Toom-3、Newton 倒数)。零永远不是负的。该类型是 pub:字段可以读取,但值只能由本包构造。

函数族

所有函数都以 dec_coeff_* 命名;没有这个前缀的是 remove_factor10_repr、exact_divide_by_power_of_ten_repr 和 trim_trailing_decimal_zeros_repr。

族函数
构造与转换dec_coeff_zero, dec_coeff_from_int, dec_coeff_from_nonnegative_int, dec_coeff_from_int_magnitude, dec_coeff_from_decimal_digits_repr, dec_coeff_from_bigint_repr, dec_coeff_to_bigint_repr, DecCoeff::to_string, dec_coeff_from_unit_accumulator, DecCoeff::to_bigint
形态查询DecCoeff::digits10, DecCoeff::is_zero, dec_coeff_unit_len, dec_coeff_limb_at, dec_coeff_is_odd, dec_coeff_copy_limbs, dec_coeff_small_value, dec_coeff_matches_unit_repr, dec_coeff_slice, dec_coeff_small_digits
加、减、比较dec_coeff_magnitude_add_repr, dec_coeff_magnitude_sub_repr, dec_coeff_magnitude_add_unit, dec_coeff_magnitude_sub_unit, dec_coeff_signed_add_repr, dec_coeff_compare_repr, dec_coeff_compare_scaled_repr, dec_coeff_compare_half_power10, dec_coeff_unit_add_sub
十的幂与去零dec_coeff_shift_power10_repr, dec_coeff_power10_repr, remove_factor10_repr, exact_divide_by_power_of_ten_repr, trim_trailing_decimal_zeros_repr
乘法dec_coeff_multiply_repr, dec_coeff_square_repr, dec_coeff_multiply_by_unit_repr
除法dec_coeff_div_rem_repr, dec_coeff_mod_repr, dec_coeff_div_rem_by_unit_repr, dec_coeff_div_rem_knuth_repr, dec_coeff_div_rem_burnikel_ziegler, dec_coeff_div_rem_newton_try
数论dec_coeff_gcd_repr, dec_coeff_count_factor_unit_repr, dec_coeff_pow_unit_repr, dec_coeff_exact_power_repr, dec_coeff_exact_power_if_fits_repr
舍入决策DecCoeffRounding, dec_coeff_should_increment, dec_coeff_round_div, dec_coeff_round_div_bigint, dec_coeff_round_div_pow10, dec_coeff_round_div_pow10_owned, dec_coeff_round_accumulator, dec_coeff_round_pow10_coeff, dec_coeff_discards_nonzero, dec_coeff_accumulator_digits, dec_coeff_accumulator_discards_nonzero, dec_coeff_round_to_digits, dec_coeff_huge_shift_rounded_unit, dec_coeff_sqrt_should_increment, dec_coeff_addend_rounding_step, dec_coeff_round_div_expansion, dec_coeff_div_scaled_inexact
精确辅助函数compare_finite_magnitude_repr, dec_coeff_integer_sqrt_rem_repr, dec_coeff_floor_nth_root_repr, dec_coeff_exact_nth_root_repr, has_finite_decimal_expansion_repr, decimal_ln_atanh_scaled_coeff, decimal_round_scaled_coeff, decimal_floor_div_scaled_repr, dec_coeff_shift_power10_mod_wide_repr, dec_coeff_to_bounded_int, is_power_of_ten_coeff, decimal_small_pow10, dec_coeff_shift_power10_mod_repr
逐位逻辑运算logical_binary_digits_repr, coefficient_shift_digits_repr, coefficient_rotate_digits_repr, logical_coeff_digits_lsd, logical_coeff_from_digits_lsd
带符号值SignedDecCoeff, SignedDecCoeff::add, SignedDecCoeff::neg, SignedDecCoeff::sub, dec_coeff_signed_compare_int_repr, dec_coeff_signed_step_unit_repr, signed_dec_coeff, signed_dec_coeff_from_int_product
阈值与常量dec_coeff_base, dec_coeff_burnikel_ziegler_threshold, dec_coeff_burnikel_ziegler_threshold_16k, dec_coeff_burnikel_ziegler_threshold_8k, dec_coeff_burnikel_ziegler_threshold_large, dec_coeff_dpun, dec_coeff_karatsuba_square_threshold, dec_coeff_karatsuba_threshold, dec_coeff_newton_enabled, dec_coeff_newton_threshold, dec_coeff_ntt_mul_threshold, dec_coeff_ntt_mul_threshold_16k, dec_coeff_ntt_mul_threshold_32k, dec_coeff_ntt_mul_threshold_64k, dec_coeff_ntt_mul_threshold_large, dec_coeff_ntt_square_threshold, dec_coeff_ntt_square_threshold_16k, dec_coeff_ntt_square_threshold_32k, dec_coeff_ntt_square_threshold_64k, dec_coeff_ntt_square_threshold_8k, dec_coeff_ntt_square_threshold_large, dec_coeff_ntt_square_toom3_threshold, dec_coeff_ntt_square_toom3_threshold_16k, dec_coeff_ntt_square_toom3_threshold_32k, dec_coeff_ntt_square_toom3_threshold_64k, dec_coeff_ntt_square_toom3_threshold_8k, dec_coeff_ntt_square_toom3_threshold_large, dec_coeff_small_limit, dec_coeff_small_powers10, dec_coeff_toom3_threshold,在各目标之间不同的值按目标选取,见 coeff_thresholds_<target>.mbt

前置条件用 abort 检查(负的移位量、零除数、会降到零以下的减法、超出 [1,109)[1, 10^9) 的单 limb 除数):它们是调用者的编程错误,绝不是某个运算的条件。

完整公共接口

此快照是该包生成的 pkg.generated.mbti。当正文与接口不一致时,以接口为准。

// Generated using `moon info`, DON'T EDIT IT
package "Luna-Flow/floating/internal/decimal_kernel"

import {
  "moonbitlang/core/bigint",
  "moonbitlang/core/debug",
}

// Values
pub fn coefficient_rotate_digits_repr(DecCoeff, Int32, Int32) -> DecCoeff

pub fn coefficient_shift_digits_repr(DecCoeff, Int32, Int32) -> DecCoeff

pub fn compare_finite_magnitude_repr(DecCoeff, Int32, DecCoeff, Int32) -> Int32

pub fn dec_coeff_accumulator_digits(Array[UInt32], Int32) -> Int32

pub fn dec_coeff_accumulator_discards_nonzero(Array[UInt32], Int32) -> Bool

pub fn dec_coeff_addend_rounding_step(Bool, Bool, Bool, Bool, Int32, DecCoeffRounding) -> Int32

pub let dec_coeff_base : Int32

pub let dec_coeff_burnikel_ziegler_threshold : Int32

pub let dec_coeff_burnikel_ziegler_threshold_16k : Int32

pub let dec_coeff_burnikel_ziegler_threshold_8k : Int32

pub let dec_coeff_burnikel_ziegler_threshold_large : Int32

pub fn dec_coeff_compare_half_power10(DecCoeff) -> Int32

pub fn dec_coeff_compare_repr(DecCoeff, DecCoeff) -> Int32

pub fn dec_coeff_compare_scaled_repr(DecCoeff, DecCoeff, Int32) -> Int32

pub fn dec_coeff_copy_limbs(DecCoeff) -> Array[UInt32]

pub fn dec_coeff_count_factor_unit_repr(DecCoeff, Int32) -> (DecCoeff, Int32)

pub fn dec_coeff_discards_nonzero(DecCoeff, Int32) -> Bool

pub fn dec_coeff_div_rem_burnikel_ziegler(DecCoeff, DecCoeff) -> (DecCoeff, DecCoeff)

pub fn dec_coeff_div_rem_by_unit_repr(DecCoeff, Int32) -> (DecCoeff, Int32)

pub fn dec_coeff_div_rem_knuth_repr(DecCoeff, DecCoeff) -> (DecCoeff, DecCoeff)

pub fn dec_coeff_div_rem_newton_try(DecCoeff, DecCoeff) -> (DecCoeff, DecCoeff)?

pub fn dec_coeff_div_rem_repr(DecCoeff, DecCoeff) -> (DecCoeff, DecCoeff)

pub fn dec_coeff_div_scaled_inexact(DecCoeff, Int32, DecCoeff) -> DecCoeff

pub let dec_coeff_dpun : Int32

pub fn dec_coeff_exact_nth_root_repr(DecCoeff, Int32) -> DecCoeff?

pub fn dec_coeff_exact_power_if_fits_repr(DecCoeff, Int32, Int32) -> DecCoeff?

pub fn dec_coeff_exact_power_repr(DecCoeff, Int32) -> DecCoeff

pub fn dec_coeff_floor_nth_root_repr(DecCoeff, Int32) -> DecCoeff

pub fn dec_coeff_from_bigint_repr(@bigint.BigInt) -> DecCoeff

pub fn dec_coeff_from_decimal_digits_repr(String) -> DecCoeff?

pub fn dec_coeff_from_int(Int32) -> DecCoeff

pub fn dec_coeff_from_int_magnitude(Int32) -> DecCoeff

pub fn dec_coeff_from_nonnegative_int(Int32) -> DecCoeff

pub fn dec_coeff_from_unit_accumulator(Array[UInt32], Int32) -> DecCoeff

pub fn dec_coeff_gcd_repr(DecCoeff, DecCoeff) -> DecCoeff

pub fn dec_coeff_huge_shift_rounded_unit(DecCoeff, Bool, DecCoeffRounding) -> DecCoeff

pub fn dec_coeff_integer_sqrt_rem_repr(DecCoeff) -> (DecCoeff, DecCoeff)

pub fn dec_coeff_is_odd(DecCoeff) -> Bool

pub let dec_coeff_karatsuba_square_threshold : Int32

pub let dec_coeff_karatsuba_threshold : Int32

pub fn dec_coeff_limb_at(DecCoeff, Int32) -> UInt32

pub fn dec_coeff_magnitude_add_repr(DecCoeff, DecCoeff) -> DecCoeff

pub fn dec_coeff_magnitude_add_unit(DecCoeff, Int32) -> DecCoeff

pub fn dec_coeff_magnitude_sub_repr(DecCoeff, DecCoeff) -> DecCoeff

pub fn dec_coeff_magnitude_sub_unit(DecCoeff, Int32) -> DecCoeff

pub fn dec_coeff_matches_unit_repr(DecCoeff, Int32) -> Bool

pub fn dec_coeff_mod_repr(DecCoeff, DecCoeff) -> DecCoeff

pub fn dec_coeff_mod_unit(DecCoeff, Int32) -> Int32

pub fn dec_coeff_multiply_by_unit_repr(DecCoeff, Int32) -> DecCoeff

pub fn dec_coeff_multiply_repr(DecCoeff, DecCoeff) -> DecCoeff

pub let dec_coeff_newton_enabled : Bool

pub let dec_coeff_newton_threshold : Int32

pub let dec_coeff_ntt_mul_threshold : Int32

pub let dec_coeff_ntt_mul_threshold_16k : Int32

pub let dec_coeff_ntt_mul_threshold_32k : Int32

pub let dec_coeff_ntt_mul_threshold_64k : Int32

pub let dec_coeff_ntt_mul_threshold_large : Int32

pub let dec_coeff_ntt_square_threshold : Int32

pub let dec_coeff_ntt_square_threshold_16k : Int32

pub let dec_coeff_ntt_square_threshold_32k : Int32

pub let dec_coeff_ntt_square_threshold_64k : Int32

pub let dec_coeff_ntt_square_threshold_8k : Int32

pub let dec_coeff_ntt_square_threshold_large : Int32

pub let dec_coeff_ntt_square_toom3_threshold : Int32

pub let dec_coeff_ntt_square_toom3_threshold_16k : Int32

pub let dec_coeff_ntt_square_toom3_threshold_32k : Int32

pub let dec_coeff_ntt_square_toom3_threshold_64k : Int32

pub let dec_coeff_ntt_square_toom3_threshold_8k : Int32

pub let dec_coeff_ntt_square_toom3_threshold_large : Int32

pub fn dec_coeff_pow_unit_repr(Int32, Int32) -> DecCoeff

pub fn dec_coeff_power10_repr(Int32) -> DecCoeff

pub fn dec_coeff_round_accumulator(Array[UInt32], Int32, Int32, Bool, DecCoeffRounding) -> DecCoeff

pub fn dec_coeff_round_div(DecCoeff, DecCoeff, Bool, DecCoeffRounding) -> DecCoeff

pub fn dec_coeff_round_div_bigint(@bigint.BigInt, @bigint.BigInt, Bool, DecCoeffRounding) -> @bigint.BigInt

pub fn dec_coeff_round_div_expansion(DecCoeff, DecCoeff, Bool, DecCoeffRounding) -> (DecCoeff, Bool)

pub fn dec_coeff_round_div_pow10(DecCoeff, Int32, Bool, DecCoeffRounding) -> (DecCoeff, Bool)

pub fn dec_coeff_round_div_pow10_owned(Array[UInt32], Int32, Int32, Bool, DecCoeffRounding) -> (DecCoeff, Bool)

pub fn dec_coeff_round_pow10_coeff(DecCoeff, Int32, Bool, DecCoeffRounding) -> DecCoeff

pub fn dec_coeff_round_to_digits(DecCoeff, Int32, Int32, Bool, DecCoeffRounding) -> (DecCoeff, Int32, Bool, Bool)

pub fn dec_coeff_shift_power10_mod_repr(DecCoeff, Int32, DecCoeff) -> DecCoeff

pub fn dec_coeff_shift_power10_mod_wide_repr(DecCoeff, Int64, DecCoeff) -> DecCoeff

pub fn dec_coeff_shift_power10_repr(DecCoeff, Int32) -> DecCoeff

pub fn dec_coeff_should_increment(DecCoeff, DecCoeff, DecCoeff, Bool, DecCoeffRounding) -> Bool

pub fn dec_coeff_signed_add_repr(Bool, DecCoeff, Bool, DecCoeff) -> (Bool, DecCoeff)

pub fn dec_coeff_signed_compare_int_repr(Bool, DecCoeff, Int32) -> Int32

pub fn dec_coeff_signed_step_unit_repr(Bool, DecCoeff, Bool) -> (Bool, DecCoeff)

pub fn dec_coeff_slice(DecCoeff, Int32, Int32) -> DecCoeff

pub fn dec_coeff_small_digits(UInt64) -> Int32

pub let dec_coeff_small_limit : UInt64

pub let dec_coeff_small_powers10 : Array[UInt64]

pub fn dec_coeff_small_value(DecCoeff) -> UInt64?

pub fn dec_coeff_sqrt_rounding(DecCoeff) -> (DecCoeff, DecSqrtRemainder)

pub fn dec_coeff_sqrt_should_increment(DecCoeff, Int32, Bool, DecCoeffRounding) -> Bool

pub fn dec_coeff_square_repr(DecCoeff) -> DecCoeff

pub fn dec_coeff_to_bigint_repr(DecCoeff) -> @bigint.BigInt

pub fn dec_coeff_to_bounded_int(DecCoeff, Int32) -> Int32?

pub let dec_coeff_toom3_threshold : Int32

pub fn dec_coeff_unit_add_sub(Array[UInt32], Int32, Array[UInt32], Int32, Int32, Array[UInt32], Int32) -> Int32

pub fn dec_coeff_unit_len(DecCoeff) -> Int32

pub fn dec_coeff_zero() -> DecCoeff

pub fn decimal_floor_div_scaled_repr(Bool, DecCoeff, DecCoeff) -> Int32

pub fn decimal_ln_atanh_scaled_coeff(DecCoeff, DecCoeff, Int32) -> SignedDecCoeff

pub fn decimal_round_scaled_coeff(SignedDecCoeff, Int32) -> SignedDecCoeff

pub fn decimal_small_pow10(Int32) -> Int32

pub fn exact_divide_by_power_of_ten_repr(DecCoeff, Int32) -> DecCoeff?

pub fn has_finite_decimal_expansion_repr(DecCoeff, DecCoeff) -> Bool

pub fn is_power_of_ten_coeff(DecCoeff) -> Bool

pub fn logical_binary_digits_repr(DecCoeff, DecCoeff, Int32, (Bool, Bool) -> Bool) -> DecCoeff

pub fn logical_coeff_digits_lsd(DecCoeff, Int32) -> Array[Int32]

pub fn logical_coeff_from_digits_lsd(Array[Int32]) -> DecCoeff

pub fn remove_factor10_repr(DecCoeff, Int32) -> (DecCoeff, Int32)

pub fn signed_dec_coeff(Bool, DecCoeff) -> SignedDecCoeff

pub fn signed_dec_coeff_from_int_product(Int32, DecCoeff) -> SignedDecCoeff

pub fn trim_trailing_decimal_zeros_repr(DecCoeff, Int32, max_drop? : Int32) -> (DecCoeff, Int32, Int32)

// Errors

// Types and methods
#derive(@debug.Debug)
pub(all) enum DecCoeff {
  Small(UInt64)
  Limbs(Array[UInt32], Int32)
}
pub fn DecCoeff::digits10(Self) -> Int32
pub fn DecCoeff::is_zero(Self) -> Bool
pub fn DecCoeff::to_bigint(Self) -> @bigint.BigInt
pub fn DecCoeff::to_string(Self) -> String

#derive(Eq)
pub(all) enum DecCoeffRounding {
  HalfEven
  HalfUp
  HalfDown
  Down
  Ceiling
  Floor
  Up
  ZeroFiveUp
}

#derive(Eq, @debug.Debug)
pub(all) enum DecSqrtRemainder {
  Zero
  UpToRoot
  AboveRoot
}

pub struct SignedDecCoeff {
  negative : Bool
  magnitude : DecCoeff
}
pub fn SignedDecCoeff::add(Self, Self) -> Self
pub fn SignedDecCoeff::neg(Self) -> Self
pub fn SignedDecCoeff::sub(Self, Self) -> Self

// Type aliases

// Traits