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/digit_map.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'factors/digit_map.go') diff --git a/factors/digit_map.go b/factors/digit_map.go index 83606e9..a16f018 100644 --- a/factors/digit_map.go +++ b/factors/digit_map.go @@ -28,6 +28,7 @@ const ( // - it will not be nice to work with Opaque TotativeType = 0x40 // This number is zero, and doesn't have a true totative type. + // (except in radix zero, where zero is considered a factor) Zero TotativeType = 0x00 ) @@ -98,6 +99,10 @@ func Split(digit, radix uint) (regular, totative uint) { // Calculates the regularity index of a number's regular part func calcRegularity(regular, radix uint) uint8 { + if regular == 0 && radix == 0 { + return 1 + } + regularity, radixPower := uint8(0), uint(1) for radixPower%regular != 0 { regularity++ @@ -125,10 +130,8 @@ func calcTotativeType(totative, radix uint) TotativeType { } // Gets the regularity and totative type of one digit in a radix +// In radix zero, zero is type 1R, one is type 0R, and everything else is 0P. func GetDigitType(digit, radix uint) DigitType { - if radix < 2 { - panic("Radices cannot be less than 2!") - } radixPF := PrimeFactorize(radix) digitPF := PrimeFactorize(digit) regular, totative := splitPF(digitPF, radixPF) @@ -139,13 +142,14 @@ func GetDigitType(digit, radix uint) DigitType { // Gets the regularity and totative type of each digit in a radix func DigitMap(radix uint) []DigitType { - if radix < 2 { - panic("Radices cannot be less than 2!") - } radixPF := PrimeFactorize(radix) types := make([]DigitType, radix, radix) - types[0] = zeroType - types[1] = oneType + if radix > 0 { + types[0] = zeroType + } + if radix > 1 { + types[1] = oneType + } for d := uint(2); d < radix; d++ { digitPF := PrimeFactorize(d) regular, totative := splitPF(digitPF, radixPF) -- cgit v1.2.3