The test expects -0 as output for a calculation that results in zero

I’m stuck on Stage #JY2.

I’ve tried implementing the arithmetic operators +, -, *, and / in my interpreter. The calculations are working correctly, but I’m encountering an unexpected test case where the result is zero, but the test expects “-0” as the output.

Here are my logs:

[tester::#JY2] [test-4] Running test case: 4
[tester::#JY2] [test-4] Writing contents to ./test.lox:
[tester::#JY2] [test-4] [test.lox] -(-25 + 25) * (77 * 51) / (1 + 4)
[tester::#JY2] [test-4] $ ./your_program.sh evaluate test.lox
[your_program] 0
[tester::#JY2] [test-4] 𐄂 0
[tester::#JY2] [test-4] Expected line #1 on stdout to be "-0", got "0"
[tester::#JY2] [test-4] Test failed (try setting 'debug: true' in your codecrafters.yml to see more details)

And here’s a snippet of my code:

def format_number(self, value: float) -> str:
    d = Decimal(str(value))
    
    if d == d.to_integral_value():
        return str(int(d))
    
    rounded = d.quantize(Decimal('1e-15'), rounding=ROUND_HALF_UP)
    return str(rounded.normalize())

The test expects “-0” as output, but mathematically, -0 is equal to 0 in floating-point arithmetic. Is this a quirk of the test, or is there a specific reason we should distinguish between 0 and -0 in this interpreter implementation?

Definitely seems like a bug on our end. We do test against the official jlox implementation, but since randomness is involved, looks like this case didn’t trigger :slightly_smiling_face:

@jwross24 thanks for highlighting! We’ll get this fixed.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.