New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compiler bug when matching on floats #5758
Comments
Comment author: @garrigue Try using ocaml -dlambda. |
Comment author: @garrigue Actually, it is pretty bad: floats are compared as strings during pattern-matching compilation, so that you can create an arbitrary number of branches... |
Comment author: pzimmer Here is a simpler example (you need to turn off errors on warnings): match 0. with 0. -> 0 | 0.0 -> 1 | 0. -> 2;; This returns 1, i.e. the compiler does not respect the ordering of the branches. Another sign of what is going on is this: match 0. with 0. -> 0 | 0.0 -> 1 | _ -> 2;; This does not raise a warning about unused match case (while replacing 0.0 with 0. does). I am sure similar examples would work with other styles of redundant float representations (i.e. 0.01 vs 1e-2). |
Comment author: @alainfrisch
I'm not sure. I guess the rationale is to avoid generating different .cmo for the same source when the libc version of float_of_string behaves in a non-standard way. The interpretation of floats is delegated to runtime (for bytecode) and to the assembler (native). But this does not really work, because some parts in the pattern matching compiler already use float_of_string at compile time to check equality of floats represented as strings (but not everywhere). |
Comment author: @garrigue Fix committed in trunk revision 12937. |
Comment author: @maranget Checked fix, looks ok. |
Comment author: @garrigue Committed in 4.00 too, since this was the point of this quick fix. |
Original bug ID: 5758
Reporter: dwu
Assigned to: @maranget
Status: closed (set by @maranget on 2012-09-27T12:31:18Z)
Resolution: fixed
Priority: normal
Severity: major
Version: 3.12.1
Target version: 4.00.1+dev
Fixed in version: 4.00.1+dev
Category: ~DO NOT USE (was: OCaml general)
Monitored by: @hcarty yminsky pzimmer @alainfrisch
Bug description
The following code outputs "Failed match A then later matched", when it should instead output "Matched A".
let test x str =
match (x, str) with
| (1. , "A") -> print_endline "Matched A";
| (1.0, "B") -> print_endline "Matched B";
| (1. , "C") -> print_endline "Matched C";
| result ->
match result with
| (1., "A") -> print_endline "Failed match A then later matched"
| _ -> print_endline "Failed twice"
;;
test 1. "A";;
However, if the line
| (1.0, "B") -> print_endline "Matched B";
is changed to
| (1. , "B") -> print_endline "Matched B";
then the code correctly outputs "Matched A".
Steps to reproduce
Run in a toplevel.
The text was updated successfully, but these errors were encountered: