关于「精确算术:整数分、汇差科目与最大余数分摊」,《四柱复式守恒代数》给出的定量结论是什么?

结论:第四、五行是一个代购店会真实遇到的情形:一张订单售价 750.40 元,客户先付订金 300.10 元,再付尾款 450.30 元。用浮点数检查「订金加尾款等于售价」,结果是 False,因为浮点合计是 750.4000000000001。改用整数分,30010 加 45030 等于 75040,检查通过。

数据口径:本书用可运行的程序说明,单式流水本身不含任何检验:在一个示意日上漏记一笔 1,500.00 元的尾款,表格依旧自洽,只有拿银行余额来比才出现差额。 书中参数为示意;全部计算由书中程序生成;本书属于非个人化的会计、税务或法律建议。

English version

对照数据表

下表数字由本章 Python 程序原样打印,可重新运行复现。 程序只用 Python 标准库与结论吻合的打印值: 300.10, 30010, 450.30, 45030, 750.40, 750.4000000000001

stored 0.13602879701896397/36028797018963968
stored 0.1 - 1/101/180143985094819840
fsum([0.1, 0.2]) =0.3 -> False (0.30000000000000004)
deposit 300.10 + balance 450.30 == price 750.40 -> False
the float total is 750.4000000000001
30010 + 45030 =75040 in fen -> True
round(2.675, 2)2.67
Decimal half-up2.68
integer half-up of 2675 tenths-of-fen268 fen
amounts 0.00 to 999.9996000 of 100000 not stored exactly
查看完整验算程序
# Chapter 4. p41: why a ledger is kept in integer fen, not in floats.
# Model: binary floating point stores 0.1 as the nearest dyadic
# fraction. math.fsum adds the stored values exactly and rounds once,
# so it removes accumulation error but cannot undo the input error.
# A conservation test of the form "left == right" then fails on
# amounts that balance on paper. Integer fen (or Fraction) passes.
# Half-up rounding to fen is shown three ways: float round(), Decimal
# with ROUND_HALF_UP, and pure integer arithmetic on fen times 10.
from decimal import Decimal, ROUND_HALF_UP
from fractions import Fraction as F
import math


def half_up_fen(tenths_of_fen):
    """Round an integer count of 0.1 fen to whole fen, half away."""
    q, r = divmod(abs(tenths_of_fen), 10)
    q += r >= 5
    return q if tenths_of_fen >= 0 else -q


if __name__ == "__main__":
    print("stored 0.1 = {}".format(F(0.1)))
    print("stored 0.1 - 1/10 = {}".format(F(0.1) - F(1, 10)))
    a = math.fsum([0.1, 0.2])
    print("fsum([0.1, 0.2]) == 0.3 -> {}  ({!r})".format(a == 0.3, a))
    left = math.fsum([300.10, 450.30])
    print("deposit 300.10 + balance 450.30 == price 750.40 -> {}".format(
        left == 750.40))
    print("  the float total is {!r}".format(left))
    print("30010 + 45030 == 75040 in fen -> {}".format(
        30010 + 45030 == 75040))
    print("round(2.675, 2) = {}".format(round(2.675, 2)))
    d = Decimal("2.675").quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
    print("Decimal half-up = {}".format(d))
    print("integer half-up of 2675 tenths-of-fen = {} fen".format(
        half_up_fen(2675)))
    bad = 0
    for cents in range(0, 100000):
        x = cents / 100
        if F(x) != F(cents, 100):
            bad += 1
    print("amounts 0.00 to 999.99: {} of 100000 not stored exactly".format(
        bad))

唯一数据源与引用

范雅福(Fan, Y.). (2026). 《四柱结算与复式记账的守恒代数、代购流水零差错对账不变量母典》第 4 章「精确算术:整数分、汇差科目与最大余数分摊」(版本 1.0.0). 5D Library. DOI 待发布

DOI 待发布 · 专著页正式发布后上线(本站暂无专著页) · 5DL-B-318 · v1.0.0 · CC-BY-NC-ND-4.0

BibTeX
@book{fan2026318,
  author    = {Fan, Yafu},
  title     = {The Conservation Algebra of Four-Pillar Settlement and Double-Entry Bookkeeping: Invariants for Error-Free Reconciliation of Buying-Agent Ledgers},
  year      = {2026},
  publisher = {5D Library},
  edition   = {1.0.0},
  note      = {DOI pending}
}

本书其他章节结论