pervognsen / bitwise

Bitwise is an educational project where we create the software/hardware stack for a computer from scratch.
Other
5.14k stars 212 forks source link

Problems with literal floating point numbers in #assert #57

Closed amoura closed 6 years ago

amoura commented 6 years ago

The following program illustrates the issue:

`import libc {...}

func main() { x := 1.0d; y := x + 1e-10d; printf("x=%.12f y=%.12f\n", x, y); printf("y-x = %.12f\n", y-x);

#assert(fabsd(y-x) < 2e-10d);

}`

The printf's work as expected, but the assertion fails. This is the output: `x=1.000000000000 y=1.000000000000

y-x = 0.000000000000

ionprobl: /home/amoura/ion-probl/tst.ion:9: main: Assertion `(fabs((y) - (x))) < (0.000000)' failed.

Aborted (core dumped) ` It looks like while scanning the assertion, the literal 2e-10d is scanned as 0.000000.

pervognsen commented 6 years ago

Thanks, I'll take a look!

pervognsen commented 6 years ago

This was a really good catch. The issue wasn't actually the scanning but the way floats were printed in the C code generator. I checked in a fix which just uses the exact string data from the Ion source.