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/digit_map.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/digit_map.go')
| -rw-r--r-- | factors/digit_map.go | 20 |
1 files changed, 12 insertions, 8 deletions
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) |
