fix(tools): calculator keeps decimals exact as written
Numbers were binary floats, so ceil(1.1*100) gave 111 and floor(0.3/0.1) gave 2. Literals are now exact fractions as written (1.1 is 11/10): + - * / // % and whole-number powers have no rounding error; only math functions and fractional powers use floats. The power size guard covers exact fractions too (2^-100000 is refused, not 0). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -34,6 +34,20 @@ def test_it_gives_the_exact_value(expression, expected):
|
||||
assert value_of(run(expression)) == expected
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
@pytest.mark.parametrize("expression, expected", [
|
||||
("ceil(1.1*100)", "110"),
|
||||
("floor(0.3/0.1)", "3"),
|
||||
("0.1+0.2", "0.3"),
|
||||
("1.1*3 - 3.3", "0"),
|
||||
("10.5 % 0.1", "0"),
|
||||
("2.675*100 // 1", "267"),
|
||||
("0.5^-2", "4"),
|
||||
])
|
||||
def test_decimals_are_exact_as_written(expression, expected):
|
||||
assert value_of(run(expression)) == expected
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_fractions_are_rounded_to_a_readable_length():
|
||||
got = float(value_of(run("1.07^12")))
|
||||
@@ -61,7 +75,7 @@ def test_anything_but_arithmetic_is_refused(expression):
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
@pytest.mark.parametrize("expression", ["9**9**9", "10**100000", "(10**300)**300"])
|
||||
@pytest.mark.parametrize("expression", ["9**9**9", "10**100000", "(10**300)**300", "1.5**100000", "2**-100000"])
|
||||
def test_a_huge_power_fails_fast_instead_of_hanging(expression):
|
||||
start = time.monotonic()
|
||||
r = run(expression)
|
||||
|
||||
Reference in New Issue
Block a user