clojure-dev 2025-11-25

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.

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

may need to back up a bit

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

oh that's an interesting edge case

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

probably yes

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

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
Alex Miller (Clojure team) 2025-11-25T19:05:36.574469Z

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

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

clojure' reader has the same behaviour

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

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

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

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

> 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.

(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)

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?

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)

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

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

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

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+

yeah with - prefix optionally

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

yes, sorry

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

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:41.665109Z

patch welcome