motoko icon indicating copy to clipboard operation
motoko copied to clipboard

Compiler bug triggered when assigning a `Float` larger than 1.79e+308

Open ByronBecker opened this issue 3 years ago • 6 comments

I received this compiler error while playing around with algorithms that hex encode large integers. Instead of a compiler error I'd expect some sort of overflow error saying that the largest int/float I can assign a variable to is x

Reproducible example:

// ok
let n_1_79e308: Float = 179000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000;
// compiler error
let n_1_80e308: Float = 180000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000;

Error message:

OOPS! You've triggered a compiler bug.
Please report this at https://github.com/dfinity/motoko/issues/new with the following details:

Motoko 0.6.29 (source w1ykh27s-ajw8misw-3zyqay9g-4x405jls)

Fatal error: exception Failure("of_string")
Raised at Stdlib.failwith in file "stdlib.ml", line 29, characters 17-33
Called from Mo_frontend__Typing.check_lit_val in file "mo_frontend/typing.ml", line 747, characters 6-17
Called from Mo_frontend__Typing.check_lit in file "mo_frontend/typing.ml", line 825, characters 20-42
Called from Mo_frontend__Typing.check_exp' in file "mo_frontend/typing.ml", line 1382, characters 4-30
Called from Mo_frontend__Typing.check_exp in file "mo_frontend/typing.ml", line 1372, characters 11-45
Called from Mo_frontend__Typing.infer_exp'' in file "mo_frontend/typing.ml", line 1329, characters 24-51
Called from Mo_frontend__Typing.infer_exp' in file "mo_frontend/typing.ml", line 896, characters 10-29
Called from Mo_frontend__Typing.infer_exp in file "mo_frontend/typing.ml" (inlined), line 880, characters 2-31
Called from Mo_frontend__Typing.infer_dec in file "mo_frontend/typing.ml", line 2189, characters 4-21
Called from Mo_frontend__Typing.infer_block in file "mo_frontend/typing.ml", line 2162, characters 10-55
Called from Mo_frontend__Typing.recover_opt.(fun) in file "mo_frontend/typing.ml", line 78, characters 55-60
Called from Mo_frontend__Typing.recover_with in file "mo_frontend/typing.ml", line 77, characters 56-59
Called from Diag.with_message_store in file "lang_utils/diag.ml", line 82, characters 10-13
Called from Pipeline.infer_prog in file "pipeline/pipeline.ml", line 172, characters 10-37
Called from Pipeline.check_progs in file "pipeline/pipeline.ml", line 192, characters 22-39
Called from Pipeline.load_progs in file "pipeline/pipeline.ml", line 411, characters 16-40
Called from Diag.bind in file "lang_utils/diag.ml", line 32, characters 27-30
Called from Diag.bind in file "lang_utils/diag.ml", line 32, characters 27-30
Called from Diag.bind in file "lang_utils/diag.ml", line 32, characters 27-30
Called from Pipeline.compile_files in file "pipeline/pipeline.ml", line 687, characters 27-71
Called from Dune__exe__Moc.process_files in file "exes/moc.ml", line 200, characters 49-94
Called from Dune__exe__Moc in file "exes/moc.ml", line 295, characters 4-23

ByronBecker avatar Sep 27 '22 23:09 ByronBecker

@ByronBecker this looks a lot like https://github.com/dfinity/motoko/issues/2734. Can you confirm?

ggreif avatar Sep 28 '22 00:09 ggreif

They definitely might be related/linked, but that error that Omer linked is different in terms of printing out the float.

In my example I'm just assigning the float and not printing anything (No call to Debug.print()).

Feel free to mark this as a duplicate!

Also, as an aside I just started playing around with printing somewhat large Floats and was wondering what the precision bound expectations are for Floats (feel free to add this to the backlog if not known 😅 )

ByronBecker avatar Sep 28 '22 17:09 ByronBecker

Ugh, these seem indeed separate issues. You get a crash in moc, the other is a crash when running the Wasm. Seems like working with Floats at the fringe of its numeric range is causing mayhem all across the map. :-(

ggreif avatar Sep 29 '22 09:09 ggreif

@ggreif

Also, just to follow up on a similar thread (and in case you're curious) - I found that floats start to lose precision right above 4.0 * 10^22. It might be nice to include these limits somewhere in the Float documentation.

let x: Float = 41_000_000_000_000_000_000_000;
let y: Float = 40_999_999_999_999_999_999_999;
let z: Float = 40_000_000_000_000_000_000_000;

Debug.print(debug_show(x));
Debug.print(debug_show(x - y)); 
Debug.print(debug_show(x - z)); 

Output:

40999999999999997902848.000000
0.000000
999999999999997902848.000000

ByronBecker avatar Sep 30 '22 07:09 ByronBecker

Actually, 4.0 * 10^22 is the limit for assignment - the limit for arithmetic operations is lower

ByronBecker avatar Sep 30 '22 07:09 ByronBecker

Still a bug:

Motoko (source 0.14.4-8-g7c45a26dc)

Fatal error: exception Failure("of_string")
Raised at Stdlib.failwith in file "stdlib.ml", line 29, characters 17-33
Called from Mo_frontend__Typing.check_lit_val in file "mo_frontend/typing.ml", line 1059, characters 6-17
Called from Mo_frontend__Typing.check_lit in file "mo_frontend/typing.ml", line 1136, characters 20-42
Called from Mo_frontend__Typing.check_exp' in file "mo_frontend/typing.ml", line 1831, characters 4-35
Called from Mo_frontend__Typing.check_exp in file "mo_frontend/typing.ml", line 1821, characters 11-45
Called from Mo_frontend__Typing.infer_exp'' in file "mo_frontend/typing.ml", line 1695, characters 24-51
Called from Mo_frontend__Typing.infer_exp_wrapper in file "mo_frontend/typing.ml", line 1209, characters 10-21
...

ggreif avatar Mar 21 '25 14:03 ggreif