Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #339 error message when calculating the median of empty, non-float arrays #342

Open
wants to merge 2 commits into
base: developer
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/FSharp.Stats/Array.fs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ module Array =
/// </code>
/// </example>
let inline weightedMean (weights:array<'T>) (items:array<'T>) =
// throws an exception if the collection is empty and the file type is not float (returns nan if float collection)
if items.Length = 0 && not (box items :? float[]) then
failwithf "Array.weightedMean is undefined for an empty non float sequence!"
// DimensionMismatchException
if (items.Length <> weights.Length) then
failwithf "The items and weights must have the same length"
Expand Down Expand Up @@ -305,6 +308,10 @@ module Array =
/// </code>
/// </example>
let inline weightedVariance mean (weights:array<'T>) (items:array<'T>) =
// throws an exception if the collection is empty and the file type is not float (returns nan if float collection)
if items.Length = 0 && not (box items :? float[]) then
failwithf "Array.weightedVariance is undefined for an empty non float sequence!"

// DimensionMismatchException
if (items.Length <> weights.Length) then
failwithf "The items and weights must have the same length"
Expand Down Expand Up @@ -334,7 +341,10 @@ module Array =
/// <code>
/// </code>
/// </example>
let inline median (items:array<'T>) =
let inline median (items: array<'T>) =
// throws an exception if the collection is empty and the file type is not float (returns nan if float collection)
if items.Length = 0 && not (box items :? float[]) then
failwithf "Array.median is undefined for an empty non float sequence!"

let zero = LanguagePrimitives.GenericZero< 'T >
let one = LanguagePrimitives.GenericOne< 'T >
Expand Down
16 changes: 14 additions & 2 deletions tests/FSharp.Stats.Tests/Array.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ let testArrayNegInfinity = [|10000.;-0.1;14.;-10.;5.;Double.NegativeInfinity|]

let testArrayEvenCountsInt = [|10000;-50;14;-9|]
let testArrayOddCountsInt = [|10000;-50;14;-10;5|]
let testArrayEmptyInt : int array = [||]
let testArrayEmptyFloat : float array = [||]
let testArrayEmptyDec : decimal array = [||]

[<Tests>]
let medianTests =
Expand All @@ -33,12 +36,21 @@ let medianTests =
let median = Array.median testArrayNegInfinity
Expect.floatClose Accuracy.high median 2.45 "Median should be 2.45"

testCase "testListEvenCountsInt" <| fun () ->
testCase "testArrayEvenCountsInt" <| fun () ->
let median = Array.median testArrayEvenCountsInt
Expect.equal median 2 "Median should be 2"
testCase "testListOddCountsInt" <| fun () ->
testCase "testArrayOddCountsInt" <| fun () ->
let median = Array.median testArrayOddCountsInt
Expect.equal median 5 "Median should be 5"
testCase "testArrayEmptyFloat" <| fun () ->
let median = Array.median testArrayEmptyFloat
Expect.isTrue (Ops.isNan median) "Median of empty float array should be nan"
testCase "testArrayEmptyInt" <| fun () ->
let median() = Array.median testArrayEmptyInt
Expect.throws (fun () -> median () |> ignore) "Median for empty non-float sequences is not defined"
testCase "testArrayEmptyDec" <| fun () ->
let median() = Array.median testArrayEmptyDec
Expect.throws (fun () -> median () |> ignore) "Median for empty non-float sequences is not defined"
]

[<Tests>]
Expand Down
Loading