import ( "fmt" "math/big" ) func main() { r := new(big.Rat) err := r.SetString("3/7") if err != nil { fmt.Println("Error setting rational number:", err) } else { fmt.Println(r.FloatString(3)) } }
import ( "fmt" "math/big" ) func main() { r := new(big.Rat) err := r.SetString("sqrt(2)") if err != nil { fmt.Println("Error setting rational number:", err) } else { fmt.Println(r.FloatString(10)) } }This code sets the value of the `r` variable to the square root of 2 using the `SetString` method. Note that the square root of 2 is not a rational number, but we can still set its value using a string representation. We can then print the floating-point approximation of the number using the `FloatString` method. The `SetString` method is part of the `math/big` package library.