import ( "fmt" "math/big" ) func main() { x := big.NewRat(4, 5) y := big.NewRat(2, 5) sub := new(big.Rat) sub.Sub(x, y) fmt.Println(sub) // Output: 2/5 }
import ( "fmt" "math/big" ) func main() { x := big.NewRat(4, 1) // equivalent to integer 4 y := big.NewRat(2, 1) // equivalent to integer 2 sub := new(big.Rat) sub.Sub(x, y) fmt.Println(sub) // Output: 2/1 (equivalent to integer 2) }Here, we have Rat values x and y that are equivalent to integers 4 and 2 respectively. We perform the subtraction using the Sub function and observe that the result, 2/1, is equivalent to the integer 2. Package library: math/big The go math.big package provides arbitrary-precision arithmetic on integers and floats, as well as support for rational numbers (Rat) and a decimal floating-point type (Float). The Rat Sub function is part of the Rat type and is used to perform subtraction operations on rational numbers.