Skip to content

Commit 0e24863

Browse files
committed
fix: decide a zero spelling from the text, not the parse
Review found that testing `value == 0.0` discarded the digits of any value too small for a float. '0.' followed by 400 zeros and a 1 -- plain decimal text, exactly what a <floatstring> is for -- underflowed and rendered 0.0, silently a different number. The cutoff was an artifact of binary64 rather than anything in the template: 1e-320 is subnormal and survived, 1e-400 did not. Ask the text instead. An all-zero mantissa spells zero whatever the exponent, so '0.00', '-0.0' and '0e5' lose their sign and keep their digits, while '1e-400' and '0.000...1' keep both. Mirrors openjd_expr::value::text_spells_zero, so openjd-rs#354 renders every one of these identically. Verification. Model suite 5536 passed, 24 skipped, 3 xfailed; ruff, black and mypy clean. Full 2023-09 conformance 1162 passed, 0 failed. Signed-off-by: David Leong <116610336+leongdl@users.noreply.github.com>
1 parent fbf09d5 commit 0e24863

2 files changed

Lines changed: 24 additions & 15 deletions

File tree

‎src/openjd/model/v2023_09/_model.py‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,14 @@ def _validate_target_runtime_seconds(cls, value: Any, info: ValidationInfo) -> A
12621262
# digit, so '0.50' keeps the zero that is its integer part.
12631263
_REDUNDANT_LEADING_ZEROS = re.compile(r"^([+-]?)0+(?=[0-9])")
12641264

1265+
# An all-zero mantissa, whatever the exponent: '0.00', '-0.0' and '0e5' spell zero,
1266+
# '1e-400' does not. Mirrors openjd_expr::value::text_spells_zero.
1267+
_SPELLS_ZERO = re.compile(r"^[+-]?[0.]*[0][0.]*(?:[eE][+-]?[0-9]+)?$")
1268+
1269+
1270+
def _spells_zero(text: str) -> bool:
1271+
return _SPELLS_ZERO.match(text) is not None
1272+
12651273

12661274
def _normalized_range_element(elem: str, to_int: bool) -> Any:
12671275
"""The value an ``<intstring>``/``<floatstring>`` range element denotes.
@@ -1275,21 +1283,19 @@ def _normalized_range_element(elem: str, to_int: bool) -> Any:
12751283
return int(elem) # int() already drops leading zeros
12761284
# Parsed only to check it is a number; the text is what renders. Not a
12771285
# Decimal -- re-rendering one is context-sensitive and unbounded (§7.5).
1278-
value = float(elem)
1286+
float(elem)
12791287
except ValueError:
12801288
# A resolved format string can be non-numeric. Literals are checked at
12811289
# template parse time, so carry it through rather than rejecting here.
12821290
return elem
12831291
# Trimmed because the text reaches a command line and float() ignores
12841292
# surrounding whitespace; openjd-rs trims here too.
12851293
text = _REDUNDANT_LEADING_ZEROS.sub(r"\1", elem.strip())
1286-
if value == 0.0:
1287-
# Zero has no sign, so drop one without dropping the decimal places:
1288-
# '-0.00' renders `0.00`. Text that reaches zero only by underflow, like
1289-
# '1e-400', does not render the value at all and is not kept.
1290-
unsigned = text.lstrip("+-")
1291-
return unsigned if set(unsigned) <= {"0", "."} else "0.0"
1292-
return text
1294+
# Zero has no sign, so drop one without dropping the decimal places: '-0.00'
1295+
# renders `0.00`. Decided from the text, not from `value` -- the parse
1296+
# underflows to 0.0 below ~5e-324, and a tiny value's digits are exactly what
1297+
# a <floatstring> is for.
1298+
return text.lstrip("+-") if _spells_zero(text) else text
12931299

12941300

12951301
# Target model for task parameters when instantiating a job.

‎test/openjd/model_v0/v2023_09/test_parameter_space.py‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -489,9 +489,12 @@ def test_intstring_elements_carry_their_value(self, param_type: str) -> None:
489489
# Dropping it must not drop the decimal places with it.
490490
pytest.param("-0.0", "0.0", id="negative zero has no sign"),
491491
pytest.param("-0.00", "0.00", id="unsigned, but still two places"),
492-
# Reaches zero only by underflow, so the text does not render the
493-
# value and is not kept.
494-
pytest.param("1e-400", "0.0", id="underflow to zero"),
492+
# An all-zero mantissa spells zero whatever the exponent.
493+
pytest.param("0E+2", "0E+2", id="exponent form of zero"),
494+
pytest.param("-0e5", "0e5", id="signed exponent form of zero"),
495+
# Underflow is not zero being spelled: the digits are the request.
496+
pytest.param("1e-400", "1e-400", id="underflow keeps its digits"),
497+
pytest.param("0." + "0" * 400 + "1", "0." + "0" * 400 + "1", id="tiny plain decimal"),
495498
# float() ignores surrounding whitespace and this text reaches a
496499
# command line, so it is trimmed rather than forwarded.
497500
pytest.param(" 1.5 ", "1.5", id="surrounding whitespace"),
@@ -524,10 +527,10 @@ def test_floatstring_elements_keep_their_scale(self, element: str, expected: str
524527
# characters, which is the whole point: nothing expands it.
525528
pytest.param("1e999999999", "1e999999999", id="huge positive exponent"),
526529
pytest.param("1E+1022", "1E+1022", id="positive exponent past the field cap"),
527-
# Too small, and the value underflows to zero, so the text no longer
528-
# renders it. openjd-rs resolves these to zero and renders 0.0 too.
529-
pytest.param("1e-999999999", "0.0", id="huge negative exponent"),
530-
pytest.param("1E-1023", "0.0", id="negative exponent past the field cap"),
530+
# Too small for a float either way, and the digits still stand:
531+
# underflowing to 0.0 is a parse limit, not the author writing zero.
532+
pytest.param("1e-999999999", "1e-999999999", id="huge negative exponent"),
533+
pytest.param("1E-1023", "1E-1023", id="negative exponent past the field cap"),
531534
),
532535
)
533536
def test_a_huge_exponent_costs_only_its_own_characters(

0 commit comments

Comments
 (0)