Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DbBXp2Vp62Ur3C7yRWQdSq
81 lines
3.5 KiB
Python
81 lines
3.5 KiB
Python
"""Ishta / Kashta Phala reading with its scale check (BUG-2088, astrologer answer 7).
|
|
|
|
``scripts/shadbala.py`` stores
|
|
|
|
ishta_phala = sqrt(max(0, Uccha x Chesta))
|
|
kashta_phala = sqrt(max(0, (60 - Uccha) x (60 - Chesta)))
|
|
|
|
The square-root formula reads on a 0-60 scale only while both inputs are
|
|
themselves inside 0-60. Outside that range the stored number is either larger
|
|
than the scale says or a negative product silently turned into 0. This module
|
|
does not change the engine: it re-reads the two inputs and says, per planet,
|
|
|
|
* ``in_range``: both inputs are inside 0-60, the 0-60 reading holds;
|
|
* ``out_of_range``: an input is outside 0-60. The raw (unclamped) root is
|
|
returned when the product is not negative; when it is negative there is no
|
|
real root and the value is ``None`` with ``negative_product`` set. Nothing is
|
|
clamped to 60 and no negative product becomes 0;
|
|
* ``input_missing``: Uccha or Chesta was not returned, so the scale was not
|
|
checked; the engine's stored values (if any) are passed through as they are.
|
|
|
|
``ishta_bala_pct`` (total strength over the minimum requirement) is a different
|
|
quantity and is not read here.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import math
|
|
from typing import Any, Dict, Optional
|
|
|
|
SCALE_MAX = 60.0
|
|
|
|
|
|
def _number(value: Any) -> Optional[float]:
|
|
if isinstance(value, bool) or not isinstance(value, (int, float)):
|
|
return None
|
|
number = float(value)
|
|
return number if math.isfinite(number) else None
|
|
|
|
|
|
def _root(product: float) -> Dict[str, Any]:
|
|
if product < 0:
|
|
return {"value": None, "negative_product": product}
|
|
return {"value": math.sqrt(product), "negative_product": None}
|
|
|
|
|
|
def ishta_kashta_reading(uccha: Any, chesta: Any, *, stored_ishta: Any = None, stored_kashta: Any = None) -> Dict[str, Any]:
|
|
"""One planet's Ishta / Kashta Phala with the inputs used and the scale verdict."""
|
|
u = _number(uccha)
|
|
c = _number(chesta)
|
|
out: Dict[str, Any] = {"uccha": u, "chesta": c, "unit": "virupa", "scale_max": SCALE_MAX}
|
|
if u is None or c is None:
|
|
out.update({
|
|
"scale": "input_missing",
|
|
"missing_inputs": [name for name, value in (("uccha", u), ("chesta", c)) if value is None],
|
|
"ishta": {"value": _number(stored_ishta), "negative_product": None},
|
|
"kashta": {"value": _number(stored_kashta), "negative_product": None},
|
|
})
|
|
return out
|
|
outside = [name for name, value in (("uccha", u), ("chesta", c)) if not 0.0 <= value <= SCALE_MAX]
|
|
out["ishta"] = _root(u * c)
|
|
out["kashta"] = _root((SCALE_MAX - u) * (SCALE_MAX - c))
|
|
out["scale"] = "out_of_range" if outside else "in_range"
|
|
out["out_of_range_inputs"] = outside
|
|
if not outside:
|
|
# In range the engine's own figures stand (it used the unrounded Chesta).
|
|
for key, stored in (("ishta", stored_ishta), ("kashta", stored_kashta)):
|
|
if _number(stored) is not None:
|
|
out[key]["value"] = _number(stored)
|
|
return out
|
|
|
|
|
|
def shadbala_row_reading(row: Any) -> Dict[str, Any]:
|
|
"""Read the inputs from one ``shadbala.planets[name]`` row."""
|
|
row = row if isinstance(row, dict) else {}
|
|
sthana = row.get("sthana_bala") if isinstance(row.get("sthana_bala"), dict) else {}
|
|
chesta = row.get("chesta_bala")
|
|
if chesta is None:
|
|
chesta = row.get("cheshta_bala")
|
|
return ishta_kashta_reading(sthana.get("ucha_bala"), chesta,
|
|
stored_ishta=row.get("ishta_phala"), stored_kashta=row.get("kashta_phala"))
|