refactor(includes): string.vp 双 double 辅助函数注解统一为内建 double
t.CDouble 与内建 double 等价(map_t_type: CDouble -> f64),_dd_split_hi/_dd_mul/_dd_add/_dd_div/_dd_from_u64/_dd_pow10/_pow2/_atof_scale_stepwise/atof 全量改写为内建名,消除混用。
This commit is contained in:
1 parent
e30c23fe39
commit
ec6c19481a
1 file changed
+46
-46
@@ -332,8 +332,8 @@ def atoll(src: str) -> t.CInt64T:
|
||||
# 含量级缩尺:|a| > 6.7e292 时先乘 2^-28 再算(否则 a*(2^27+1) 溢出为 inf);
|
||||
# |a| 极小(< 1e-292)时先乘 2^28(否则 lo = a-hi 掉进次正规丢位)。
|
||||
# 缩尺因子均为 2 的幂 ⇒ 对正规数**无损**;调用方用 lo = a - hi 取下半部分。
|
||||
def _dd_split_hi(a: t.CDouble) -> t.CDouble:
|
||||
aa: t.CDouble = a
|
||||
def _dd_split_hi(a: double) -> double:
|
||||
aa: double = a
|
||||
big: int = 0
|
||||
small: int = 0
|
||||
if a > 6.7e292 or a < -6.7e292:
|
||||
@@ -342,8 +342,8 @@ def _dd_split_hi(a: t.CDouble) -> t.CDouble:
|
||||
elif (a > 0.0 and a < 1.0e-292) or (a < 0.0 and a > -1.0e-292):
|
||||
aa = a * 268435456.0
|
||||
small = 1
|
||||
tt: t.CDouble = aa * 134217729.0
|
||||
hi: t.CDouble = tt - (tt - aa)
|
||||
tt: double = aa * 134217729.0
|
||||
hi: double = tt - (tt - aa)
|
||||
if big != 0:
|
||||
return hi * 268435456.0
|
||||
if small != 0:
|
||||
@@ -352,56 +352,56 @@ def _dd_split_hi(a: t.CDouble) -> t.CDouble:
|
||||
|
||||
|
||||
# _dd_mul - 双 double 乘法:out = a × b(out[0]=hi, out[1]=lo)
|
||||
def _dd_mul(ah: t.CDouble, al: t.CDouble, bh: t.CDouble, bl: t.CDouble,
|
||||
out: t.CArray[t.CDouble, 2]):
|
||||
p: t.CDouble = ah * bh
|
||||
a_hi: t.CDouble = _dd_split_hi(ah)
|
||||
a_lo: t.CDouble = ah - a_hi
|
||||
b_hi: t.CDouble = _dd_split_hi(bh)
|
||||
b_lo: t.CDouble = bh - b_hi
|
||||
e: t.CDouble = ((a_hi * b_hi - p) + a_hi * b_lo + a_lo * b_hi) + a_lo * b_lo
|
||||
def _dd_mul(ah: double, al: double, bh: double, bl: double,
|
||||
out: t.CArray[double, 2]):
|
||||
p: double = ah * bh
|
||||
a_hi: double = _dd_split_hi(ah)
|
||||
a_lo: double = ah - a_hi
|
||||
b_hi: double = _dd_split_hi(bh)
|
||||
b_lo: double = bh - b_hi
|
||||
e: double = ((a_hi * b_hi - p) + a_hi * b_lo + a_lo * b_hi) + a_lo * b_lo
|
||||
e = e + (ah * bl + al * bh)
|
||||
s: t.CDouble = p + e
|
||||
s: double = p + e
|
||||
out[0] = s
|
||||
out[1] = e - (s - p)
|
||||
|
||||
|
||||
# _dd_add - 双 double 加法:out = a + b
|
||||
def _dd_add(ah: t.CDouble, al: t.CDouble, bh: t.CDouble, bl: t.CDouble,
|
||||
out: t.CArray[t.CDouble, 2]):
|
||||
s: t.CDouble = ah + bh
|
||||
bb: t.CDouble = s - ah
|
||||
e: t.CDouble = (ah - (s - bb)) + (bh - bb)
|
||||
def _dd_add(ah: double, al: double, bh: double, bl: double,
|
||||
out: t.CArray[double, 2]):
|
||||
s: double = ah + bh
|
||||
bb: double = s - ah
|
||||
e: double = (ah - (s - bb)) + (bh - bb)
|
||||
e = e + (al + bl)
|
||||
r0: t.CDouble = s + e
|
||||
r0: double = s + e
|
||||
out[0] = r0
|
||||
out[1] = e - (r0 - s)
|
||||
|
||||
|
||||
# _dd_div - 双 double 除法:out = a / b(q0 + 一步残差修正)
|
||||
def _dd_div(ah: t.CDouble, al: t.CDouble, bh: t.CDouble, bl: t.CDouble,
|
||||
out: t.CArray[t.CDouble, 2], tmp: t.CArray[t.CDouble, 2]):
|
||||
q0: t.CDouble = ah / bh
|
||||
def _dd_div(ah: double, al: double, bh: double, bl: double,
|
||||
out: t.CArray[double, 2], tmp: t.CArray[double, 2]):
|
||||
q0: double = ah / bh
|
||||
_dd_mul(bh, bl, q0, 0.0, tmp)
|
||||
_dd_add(ah, al, -tmp[0], -tmp[1], tmp)
|
||||
q1: t.CDouble = tmp[0] / bh
|
||||
q1: double = tmp[0] / bh
|
||||
_dd_add(q0, 0.0, q1, 0.0, out)
|
||||
|
||||
|
||||
# _dd_from_u64 - u64 → 双 double(精确:hi 为四舍五入值,lo 为精确残差)
|
||||
def _dd_from_u64(m: t.CUInt64T, out: t.CArray[t.CDouble, 2]):
|
||||
hi: t.CDouble = t.CDouble(m)
|
||||
lo: t.CDouble = 0.0
|
||||
def _dd_from_u64(m: t.CUInt64T, out: t.CArray[double, 2]):
|
||||
hi: double = double(m)
|
||||
lo: double = 0.0
|
||||
if m >= t.CUInt64T(9007199254740992):
|
||||
lo = t.CDouble(m - t.CUInt64T(hi))
|
||||
s: t.CDouble = hi + lo
|
||||
bb: t.CDouble = s - hi
|
||||
lo = double(m - t.CUInt64T(hi))
|
||||
s: double = hi + lo
|
||||
bb: double = s - hi
|
||||
out[0] = s
|
||||
out[1] = (hi - (s - bb)) + (lo - bb)
|
||||
|
||||
|
||||
# _dd_pow10 - 10^e 的双 double(e >= 0;按 ≤22 分块,每块可精确表示)
|
||||
def _dd_pow10(e: int, out: t.CArray[t.CDouble, 2], tmp: t.CArray[t.CDouble, 2]):
|
||||
def _dd_pow10(e: int, out: t.CArray[double, 2], tmp: t.CArray[double, 2]):
|
||||
out[0] = 1.0
|
||||
out[1] = 0.0
|
||||
k: int = e
|
||||
@@ -409,7 +409,7 @@ def _dd_pow10(e: int, out: t.CArray[t.CDouble, 2], tmp: t.CArray[t.CDouble, 2]):
|
||||
step: int = 22
|
||||
if k < 22:
|
||||
step = k
|
||||
p10: t.CDouble = 1.0
|
||||
p10: double = 1.0
|
||||
i: int = 0
|
||||
while i < step:
|
||||
p10 = p10 * 10.0
|
||||
@@ -421,8 +421,8 @@ def _dd_pow10(e: int, out: t.CArray[t.CDouble, 2], tmp: t.CArray[t.CDouble, 2]):
|
||||
|
||||
|
||||
# _pow2 - 2^n(|n| <= 1000):循环乘 2/0.5,每步精确
|
||||
def _pow2(n: int) -> t.CDouble:
|
||||
r: t.CDouble = 1.0
|
||||
def _pow2(n: int) -> double:
|
||||
r: double = 1.0
|
||||
i: int = n
|
||||
if i >= 0:
|
||||
while i > 0:
|
||||
@@ -436,8 +436,8 @@ def _pow2(n: int) -> t.CDouble:
|
||||
|
||||
|
||||
# _atof_scale_stepwise - 旧的逐步 ×/÷10 缩放(次正规/溢出场景的兜底)
|
||||
def _atof_scale_stepwise(x: t.CDouble, p: int) -> t.CDouble:
|
||||
r: t.CDouble = x
|
||||
def _atof_scale_stepwise(x: double, p: int) -> double:
|
||||
r: double = x
|
||||
e: int = p
|
||||
while e > 0:
|
||||
r = r * 10.0
|
||||
@@ -448,14 +448,14 @@ def _atof_scale_stepwise(x: t.CDouble, p: int) -> t.CDouble:
|
||||
return r
|
||||
|
||||
|
||||
def atof(src: str) -> t.CDouble:
|
||||
def atof(src: str) -> double:
|
||||
s: str = src
|
||||
if s == None: return 0.0
|
||||
# 跳过空白
|
||||
while s[0] == ' ' or s[0] == '\t' or s[0] == '\n' or s[0] == '\r':
|
||||
s += 1
|
||||
# 符号
|
||||
sign: t.CDouble = 1.0
|
||||
sign: double = 1.0
|
||||
if s[0] == '-':
|
||||
sign = -1.0
|
||||
s += 1
|
||||
@@ -523,14 +523,14 @@ def atof(src: str) -> t.CDouble:
|
||||
exp = exp * exp_sign
|
||||
p: int = dexp + exp
|
||||
if p == 0:
|
||||
return sign * t.CDouble(mant)
|
||||
return sign * double(mant)
|
||||
# 双 double 缩放:mant × 10^p,最后一次舍入
|
||||
ta: t.CArray[t.CDouble, 2]
|
||||
tb: t.CArray[t.CDouble, 2]
|
||||
tc: t.CArray[t.CDouble, 2]
|
||||
tr: t.CArray[t.CDouble, 2]
|
||||
ta: t.CArray[double, 2]
|
||||
tb: t.CArray[double, 2]
|
||||
tc: t.CArray[double, 2]
|
||||
tr: t.CArray[double, 2]
|
||||
_dd_from_u64(mant, ta)
|
||||
v: t.CDouble = 0.0
|
||||
v: double = 0.0
|
||||
if p > 0:
|
||||
_dd_pow10(p, tb, tr)
|
||||
_dd_mul(ta[0], ta[1], tb[0], tb[1], tr)
|
||||
@@ -558,10 +558,10 @@ def atof(src: str) -> t.CDouble:
|
||||
and (v == 0.0 or v >= 2.2250738585072014e-308 or v <= -2.2250738585072014e-308):
|
||||
return sign * v
|
||||
# 溢出 / 次正规 / NaN → 回退旧逐步缩放(与旧实现语义一致)
|
||||
return sign * _atof_scale_stepwise(t.CDouble(mant), p)
|
||||
return sign * _atof_scale_stepwise(double(mant), p)
|
||||
|
||||
|
||||
def atof_out(src: str, out: t.CDouble | t.CPtr) -> int:
|
||||
def atof_out(src: str, out: double | t.CPtr) -> int:
|
||||
"""atof 结果写入 out[0](输出参数方式,避免跨模块 double 返回值传递 bug)
|
||||
|
||||
返回值仅表示成功与否(1=成功,0=失败),结果值通过 out 指针传递,
|
||||
@@ -569,7 +569,7 @@ def atof_out(src: str, out: t.CDouble | t.CPtr) -> int:
|
||||
"""
|
||||
if out is None:
|
||||
return 0
|
||||
v: t.CDouble = atof(src)
|
||||
v: double = atof(src)
|
||||
out[0] = v
|
||||
return 1
|
||||
|
||||
|
||||
Reference in new issue
Block a user