clojure-dev

Kirill Chernyshov 2025-11-25T18:49:52.905719Z

I'm wondering if there are any materials left from when decisions were made about what numbers were possible in the form of ratio numbers. Specifically, I'm interested in why only decimal integers can be used in ratio.

2025-12-01T15:54:36.946599Z

I've uploaded a patch which changes the regex to [1-9][0-9]* for both numerator and denominator.

2025-11-25T18:55:51.671709Z

may need to back up a bit

2025-11-25T18:57:24.169519Z

the answer is almost certainly precision, floating point numbers are not exact, bigdecimal and ratios are, but there may be other stuff to unpack in the question

Kirill Chernyshov 2025-11-25T18:57:55.580389Z

https://ask.clojure.org/index.php/14763/inconsistent-errors-ratio-parsing-either-clojure-clojure the context is octal, hex etc

borkdude 2025-11-25T18:58:30.585709Z

oh that's an interesting edge case

2025-11-25T18:58:56.164069Z

oh, yeah, that doesn't have anything to do with the actual type right, that is just the reader

Kirill Chernyshov 2025-11-25T18:59:12.752869Z

probably yes

Kirill Chernyshov 2025-11-25T19:01:01.861299Z

asking because I'm trying to wrap my head around that for two days and feel I'm going crazy)

2025-11-25T19:01:16.056229Z

I mean for sure the answer is "the r radix syntax is almost never used, and octal and hexadecimal support tend to be used in bit twiddling code which is different code from the code that might use ratios, so no one has ever mashed these together before"

👍 1
2025-11-25T19:03:28.542219Z

https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/LispReader.java#L73 is the regex the reader uses for ratio "tokens"

Alex Miller (Clojure team) 2025-11-25T19:04:59.735259Z

https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/EdnReader.java#L32 is more relevant here

Alex Miller (Clojure team) 2025-11-25T19:05:36.574469Z

this particular ask is only looking at edn/read-string

2025-11-25T19:06:24.862449Z

sure, but the ednreader part of that is just copied from lispreader?

Kirill Chernyshov 2025-11-25T19:08:35.833059Z

clojure' reader has the same behaviour

user=> 0x74/5
Syntax error reading source at (REPL:41:0).
Invalid number: 0x74/5

2025-11-25T19:08:46.530539Z

https://github.com/edn-format/edn/issues/47 is also relevant

Alex Miller (Clojure team) 2025-11-25T19:08:50.548509Z

just saying. edn does not actually define ratios at all (or octal or hex or radix nums), so this is all in the gray areas

Alex Miller (Clojure team) 2025-11-25T19:09:30.587709Z

and the answers here don't necessarily need to be the same between the two

Kirill Chernyshov 2025-11-25T19:11:20.534099Z

so, to summarize - there was no some chain of thought that lead to that decision, right?

Kirill Chernyshov 2025-11-25T19:11:58.704039Z

I was trying to rationalise ratio with only decimal ints to a friend and feel dumb right now)

borkdude 2025-11-25T19:14:16.936539Z

> EDN read is more liberal than the spec. I don't see that as a problem, as the spec is the arbiter, not implementations. The spec is going to start conservatively, but these may be added iff there is demand/need.

borkdude 2025-11-25T19:14:21.966999Z

(from that link)

Alex Miller (Clojure team) 2025-11-25T19:14:23.375419Z

I believe the intent is that a ratio is defined as having integer numerator and denominator (not valid: bigint N, float, bigdec M, octal, hex, or radix syntax)

borkdude 2025-11-25T19:14:48.927789Z

undefined

Kirill Chernyshov 2025-11-25T19:15:13.874669Z

ok, is there then something behind this definition?

Alex Miller (Clojure team) 2025-11-25T19:15:42.883749Z

do you mean where is "this definition" defined?

2025-11-25T19:15:58.967469Z

I feel like there may be an ask or something somewhere about doing something like bigint for ratios (I think it uses BigInteger under the hood, but could use longs)

Kirill Chernyshov 2025-11-25T19:16:18.128959Z

no, I mean "it is defined like that because ..."

Alex Miller (Clojure team) 2025-11-25T19:16:25.228429Z

https://clojure.org/reference/data_structures#_ratio

Alex Miller (Clojure team) 2025-11-25T19:18:34.263869Z

@hiredman from my skim of the impl, it seems like it autosizes numerator and denominator to long or bigint as needed as part of reading

2025-11-25T19:19:03.531239Z

oh, nice

Alex Miller (Clojure team) 2025-11-25T19:19:16.217869Z

@delaguardo rational ratios use integers

Alex Miller (Clojure team) 2025-11-25T19:19:47.662969Z

from my understanding, the intent of the ratio type is to represent rational ratios

borkdude 2025-11-25T19:20:07.856459Z

I guess his point is more about this:

user=> (integer? 0777)
true

user=> 0777/1
777

Alex Miller (Clojure team) 2025-11-25T19:22:30.899349Z

my reading of the admittedly minimal reference material and the intent of the impl is that the syntax for ratios is not a composite of other integer syntaxes, it is an atomic definition that defines the numerator and denominator as a string of digits (that leading 0 is allowed is definitely a bug imo)

Alex Miller (Clojure team) 2025-11-25T19:23:35.911709Z

that is, the syntax is: digit+/digit+

borkdude 2025-11-25T19:24:17.156309Z

yeah with - prefix optionally

Alex Miller (Clojure team) 2025-11-25T19:24:28.663149Z

yes, sorry

borkdude 2025-11-25T19:24:43.239159Z

oh hmm:

-10/11 ;; works 
-10/-11 ;; throws

Alex Miller (Clojure team) 2025-11-25T19:25:35.314479Z

seems correct and reinforces what I said above

Kirill Chernyshov 2025-11-25T19:28:28.900589Z

got it. thanks

Alex Miller (Clojure team) 2025-11-25T19:32:08.440459Z

I will log a jira for the leading 0

1
Alex Miller (Clojure team) 2025-11-25T19:50:37.723999Z

https://clojure.atlassian.net/browse/CLJ-2925

🎉 2
Alex Miller (Clojure team) 2025-11-25T19:50:41.665109Z

patch welcome