diff options
| author | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2023-09-13 19:38:39 -0500 |
|---|---|---|
| committer | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2023-09-13 19:38:39 -0500 |
| commit | f657d4257e6b39a3899a2ebef8f30f99ebfb313f (patch) | |
| tree | 16c117ba1ce8c10fd95230fb372c439c1ec6c613 /factors/score.go | |
| parent | 345f6bae7b005cdd386833aa496aa71ca11c7b97 (diff) | |
factors: Remove most panics
Panics are not the best way of handling errors in Go. I've replaced
panics with default values whenever a sensible one exists.
Factors(0) does not have a sensible default value (as every number is a
factor of zero), so it still panics.
Diffstat (limited to 'factors/score.go')
| -rw-r--r-- | factors/score.go | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/factors/score.go b/factors/score.go index b47aac4..9288e8f 100644 --- a/factors/score.go +++ b/factors/score.go @@ -1,5 +1,7 @@ package factors +import "math" + // Score returns a "factor score" equal to the sum of the reciprocoals // of the number n's factors. // Rationale: @@ -17,7 +19,7 @@ package factors // a factor's score is the probability a random number is divisible by it. func Score(n uint) float64 { if n == 0 { - panic("Cannot get factor score of 0.") + return math.NaN() } factorSum := uint(0) @@ -27,9 +29,11 @@ func Score(n uint) float64 { return float64(factorSum) / float64(n) } -// BasicRank returns a rank describing how well a base handles the simplest -// fractions (1/2, 1/3, 1/4 and 1/5) -// also known as 2345 Rank +// BasicRank returns a rank describing how well a radix handles the simplest +// fractions (1/2, 1/3, 1/4 and 1/5). Zero and one are not true radices, +// but because this rank otherwise only depends on a radix's remainder +// mod 60, they have the same ranks as 60 and 61 (A+, F~). +// Also known as 2345 Rank. func BasicRank(n uint) string { var firstRank, secondRank string if n%2 == 0 { |
