Skip to content

Commit a392ade

Browse files
authored
Fix bug in quantile: where the input probability is given as a float. (#188)
In Pharo 9, in cases like where the input probability is a Float and not a Fraction like ``` #(1 2 3 4 5) asSortedCollection quantile: 0.25 ``` an error gets thrown as SortedCollection requires an Integer for a lookup. With the previous code the value of the temporary value, `p`, could end up being a Float like `2.0` that would equal the integer ( i.e. `p truncate = p` ) but using `2.0` as an index no longer works.
1 parent 1907ebe commit a392ade

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/Math-Quantile/SortedCollection.extension.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ SortedCollection >> quantile: aProbability withProbs: anArray [
8888
ifTrue: [ 1 ]
8989
ifFalse: [ p min: self size ].
9090
^ p truncated = p
91-
ifTrue: [ self at: p ]
91+
ifTrue: [ self at: p asInteger ] "we have to convert io integer here for cases where aProbability was a Float"
9292
ifFalse: [ (f := self at: p floor)
9393
+ (((self at: p ceiling) - f) * (p fractionPart * d + c)) ]
9494
]

src/Math-Tests-Quantile/PMQuantileTest.class.st

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ PMQuantileTest >> testExtremeCase [
5858
equals: 1
5959
]
6060

61+
{ #category : #tests }
62+
PMQuantileTest >> testFloatProbability [
63+
"tests that input quantile probabilities can be a float"
64+
65+
self assert: (c quantile: 0.25) equals: 2.
66+
67+
self assert: (c quantile: 0.27) equals: 2.08
68+
]
69+
6170
{ #category : #tests }
6271
PMQuantileTest >> testQuantileA [
6372
self

0 commit comments

Comments
 (0)