replace log.Fatal with cobra.CheckErr

This commit is contained in:
filifa 2025-08-27 21:33:44 -04:00
parent 51e1a97c43
commit 65c8ab4057
5 changed files with 14 additions and 18 deletions

View File

@ -18,7 +18,6 @@ package cmd
import (
"fmt"
"log"
"math/big"
"github.com/spf13/cobra"
@ -41,14 +40,14 @@ func convergents(cmd *cobra.Command, args []string) {
a0, ok := new(big.Int).SetString(args[0], 10)
if !ok {
log.Fatal("invalid input " + args[0])
cobra.CheckErr("invalid input " + args[0])
}
denoms := make([]big.Int, len(args[1:]))
for i, d := range args[1:] {
_, ok = denoms[i].SetString(d, 10)
if !ok {
log.Fatal("invalid input " + d)
cobra.CheckErr("invalid input " + d)
}
}

View File

@ -18,7 +18,6 @@ package cmd
import (
"fmt"
"log"
"math/big"
"github.com/spf13/cobra"
@ -30,7 +29,7 @@ var moduli []string
func crt(cmd *cobra.Command, args []string) {
if len(remainders) != len(moduli) {
log.Fatal("number of remainders and moduli do not match")
cobra.CheckErr("number of remainders and moduli do not match")
}
ns := make([]*big.Int, len(moduli))
@ -39,18 +38,19 @@ func crt(cmd *cobra.Command, args []string) {
var ok bool
ns[i], ok = new(big.Int).SetString(moduli[i], 10)
if !ok {
log.Fatal("invalid input " + moduli[i])
cobra.CheckErr("invalid input " + moduli[i])
}
rs[i], ok = new(big.Int).SetString(remainders[i], 10)
if !ok {
log.Fatal("invalid input " + remainders[i])
cobra.CheckErr("invalid input " + remainders[i])
}
}
// TODO: support non-pairwise coprime moduli
if !lib.ArePairwiseCoprime(ns) {
log.Fatalf("moduli %v are not pairwise coprime", moduli)
err := fmt.Errorf("moduli %v are not pairwise coprime", moduli)
cobra.CheckErr(err)
}
x, N := lib.CRTSolution(rs, ns)

View File

@ -18,7 +18,6 @@ package cmd
import (
"fmt"
"log"
"math/big"
"github.com/spf13/cobra"
@ -47,7 +46,7 @@ func gcd(cmd *cobra.Command, args []string) {
for i, s := range args {
_, ok := w.SetString(s, 10)
if !ok {
log.Fatal("invalid input " + s)
cobra.CheckErr("invalid input " + s)
}
z.GCD(x, y, z, w)

View File

@ -18,7 +18,6 @@ package cmd
import (
"fmt"
"log"
"math/big"
"github.com/spf13/cobra"
@ -30,18 +29,18 @@ var n string
func jacobi(cmd *cobra.Command, args []string) {
x, ok := new(big.Int).SetString(a, 10)
if !ok {
log.Fatal("invalid input " + a)
cobra.CheckErr("invalid input " + a)
}
y, ok := new(big.Int).SetString(n, 10)
if !ok {
log.Fatal("invalid input " + n)
cobra.CheckErr("invalid input " + n)
}
mod := big.NewInt(2)
mod.Mod(y, mod)
if mod.Cmp(big.NewInt(0)) == 0 {
log.Fatal("modulus must be odd")
cobra.CheckErr("modulus must be odd")
}
z := big.Jacobi(x, y)

View File

@ -18,7 +18,6 @@ package cmd
import (
"fmt"
"log"
"math/big"
"github.com/spf13/cobra"
@ -45,7 +44,7 @@ func count(tpf []string) map[string]*big.Int {
func primitiveRoot(cmd *cobra.Command, args []string) {
m, ok := new(big.Int).SetString(modulus, 10)
if !ok {
log.Fatal("invalid input " + modulus)
cobra.CheckErr("invalid input " + modulus)
}
factors := count(tpf)
@ -55,12 +54,12 @@ func primitiveRoot(cmd *cobra.Command, args []string) {
if len(factors) == 0 {
root, err = lib.PrimitiveRoot(m)
if err != nil {
log.Fatal(err)
cobra.CheckErr(err)
}
} else {
root, err = lib.PrimitiveRootFast(m, factors)
if err != nil {
log.Fatal(err)
cobra.CheckErr(err)
}
}