185 lines
6.6 KiB
Python
185 lines
6.6 KiB
Python
#!/usr/bin/env python3
|
|
"""BigValue: scientific-decimal big number for endless incremental growth.
|
|
|
|
Python port of clicker_heroes_rebuild/scripts/big_value.gd (the game-number core
|
|
that makes CH's endless 1e72000-scale progression possible). mantissa * 10^exponent
|
|
so values far beyond float64 (~1e308) are representable. Approximate, NOT
|
|
original-native numeric equivalence (same caveat as the CH source).
|
|
|
|
Ported core subset (game-growth operations): normalize, plus/minus/times/scale/
|
|
divided_by, power, pow_int, square_root, compare, floored/ceiled/rounded,
|
|
to_float, from_text/encoded (save), and int conversion. The source's exact
|
|
decimal-string rounding (_round_decimal/_decimal_midpoint) is NOT ported -- it
|
|
only affects float32 boundary parsing, not growth-curve arithmetic.
|
|
|
|
Mirrored by p2_godot_sample/scripts/big_value.gd (parity gated).
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import math
|
|
from typing import Union
|
|
|
|
LN10 = 2.302585092994045
|
|
FLOOR_BIAS = 1e-11
|
|
|
|
|
|
class BigValue:
|
|
__slots__ = ("mantissa", "exponent")
|
|
|
|
def __init__(self, value: float = 0.0, power: float = 0.0):
|
|
self.mantissa = float(value)
|
|
self.exponent = float(power)
|
|
self._normalize()
|
|
|
|
@staticmethod
|
|
def pair(value: float, power: float) -> "BigValue":
|
|
r = BigValue.__new__(BigValue)
|
|
r.mantissa = float(value)
|
|
r.exponent = float(power)
|
|
return r # deliberately NOT normalized (matches CH _pair)
|
|
|
|
def _normalize(self) -> None:
|
|
if math.isinf(self.mantissa) or math.isinf(self.exponent):
|
|
return
|
|
if self.mantissa == 0.0:
|
|
self.exponent = 0.0
|
|
return
|
|
m = abs(self.mantissa)
|
|
if m >= 10.0:
|
|
shift = int(math.floor(math.log(m) / LN10))
|
|
self.mantissa /= (10.0 ** shift)
|
|
self.exponent += shift
|
|
elif m < 1.0:
|
|
shift = -int(math.floor(math.log(m) / LN10))
|
|
self.mantissa *= (10.0 ** shift)
|
|
self.exponent -= shift
|
|
|
|
def plus(self, other: "BigValue") -> "BigValue":
|
|
delta = self.exponent - other.exponent
|
|
if delta > 10:
|
|
return BigValue.pair(self.mantissa, self.exponent)
|
|
if delta < -10:
|
|
return BigValue.pair(other.mantissa, other.exponent)
|
|
if delta == 0:
|
|
return BigValue(self.mantissa + other.mantissa, self.exponent)
|
|
if delta > 0:
|
|
scaled = self.mantissa * (10.0 ** delta)
|
|
return BigValue(other.mantissa + scaled, other.exponent)
|
|
scaled = other.mantissa * (10.0 ** -delta)
|
|
return BigValue(self.mantissa + scaled, self.exponent)
|
|
|
|
def minus(self, other: "BigValue") -> "BigValue":
|
|
return self.plus(BigValue.pair(-other.mantissa, other.exponent))
|
|
|
|
def times(self, other: "BigValue") -> "BigValue":
|
|
return BigValue(self.mantissa * other.mantissa, self.exponent + other.exponent)
|
|
|
|
def scale(self, value: float) -> "BigValue":
|
|
return self.times(BigValue(value))
|
|
|
|
def divided_by(self, other: "BigValue") -> "BigValue":
|
|
q = self.mantissa / other.mantissa
|
|
if q < 10000.0:
|
|
return BigValue(q * 10000.0, (self.exponent - 4) - other.exponent)
|
|
return BigValue(q, self.exponent - other.exponent)
|
|
|
|
@staticmethod
|
|
def power(base: float, degree: float) -> "BigValue":
|
|
if degree == 0.0:
|
|
return BigValue(1.0)
|
|
log10v = math.log(base) / math.log(10.0) * degree
|
|
whole = math.floor(log10v)
|
|
return BigValue(10.0 ** (log10v - whole), whole)
|
|
|
|
def pow_int(self, degree: int) -> "BigValue":
|
|
result = BigValue(1.0)
|
|
if degree == 0:
|
|
return result
|
|
base = BigValue.pair(self.mantissa, self.exponent)
|
|
if degree < 0:
|
|
base = result.divided_by(base)
|
|
degree = -degree
|
|
while degree >= 2:
|
|
if degree % 2 != 0:
|
|
result = base.times(result)
|
|
degree -= 1
|
|
base = base.times(base)
|
|
degree >>= 1
|
|
return base.times(result)
|
|
|
|
def square_root(self) -> "BigValue":
|
|
base = self.mantissa
|
|
power = self.exponent
|
|
if power % 2.0 != 0.0:
|
|
base *= 10.0
|
|
power -= 1
|
|
return BigValue(math.sqrt(base), power * 0.5)
|
|
|
|
def compare(self, other: "BigValue") -> int:
|
|
if self.mantissa == 0.0 and other.mantissa == 0.0:
|
|
return 0
|
|
if self.mantissa < 0 or other.mantissa < 0:
|
|
return 1 if self.mantissa > other.mantissa else -1
|
|
if self.exponent != other.exponent:
|
|
return 1 if self.exponent > other.exponent else -1
|
|
if self.mantissa == other.mantissa:
|
|
return 0
|
|
return 1 if self.mantissa > other.mantissa else -1
|
|
|
|
def floored(self) -> "BigValue":
|
|
if self.exponent <= -15 or self.exponent >= 15:
|
|
return BigValue.pair(self.mantissa, self.exponent)
|
|
return BigValue(math.floor(self.to_float() + FLOOR_BIAS))
|
|
|
|
def ceiled(self) -> "BigValue":
|
|
if self.exponent <= -15 or self.exponent >= 15:
|
|
return BigValue.pair(self.mantissa, self.exponent)
|
|
return BigValue(math.ceil(self.to_float()))
|
|
|
|
def rounded(self) -> "BigValue":
|
|
lower = self.floored()
|
|
return self.ceiled() if self.minus(lower).compare(BigValue(0.5)) > 0 else lower
|
|
|
|
def to_float(self) -> float:
|
|
return self.mantissa * (10.0 ** self.exponent)
|
|
|
|
def to_int(self) -> int:
|
|
return int(round(self.to_float()))
|
|
|
|
def encoded(self) -> str:
|
|
# colon-separated; str() matches GDScript str(float) shortest round-trip
|
|
return f"{self.mantissa}:{self.exponent}"
|
|
|
|
@staticmethod
|
|
def from_text(text: str) -> "BigValue":
|
|
text = text.strip()
|
|
try:
|
|
if ":" in text:
|
|
m, p = text.split(":", 1)
|
|
return BigValue(float(m), float(p))
|
|
# plain scientific text like "1.5e95"
|
|
return BigValue(float(text))
|
|
except ValueError:
|
|
return BigValue()
|
|
|
|
def __repr__(self) -> str:
|
|
return f"BigValue({self.mantissa:.6g}e{self.exponent:g})"
|
|
|
|
|
|
def validate() -> list:
|
|
p = []
|
|
if BigValue(10.0, 0).mantissa != 1.0 or BigValue(10.0, 0).exponent != 1.0:
|
|
p.append("normalize 10 -> 1e1 wrong")
|
|
one = BigValue(1.0)
|
|
ten = BigValue(10.0)
|
|
if one.plus(ten).to_float() != 11.0:
|
|
p.append("1+10 wrong")
|
|
big = BigValue.power(1.55, 1000)
|
|
if big.exponent < 100:
|
|
p.append("power(1.55,1000) exponent too small")
|
|
if abs(BigValue(2.0).pow_int(20).to_float() - 2.0 ** 20) > 1e-6:
|
|
p.append("pow_int wrong")
|
|
if BigValue.from_text(big.encoded()).compare(big) != 0:
|
|
p.append("encode/from_text round-trip wrong")
|
|
return p
|