5D Library › 四柱排盘的历法计算与规则引擎 · English
第六章 两个排盘引擎与一套测试向量
同一个出生,两个引擎
前五章的每一个约定,在一个排盘软件里都是一行代码:用钟表时间还是真太阳时,子时怎样换日,节气时刻从哪里来,藏干怎样计权。两个软件只要有一行不同,对同一个出生就可能排出不同的盘。下面的程序比较两个引擎:引擎 X 读钟表时间、23 点换日(第二章的约定 A);引擎 Y 换算真太阳时、零点换日并按晚子时起时干(约定 B)。年柱与月柱只取决于出生的时刻,两个引擎相同。把 2026 年的每一分钟都当作同样可能的出生时间,逐分钟比较。
推导
# Chapter 6. Two engines, one birth: how often do the pillars differ?
# Engine X reads the clock (UTC+8) and changes the day at 23:00 (rule A
# of p03). Engine Y converts to true solar time (longitude and the
# equation of time, as in p04) and changes the day at midnight with the
# late-zi stem rule (rule B). Year and month pillars depend only on the
# instant of birth and are the same in both. Every minute of 2026 is
# taken as an equally likely birth time.
import math
CITIES = (("Shanghai", 121.47), ("Beijing", 116.40),
("Singapore", 103.82), ("Kuala Lumpur", 101.69))
JDN_2026 = 2461042 # 1 January 2026
def equation_of_time(n):
g = 2 * math.pi / 365 * (n - 1)
return 229.18 * (0.000075 + 0.001868 * math.cos(g)
- 0.032077 * math.sin(g) - 0.014615 * math.cos(2 * g)
- 0.040849 * math.sin(2 * g))
def pillars(day_idx, minute, rule):
hb = ((minute + 60) // 120) % 12
late = minute >= 23 * 60
d = day_idx + (1 if late and rule == "A" else 0)
stem_day = day_idx + (1 if late else 0)
return d % 60, (2 * (stem_day % 10 % 5) + hb) % 10, hb
if __name__ == "__main__":
print(" city day pillar hour pillar any pillar")
for name, lon in CITIES:
day = hour = anyp = total = 0
for n in range(365):
di = (JDN_2026 + n + 49) % 60
off = 4 * (lon - 120) + equation_of_time(n + 1)
for t in range(1440):
x = pillars(di, t, "A")
ts = t + off
dj = di
if ts < 0:
ts, dj = ts + 1440, di - 1
elif ts >= 1440:
ts, dj = ts - 1440, di + 1
y = pillars(dj, int(ts), "B")
day += x[0] != y[0]
hour += x[1:] != y[1:]
anyp += x != y
total += 1
print(" {:12s} {:8.4f} {:8.4f} {:8.4f}".format(
name, day / total, hour / total, anyp / total))
print("\nReading: in Shanghai the two engines disagree on about one")
print("birth in ten; in Kuala Lumpur and Singapore on more than half.")
print("Most disagreements are in the hour pillar; the day pillar")
print("differs for four to nine births in a hundred, around midnight.")
# city day pillar hour pillar any pillar
# Shanghai 0.0379 0.0674 0.1043
# Beijing 0.0520 0.1252 0.1668
# Singapore 0.0869 0.5432 0.5801
# Kuala Lumpur 0.0929 0.6143 0.6463
#
# Reading: in Shanghai the two engines disagree on about one
# birth in ten; in Kuala Lumpur and Singapore on more than half.
# Most disagreements are in the hour pillar; the day pillar
# differs for four to nine births in a hundred, around midnight.在上海,两个引擎有 0.1043 的出生至少一柱不同,其中时柱不同 0.0674,日柱不同 0.0379;在北京,至少一柱不同的比例为 0.1668。在新加坡与吉隆坡,这一比例分别为 0.5801 与 0.6463。分歧主要在时柱;日柱的分歧集中在午夜前后,占四到九个百分点。
所以当用户说「两个软件排出来不一样」时,最可能的原因不是任何一方出了错,而是两个引擎在第二章的约定上取法不同。这类差异可以在软件中写明,也可以让用户自选。
一套测试向量
一个工程上的办法是给排盘引擎一套公开的测试向量:一些必须满足的不变式,以及一张标准表的指纹。两个引擎若对标准表算出同一个指纹,就对表中的每一项一致;指纹不同,说明应当先找约定上的差别,再找程序错误。
推导
# Chapter 6. A conformance check that any two engines can run.
# Invariants that every correct day-and-hour engine must satisfy, checked
# on every day from 1900 to 2100, and a fingerprint (SHA-256) of a
# canonical table: day and hour pillars for 00:30, 12:00 and 23:30 of
# every day of 2026, under rules A and B of p03. Two engines that print
# the same fingerprint agree on all 2,190 entries.
import datetime as dt
import hashlib
STEMS = ("jia", "yi", "bing", "ding", "wu", "ji", "geng", "xin", "ren",
"gui")
BRANCHES = ("zi", "chou", "yin", "mao", "chen", "si", "wu", "wei", "shen",
"you", "xu", "hai")
def day_index(d):
return (d.toordinal() + 1721425 + 49) % 60
def pillars(d, minute, rule):
di = day_index(d)
hb = ((minute + 60) // 120) % 12
late = minute >= 23 * 60
dd = di + (1 if late and rule == "A" else 0)
sd = di + (1 if late else 0)
hs = (2 * (sd % 10 % 5) + hb) % 10
return "{}-{} {}-{}".format(STEMS[dd % 10], BRANCHES[dd % 12],
STEMS[hs], BRANCHES[hb])
if __name__ == "__main__":
days = [dt.date(1900, 1, 1) + dt.timedelta(k) for k in range(73414)]
step = all((day_index(b) - day_index(a)) % 60 == 1
for a, b in zip(days, days[1:]))
five = all(pillars(a, 600, "A").split()[1]
== pillars(b, 600, "A").split()[1]
for a, b in zip(days, days[5:]))
jiazi = all((day_index(d) % 10) % 5 == 0
for d in days if pillars(d, 30, "B").endswith("jia-zi"))
print(" day index rises by 1 (mod 60) every day, 1900-2100: {}".format(
step))
print(" hour pillars repeat every 5 days: {}".format(five))
print(" a jia-zi hour falls only on jia or ji days: {}".format(jiazi))
rows = []
for k in range(365):
d = dt.date(2026, 1, 1) + dt.timedelta(k)
for minute in (30, 720, 1410):
for rule in "AB":
rows.append("{} {:04d} {} {}".format(d.isoformat(), minute,
rule, pillars(d, minute,
rule)))
digest = hashlib.sha256("\n".join(rows).encode()).hexdigest()
print(" canonical table: {:,d} entries".format(len(rows)))
print(" first: {}".format(rows[0]))
print(" last: {}".format(rows[-1]))
print(" SHA-256 {}".format(digest[:32]))
print(" {}".format(digest[32:]))
print("\nReading: two engines that print the same fingerprint agree on")
print("every entry of the table; a different fingerprint says to look")
print("for a convention difference before looking for a bug.")
# day index rises by 1 (mod 60) every day, 1900-2100: True
# hour pillars repeat every 5 days: True
# a jia-zi hour falls only on jia or ji days: True
# canonical table: 2,190 entries
# first: 2026-01-01 0030 A yi-hai bing-zi
# last: 2026-12-31 1410 B ji-mao bing-zi
# SHA-256 91474ec7886aebba0086dbb2511a2456
# 82e96813b80dc902a7800e32084b66b9
#
# Reading: two engines that print the same fingerprint agree on
# every entry of the table; a different fingerprint says to look
# for a convention difference before looking for a bug.程序检查了三个不变式:1900 年到 2100 年的每一天,日柱序号恰好加一;时柱每五天重复一次(60 个时辰为一周);甲子时只出现在甲日与己日。然后对 2026 年每一天的零点 30 分、正午与 23 点 30 分,按约定 A 与 B 各排一次日柱与时柱,共 2,190 项,算出它们的 SHA-256 指纹。任何引擎都可以用同样的格式输出这张表并比较指纹。
注
测试向量的范围。 这张表只覆盖日柱与时柱,因为它们是纯算术;年柱与月柱依赖节气时刻,应当另用天文台历书的节气时刻做一张表。真太阳时换算依赖经度与均时差公式,也应当单列,并写明所用的公式。
小结
一个读钟表时间、23 点换日的引擎,与一个用真太阳时、零点换日的引擎,在上海对 0.1043 的出生至少一柱不同,在吉隆坡对 0.6463 的出生不同。分歧来自约定,而不是错误。一套公开的不变式与标准表指纹,可以让任何两个引擎在几秒钟内核对彼此。
Fan, Y. (2026). Four-Pillar Calendrical Computation and Formal Rule Engines: Julian-Day Sexagenary Cycles, Midnight-Boundary Disagreements, Birth-Time Sensitivity Bounds and Weighted-Graph Strength Scoring [四柱排盘的历法计算与规则引擎] (Version 1.0.0). 5D Library. https://doi.org/10.5281/zenodo.24090180 · Licensed under Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0).