-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
34 lines (27 loc) · 1.77 KB
/
example_test.go
File metadata and controls
34 lines (27 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package exact_test
import (
format "fmt"
"math/big"
"github.com/madlambda/exact"
)
func ExampleArith() {
f1 := exact.NewRat(2207, 987)
f2 := exact.NewRat(15, 3)
// r = (2207/987)+((2207/987)/(15/3))*(1/2)
r := exact.Mul(exact.Add(f1, exact.Div(f2, f1)), exact.NewRat(1, 2))
format.Printf("%s", r.Simplify())
// Output: 4870847/2178309
}
func ExampleSqrt() {
sqrt2 := exact.Sqrt(exact.NewRat(2, 1))
sqrtStr := big.NewRat(1, 1).SetFrac(sqrt2.P, sqrt2.Q).FloatString(200)
format.Printf("rational: %s\ndecimal: %s\n", sqrt2, sqrtStr)
// Output: rational: 48926646634423881954586808839856694558492182258668537145547700898547222910968507268117381704646657/34596363615919099765318545389014861517389860071988342648187104766246565694525469768325292176831232
// decimal: 1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206086686
square := exact.Mul(sqrt2, sqrt2)
squareStr := big.NewRat(1, 1).SetFrac(square.P, square.Q).FloatString(200)
format.Printf("sqrt(2)^2 = %s\n", square)
format.Printf("sqrt(2)^2 = %s\n", squareStr)
// sqrt(2) = 2393816750889781775169375739082176313196905492596202627327114638125962681393885153176192162692298199297165620016318855149608788834131565123446110899404713627587174222798551817391907017545221275649/1196908375444890887584687869541088156598452746298101313663557319062981340696942576588096081346149099648582810008159427574804394417065782561723055449702356813793587111399275908695953508772610637824
// sqrt(2)^2 = 2.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000083549
}