Programming Language: Golang

Namespace/Package Name: math/big

Class/Type: Rat

Method/Function: Sub

Examples at hotexamples.com: 18

The go math.big Rat Sub function is used to subtract two Rat values and return the result in a new Rat value. Rat values are rational numbers represented as a fraction (numerator and denominator).

**Example 1:** Subtracting two Rat values

In this example, we have two Rat values - x and y, with values 4/5 and 2/5 respectively. We create a new Rat value, sub, and use the Sub function to subtract y from x. The result, 2/5, is stored in sub and printed to the console.

**Example 2:** Subtraction with integer values

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 }

In this example, we have two Rat values - x and y, with values 4/5 and 2/5 respectively. We create a new Rat value, sub, and use the Sub function to subtract y from x. The result, 2/5, is stored in sub and printed to the console.

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.

Frequently Used Methods

Frequently Used Methods

Frequently Used Methods

Related

Related in langs