According to the wiki entry on the median value it seems that the common interpretation of a median value of an even number of values is the average of the two middle values (when sorted). But I got surprised that Datomic has applied other semantics to the median function:

Running [:find (median ?v) :with ?e :where [?e :ns/double ?v]] on the following numbers I get these results:

(1.0, 2.0, 3.0, 4.0) => 2.0 // expected (2.0 + 3.0) / 2 = 2.5

(1.0, 2.1, 3.1, 4.0) => 2.0 // expected (2.1 + 3.1) / 2 = 2.6

(1.0, 2.9, 3.9, 4.0) => 3.0 // expected (2.9 + 3.9) / 2 = 3,4

(1.0, 2.9, 5.9, 6.0) => 4.0 // expected (2.9 + 5.9) / 2 = 4,4

Datomic seems to calculate the median by rounding down the average of the two middle numbers to the nearest whole number. This just goes wrong when the two middle numbers are the same and have decimals:

(1.0, 2.5, 2.5, 3.0) => 2.0 // expected 2.5 - 2.0 is outright wrong.

I was surprised in this particular case to see that I can get the correct result if I omit the :with clause:

[:find (median ?v) :where [?e :ns/double ?v]] => 2.5 // correct

The previous results are the same with or without the :with clause.

Are there strong reasons to have other semantics for the median function than that described on the wiki? Or have I misunderstood something?

Thanks

Marc