From f657d4257e6b39a3899a2ebef8f30f99ebfb313f Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Wed, 13 Sep 2023 19:38:39 -0500 Subject: 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. --- factors/score.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'factors/score.go') 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 { -- cgit v1.2.3