Welcome! Please see the About page for a little more info on how this works.

+1 vote
ago in Syntax and reader by
retagged ago by
user=> (clojure.edn/read-string "777/2") ;; no surprises
777/2
user=> (clojure.edn/read-string "0777/2") ;; wait, 0777 supposed to be 511
777/2
user=> (clojure.edn/read-string "0x777/2") ;; hexadecimal suddenly throws
Execution error (NumberFormatException) at user/eval175 (REPL:1).
Invalid number: 0x777/2
user=> (clojure.edn/read-string "32r777/2") ;; radix throws as well
Execution error (NumberFormatException) at user/eval177 (REPL:1).
Invalid number: 32r777/2
user=> (clojure.edn/read-string "777N/2") ;; same for BigInt
Execution error (NumberFormatException) at user/eval181 (REPL:1).
Invalid number: 777N/2

Inconsistent part here is octal integer numerator form. I would expect it to fail the same way as the rest of this list but not to silently drop leading zero and turn to not octal form at all.

Second part of the question is: Why other forms throws at all?

1 Answer

0 votes
ago by
selected ago by
 
Best answer

Ratios in Clojure are rational ratios with integer numerator and denominator. Ratio syntax is not a composite of integer or other numeric syntaxes, it is its own definition where the numerator and denominator are strings of digits of arbitrary length (treated as long or biginteger as necessary based on scale).

The syntax is semantically defined as:
sign? digit+ '/' digit+

The regex pattern is currently:
"([-+]?[0-9]+)/([0-9]+)"

This pattern incorrectly allows leading 0 and should be narrowed.

Filed jira at https://clojure.atlassian.net/browse/CLJ-2925

The other errors are expected and these syntaxes are not supported.

...