summaryrefslogtreecommitdiff
path: root/devdocs/go/math%2Fbig%2Findex.html
blob: 1c1d9c2baa659d171f2231e7e5bd2f130e13f20c (plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
<h1> Package big  </h1>     <ul id="short-nav">
<li><code>import "math/big"</code></li>
<li><a href="#pkg-overview" class="overviewLink">Overview</a></li>
<li><a href="#pkg-index" class="indexLink">Index</a></li>
<li><a href="#pkg-examples" class="examplesLink">Examples</a></li>
</ul>     <h2 id="pkg-overview">Overview </h2> <p>Package big implements arbitrary-precision arithmetic (big numbers). The following numeric types are supported: </p>
<pre data-language="go">Int    signed integers
Rat    rational numbers
Float  floating-point numbers
</pre> <p>The zero value for an <a href="#Int">Int</a>, <a href="#Rat">Rat</a>, or <a href="#Float">Float</a> correspond to 0. Thus, new values can be declared in the usual ways and denote 0 without further initialization: </p>
<pre data-language="go">var x Int        // &amp;x is an *Int of value 0
var r = &amp;Rat{}   // r is a *Rat of value 0
y := new(Float)  // y is a *Float of value 0
</pre> <p>Alternatively, new values can be allocated and initialized with factory functions of the form: </p>
<pre data-language="go">func NewT(v V) *T
</pre> <p>For instance, <a href="#NewInt">NewInt</a>(x) returns an *<a href="#Int">Int</a> set to the value of the int64 argument x, <a href="#NewRat">NewRat</a>(a, b) returns a *<a href="#Rat">Rat</a> set to the fraction a/b where a and b are int64 values, and <a href="#NewFloat">NewFloat</a>(f) returns a *<a href="#Float">Float</a> initialized to the float64 argument f. More flexibility is provided with explicit setters, for instance: </p>
<pre data-language="go">var z1 Int
z1.SetUint64(123)                 // z1 := 123
z2 := new(Rat).SetFloat64(1.25)   // z2 := 5/4
z3 := new(Float).SetInt(z1)       // z3 := 123.0
</pre> <p>Setters, numeric operations and predicates are represented as methods of the form: </p>
<pre data-language="go">func (z *T) SetV(v V) *T          // z = v
func (z *T) Unary(x *T) *T        // z = unary x
func (z *T) Binary(x, y *T) *T    // z = x binary y
func (x *T) Pred() P              // p = pred(x)
</pre> <p>with T one of <a href="#Int">Int</a>, <a href="#Rat">Rat</a>, or <a href="#Float">Float</a>. For unary and binary operations, the result is the receiver (usually named z in that case; see below); if it is one of the operands x or y it may be safely overwritten (and its memory reused). </p>
<p>Arithmetic expressions are typically written as a sequence of individual method calls, with each call corresponding to an operation. The receiver denotes the result and the method arguments are the operation's operands. For instance, given three *Int values a, b and c, the invocation </p>
<pre data-language="go">c.Add(a, b)
</pre> <p>computes the sum a + b and stores the result in c, overwriting whatever value was held in c before. Unless specified otherwise, operations permit aliasing of parameters, so it is perfectly ok to write </p>
<pre data-language="go">sum.Add(sum, x)
</pre> <p>to accumulate values x in a sum. </p>
<p>(By always passing in a result value via the receiver, memory use can be much better controlled. Instead of having to allocate new memory for each result, an operation can reuse the space allocated for the result value, and overwrite that value with the new result in the process.) </p>
<p>Notational convention: Incoming method parameters (including the receiver) are named consistently in the API to clarify their use. Incoming operands are usually named x, y, a, b, and so on, but never z. A parameter specifying the result is named z (typically the receiver). </p>
<p>For instance, the arguments for (*Int).Add are named x and y, and because the receiver specifies the result destination, it is called z: </p>
<pre data-language="go">func (z *Int) Add(x, y *Int) *Int
</pre> <p>Methods of this form typically return the incoming receiver as well, to enable simple call chaining. </p>
<p>Methods which don't require a result value to be passed in (for instance, <a href="#Int.Sign">Int.Sign</a>), simply return the result. In this case, the receiver is typically the first operand, named x: </p>
<pre data-language="go">func (x *Int) Sign() int
</pre> <p>Various methods support conversions between strings and corresponding numeric values, and vice versa: *<a href="#Int">Int</a>, *<a href="#Rat">Rat</a>, and *<a href="#Float">Float</a> values implement the Stringer interface for a (default) string representation of the value, but also provide SetString methods to initialize a value from a string in a variety of supported formats (see the respective SetString documentation). </p>
<p>Finally, *<a href="#Int">Int</a>, *<a href="#Rat">Rat</a>, and *<a href="#Float">Float</a> satisfy <span>fmt.Scanner</span> for scanning and (except for *<a href="#Rat">Rat</a>) the Formatter interface for formatted printing. </p>   <h4 id="example__eConvergents"> <span class="text">Example (EConvergents)</span>
</h4> <p>This example demonstrates how to use big.Rat to compute the first 15 terms in the sequence of rational convergents for the constant e (base of natural logarithm). </p> <p>Code:</p> <pre class="code" data-language="go">package big_test

import (
    "fmt"
    "math/big"
)

// Use the classic continued fraction for e
//
//	e = [1; 0, 1, 1, 2, 1, 1, ... 2n, 1, 1, ...]
//
// i.e., for the nth term, use
//
//	   1          if   n mod 3 != 1
//	(n-1)/3 * 2   if   n mod 3 == 1
func recur(n, lim int64) *big.Rat {
    term := new(big.Rat)
    if n%3 != 1 {
        term.SetInt64(1)
    } else {
        term.SetInt64((n - 1) / 3 * 2)
    }

    if n &gt; lim {
        return term
    }

    // Directly initialize frac as the fractional
    // inverse of the result of recur.
    frac := new(big.Rat).Inv(recur(n+1, lim))

    return term.Add(term, frac)
}

// This example demonstrates how to use big.Rat to compute the
// first 15 terms in the sequence of rational convergents for
// the constant e (base of natural logarithm).
func Example_eConvergents() {
    for i := 1; i &lt;= 15; i++ {
        r := recur(0, int64(i))

        // Print r both as a fraction and as a floating-point number.
        // Since big.Rat implements fmt.Formatter, we can use %-13s to
        // get a left-aligned string representation of the fraction.
        fmt.Printf("%-13s = %s\n", r, r.FloatString(8))
    }

    // Output:
    // 2/1           = 2.00000000
    // 3/1           = 3.00000000
    // 8/3           = 2.66666667
    // 11/4          = 2.75000000
    // 19/7          = 2.71428571
    // 87/32         = 2.71875000
    // 106/39        = 2.71794872
    // 193/71        = 2.71830986
    // 1264/465      = 2.71827957
    // 1457/536      = 2.71828358
    // 2721/1001     = 2.71828172
    // 23225/8544    = 2.71828184
    // 25946/9545    = 2.71828182
    // 49171/18089   = 2.71828183
    // 517656/190435 = 2.71828183
}
</pre>      <h4 id="example__fibonacci"> <span class="text">Example (Fibonacci)</span>
</h4> <p>This example demonstrates how to use big.Int to compute the smallest Fibonacci number with 100 decimal digits and to test whether it is prime. </p> <p>Code:</p> <pre class="code" data-language="go">// Initialize two big ints with the first two numbers in the sequence.
a := big.NewInt(0)
b := big.NewInt(1)

// Initialize limit as 10^99, the smallest integer with 100 digits.
var limit big.Int
limit.Exp(big.NewInt(10), big.NewInt(99), nil)

// Loop while a is smaller than 1e100.
for a.Cmp(&amp;limit) &lt; 0 {
    // Compute the next Fibonacci number, storing it in a.
    a.Add(a, b)
    // Swap a and b so that b is the next number in the sequence.
    a, b = b, a
}
fmt.Println(a) // 100-digit Fibonacci number

// Test a for primality.
// (ProbablyPrimes' argument sets the number of Miller-Rabin
// rounds to be performed. 20 is a good value.)
fmt.Println(a.ProbablyPrime(20))

</pre> <p>Output:</p> <pre class="output" data-language="go">1344719667586153181419716641724567886890850696275767987106294472017884974410332069524504824747437757
false
</pre>      <h4 id="example__sqrt2"> <span class="text">Example (Sqrt2)</span>
</h4> <p>This example shows how to use big.Float to compute the square root of 2 with a precision of 200 bits, and how to print the result as a decimal number. </p> <p>Code:</p> <pre class="code" data-language="go">// We'll do computations with 200 bits of precision in the mantissa.
const prec = 200

// Compute the square root of 2 using Newton's Method. We start with
// an initial estimate for sqrt(2), and then iterate:
//     x_{n+1} = 1/2 * ( x_n + (2.0 / x_n) )

// Since Newton's Method doubles the number of correct digits at each
// iteration, we need at least log_2(prec) steps.
steps := int(math.Log2(prec))

// Initialize values we need for the computation.
two := new(big.Float).SetPrec(prec).SetInt64(2)
half := new(big.Float).SetPrec(prec).SetFloat64(0.5)

// Use 1 as the initial estimate.
x := new(big.Float).SetPrec(prec).SetInt64(1)

// We use t as a temporary variable. There's no need to set its precision
// since big.Float values with unset (== 0) precision automatically assume
// the largest precision of the arguments when used as the result (receiver)
// of a big.Float operation.
t := new(big.Float)

// Iterate.
for i := 0; i &lt;= steps; i++ {
    t.Quo(two, x)  // t = 2.0 / x_n
    t.Add(x, t)    // t = x_n + (2.0 / x_n)
    x.Mul(half, t) // x_{n+1} = 0.5 * t
}

// We can use the usual fmt.Printf verbs since big.Float implements fmt.Formatter
fmt.Printf("sqrt(2) = %.50f\n", x)

// Print the error between 2 and x*x.
t.Mul(x, x) // t = x*x
fmt.Printf("error = %e\n", t.Sub(two, t))

</pre> <p>Output:</p> <pre class="output" data-language="go">sqrt(2) = 1.41421356237309504880168872420969807856967187537695
error = 0.000000e+00
</pre>        <h2 id="pkg-index">Index </h2>  <ul id="manual-nav">
<li><a href="#pkg-constants">Constants</a></li>
<li><a href="#Jacobi">func Jacobi(x, y *Int) int</a></li>
<li><a href="#Accuracy">type Accuracy</a></li>
<li> <a href="#Accuracy.String">func (i Accuracy) String() string</a>
</li>
<li><a href="#ErrNaN">type ErrNaN</a></li>
<li> <a href="#ErrNaN.Error">func (err ErrNaN) Error() string</a>
</li>
<li><a href="#Float">type Float</a></li>
<li> <a href="#NewFloat">func NewFloat(x float64) *Float</a>
</li>
<li> <a href="#ParseFloat">func ParseFloat(s string, base int, prec uint, mode RoundingMode) (f *Float, b int, err error)</a>
</li>
<li> <a href="#Float.Abs">func (z *Float) Abs(x *Float) *Float</a>
</li>
<li> <a href="#Float.Acc">func (x *Float) Acc() Accuracy</a>
</li>
<li> <a href="#Float.Add">func (z *Float) Add(x, y *Float) *Float</a>
</li>
<li> <a href="#Float.Append">func (x *Float) Append(buf []byte, fmt byte, prec int) []byte</a>
</li>
<li> <a href="#Float.Cmp">func (x *Float) Cmp(y *Float) int</a>
</li>
<li> <a href="#Float.Copy">func (z *Float) Copy(x *Float) *Float</a>
</li>
<li> <a href="#Float.Float32">func (x *Float) Float32() (float32, Accuracy)</a>
</li>
<li> <a href="#Float.Float64">func (x *Float) Float64() (float64, Accuracy)</a>
</li>
<li> <a href="#Float.Format">func (x *Float) Format(s fmt.State, format rune)</a>
</li>
<li> <a href="#Float.GobDecode">func (z *Float) GobDecode(buf []byte) error</a>
</li>
<li> <a href="#Float.GobEncode">func (x *Float) GobEncode() ([]byte, error)</a>
</li>
<li> <a href="#Float.Int">func (x *Float) Int(z *Int) (*Int, Accuracy)</a>
</li>
<li> <a href="#Float.Int64">func (x *Float) Int64() (int64, Accuracy)</a>
</li>
<li> <a href="#Float.IsInf">func (x *Float) IsInf() bool</a>
</li>
<li> <a href="#Float.IsInt">func (x *Float) IsInt() bool</a>
</li>
<li> <a href="#Float.MantExp">func (x *Float) MantExp(mant *Float) (exp int)</a>
</li>
<li> <a href="#Float.MarshalText">func (x *Float) MarshalText() (text []byte, err error)</a>
</li>
<li> <a href="#Float.MinPrec">func (x *Float) MinPrec() uint</a>
</li>
<li> <a href="#Float.Mode">func (x *Float) Mode() RoundingMode</a>
</li>
<li> <a href="#Float.Mul">func (z *Float) Mul(x, y *Float) *Float</a>
</li>
<li> <a href="#Float.Neg">func (z *Float) Neg(x *Float) *Float</a>
</li>
<li> <a href="#Float.Parse">func (z *Float) Parse(s string, base int) (f *Float, b int, err error)</a>
</li>
<li> <a href="#Float.Prec">func (x *Float) Prec() uint</a>
</li>
<li> <a href="#Float.Quo">func (z *Float) Quo(x, y *Float) *Float</a>
</li>
<li> <a href="#Float.Rat">func (x *Float) Rat(z *Rat) (*Rat, Accuracy)</a>
</li>
<li> <a href="#Float.Scan">func (z *Float) Scan(s fmt.ScanState, ch rune) error</a>
</li>
<li> <a href="#Float.Set">func (z *Float) Set(x *Float) *Float</a>
</li>
<li> <a href="#Float.SetFloat64">func (z *Float) SetFloat64(x float64) *Float</a>
</li>
<li> <a href="#Float.SetInf">func (z *Float) SetInf(signbit bool) *Float</a>
</li>
<li> <a href="#Float.SetInt">func (z *Float) SetInt(x *Int) *Float</a>
</li>
<li> <a href="#Float.SetInt64">func (z *Float) SetInt64(x int64) *Float</a>
</li>
<li> <a href="#Float.SetMantExp">func (z *Float) SetMantExp(mant *Float, exp int) *Float</a>
</li>
<li> <a href="#Float.SetMode">func (z *Float) SetMode(mode RoundingMode) *Float</a>
</li>
<li> <a href="#Float.SetPrec">func (z *Float) SetPrec(prec uint) *Float</a>
</li>
<li> <a href="#Float.SetRat">func (z *Float) SetRat(x *Rat) *Float</a>
</li>
<li> <a href="#Float.SetString">func (z *Float) SetString(s string) (*Float, bool)</a>
</li>
<li> <a href="#Float.SetUint64">func (z *Float) SetUint64(x uint64) *Float</a>
</li>
<li> <a href="#Float.Sign">func (x *Float) Sign() int</a>
</li>
<li> <a href="#Float.Signbit">func (x *Float) Signbit() bool</a>
</li>
<li> <a href="#Float.Sqrt">func (z *Float) Sqrt(x *Float) *Float</a>
</li>
<li> <a href="#Float.String">func (x *Float) String() string</a>
</li>
<li> <a href="#Float.Sub">func (z *Float) Sub(x, y *Float) *Float</a>
</li>
<li> <a href="#Float.Text">func (x *Float) Text(format byte, prec int) string</a>
</li>
<li> <a href="#Float.Uint64">func (x *Float) Uint64() (uint64, Accuracy)</a>
</li>
<li> <a href="#Float.UnmarshalText">func (z *Float) UnmarshalText(text []byte) error</a>
</li>
<li><a href="#Int">type Int</a></li>
<li> <a href="#NewInt">func NewInt(x int64) *Int</a>
</li>
<li> <a href="#Int.Abs">func (z *Int) Abs(x *Int) *Int</a>
</li>
<li> <a href="#Int.Add">func (z *Int) Add(x, y *Int) *Int</a>
</li>
<li> <a href="#Int.And">func (z *Int) And(x, y *Int) *Int</a>
</li>
<li> <a href="#Int.AndNot">func (z *Int) AndNot(x, y *Int) *Int</a>
</li>
<li> <a href="#Int.Append">func (x *Int) Append(buf []byte, base int) []byte</a>
</li>
<li> <a href="#Int.Binomial">func (z *Int) Binomial(n, k int64) *Int</a>
</li>
<li> <a href="#Int.Bit">func (x *Int) Bit(i int) uint</a>
</li>
<li> <a href="#Int.BitLen">func (x *Int) BitLen() int</a>
</li>
<li> <a href="#Int.Bits">func (x *Int) Bits() []Word</a>
</li>
<li> <a href="#Int.Bytes">func (x *Int) Bytes() []byte</a>
</li>
<li> <a href="#Int.Cmp">func (x *Int) Cmp(y *Int) (r int)</a>
</li>
<li> <a href="#Int.CmpAbs">func (x *Int) CmpAbs(y *Int) int</a>
</li>
<li> <a href="#Int.Div">func (z *Int) Div(x, y *Int) *Int</a>
</li>
<li> <a href="#Int.DivMod">func (z *Int) DivMod(x, y, m *Int) (*Int, *Int)</a>
</li>
<li> <a href="#Int.Exp">func (z *Int) Exp(x, y, m *Int) *Int</a>
</li>
<li> <a href="#Int.FillBytes">func (x *Int) FillBytes(buf []byte) []byte</a>
</li>
<li> <a href="#Int.Float64">func (x *Int) Float64() (float64, Accuracy)</a>
</li>
<li> <a href="#Int.Format">func (x *Int) Format(s fmt.State, ch rune)</a>
</li>
<li> <a href="#Int.GCD">func (z *Int) GCD(x, y, a, b *Int) *Int</a>
</li>
<li> <a href="#Int.GobDecode">func (z *Int) GobDecode(buf []byte) error</a>
</li>
<li> <a href="#Int.GobEncode">func (x *Int) GobEncode() ([]byte, error)</a>
</li>
<li> <a href="#Int.Int64">func (x *Int) Int64() int64</a>
</li>
<li> <a href="#Int.IsInt64">func (x *Int) IsInt64() bool</a>
</li>
<li> <a href="#Int.IsUint64">func (x *Int) IsUint64() bool</a>
</li>
<li> <a href="#Int.Lsh">func (z *Int) Lsh(x *Int, n uint) *Int</a>
</li>
<li> <a href="#Int.MarshalJSON">func (x *Int) MarshalJSON() ([]byte, error)</a>
</li>
<li> <a href="#Int.MarshalText">func (x *Int) MarshalText() (text []byte, err error)</a>
</li>
<li> <a href="#Int.Mod">func (z *Int) Mod(x, y *Int) *Int</a>
</li>
<li> <a href="#Int.ModInverse">func (z *Int) ModInverse(g, n *Int) *Int</a>
</li>
<li> <a href="#Int.ModSqrt">func (z *Int) ModSqrt(x, p *Int) *Int</a>
</li>
<li> <a href="#Int.Mul">func (z *Int) Mul(x, y *Int) *Int</a>
</li>
<li> <a href="#Int.MulRange">func (z *Int) MulRange(a, b int64) *Int</a>
</li>
<li> <a href="#Int.Neg">func (z *Int) Neg(x *Int) *Int</a>
</li>
<li> <a href="#Int.Not">func (z *Int) Not(x *Int) *Int</a>
</li>
<li> <a href="#Int.Or">func (z *Int) Or(x, y *Int) *Int</a>
</li>
<li> <a href="#Int.ProbablyPrime">func (x *Int) ProbablyPrime(n int) bool</a>
</li>
<li> <a href="#Int.Quo">func (z *Int) Quo(x, y *Int) *Int</a>
</li>
<li> <a href="#Int.QuoRem">func (z *Int) QuoRem(x, y, r *Int) (*Int, *Int)</a>
</li>
<li> <a href="#Int.Rand">func (z *Int) Rand(rnd *rand.Rand, n *Int) *Int</a>
</li>
<li> <a href="#Int.Rem">func (z *Int) Rem(x, y *Int) *Int</a>
</li>
<li> <a href="#Int.Rsh">func (z *Int) Rsh(x *Int, n uint) *Int</a>
</li>
<li> <a href="#Int.Scan">func (z *Int) Scan(s fmt.ScanState, ch rune) error</a>
</li>
<li> <a href="#Int.Set">func (z *Int) Set(x *Int) *Int</a>
</li>
<li> <a href="#Int.SetBit">func (z *Int) SetBit(x *Int, i int, b uint) *Int</a>
</li>
<li> <a href="#Int.SetBits">func (z *Int) SetBits(abs []Word) *Int</a>
</li>
<li> <a href="#Int.SetBytes">func (z *Int) SetBytes(buf []byte) *Int</a>
</li>
<li> <a href="#Int.SetInt64">func (z *Int) SetInt64(x int64) *Int</a>
</li>
<li> <a href="#Int.SetString">func (z *Int) SetString(s string, base int) (*Int, bool)</a>
</li>
<li> <a href="#Int.SetUint64">func (z *Int) SetUint64(x uint64) *Int</a>
</li>
<li> <a href="#Int.Sign">func (x *Int) Sign() int</a>
</li>
<li> <a href="#Int.Sqrt">func (z *Int) Sqrt(x *Int) *Int</a>
</li>
<li> <a href="#Int.String">func (x *Int) String() string</a>
</li>
<li> <a href="#Int.Sub">func (z *Int) Sub(x, y *Int) *Int</a>
</li>
<li> <a href="#Int.Text">func (x *Int) Text(base int) string</a>
</li>
<li> <a href="#Int.TrailingZeroBits">func (x *Int) TrailingZeroBits() uint</a>
</li>
<li> <a href="#Int.Uint64">func (x *Int) Uint64() uint64</a>
</li>
<li> <a href="#Int.UnmarshalJSON">func (z *Int) UnmarshalJSON(text []byte) error</a>
</li>
<li> <a href="#Int.UnmarshalText">func (z *Int) UnmarshalText(text []byte) error</a>
</li>
<li> <a href="#Int.Xor">func (z *Int) Xor(x, y *Int) *Int</a>
</li>
<li><a href="#Rat">type Rat</a></li>
<li> <a href="#NewRat">func NewRat(a, b int64) *Rat</a>
</li>
<li> <a href="#Rat.Abs">func (z *Rat) Abs(x *Rat) *Rat</a>
</li>
<li> <a href="#Rat.Add">func (z *Rat) Add(x, y *Rat) *Rat</a>
</li>
<li> <a href="#Rat.Cmp">func (x *Rat) Cmp(y *Rat) int</a>
</li>
<li> <a href="#Rat.Denom">func (x *Rat) Denom() *Int</a>
</li>
<li> <a href="#Rat.Float32">func (x *Rat) Float32() (f float32, exact bool)</a>
</li>
<li> <a href="#Rat.Float64">func (x *Rat) Float64() (f float64, exact bool)</a>
</li>
<li> <a href="#Rat.FloatPrec">func (x *Rat) FloatPrec() (n int, exact bool)</a>
</li>
<li> <a href="#Rat.FloatString">func (x *Rat) FloatString(prec int) string</a>
</li>
<li> <a href="#Rat.GobDecode">func (z *Rat) GobDecode(buf []byte) error</a>
</li>
<li> <a href="#Rat.GobEncode">func (x *Rat) GobEncode() ([]byte, error)</a>
</li>
<li> <a href="#Rat.Inv">func (z *Rat) Inv(x *Rat) *Rat</a>
</li>
<li> <a href="#Rat.IsInt">func (x *Rat) IsInt() bool</a>
</li>
<li> <a href="#Rat.MarshalText">func (x *Rat) MarshalText() (text []byte, err error)</a>
</li>
<li> <a href="#Rat.Mul">func (z *Rat) Mul(x, y *Rat) *Rat</a>
</li>
<li> <a href="#Rat.Neg">func (z *Rat) Neg(x *Rat) *Rat</a>
</li>
<li> <a href="#Rat.Num">func (x *Rat) Num() *Int</a>
</li>
<li> <a href="#Rat.Quo">func (z *Rat) Quo(x, y *Rat) *Rat</a>
</li>
<li> <a href="#Rat.RatString">func (x *Rat) RatString() string</a>
</li>
<li> <a href="#Rat.Scan">func (z *Rat) Scan(s fmt.ScanState, ch rune) error</a>
</li>
<li> <a href="#Rat.Set">func (z *Rat) Set(x *Rat) *Rat</a>
</li>
<li> <a href="#Rat.SetFloat64">func (z *Rat) SetFloat64(f float64) *Rat</a>
</li>
<li> <a href="#Rat.SetFrac">func (z *Rat) SetFrac(a, b *Int) *Rat</a>
</li>
<li> <a href="#Rat.SetFrac64">func (z *Rat) SetFrac64(a, b int64) *Rat</a>
</li>
<li> <a href="#Rat.SetInt">func (z *Rat) SetInt(x *Int) *Rat</a>
</li>
<li> <a href="#Rat.SetInt64">func (z *Rat) SetInt64(x int64) *Rat</a>
</li>
<li> <a href="#Rat.SetString">func (z *Rat) SetString(s string) (*Rat, bool)</a>
</li>
<li> <a href="#Rat.SetUint64">func (z *Rat) SetUint64(x uint64) *Rat</a>
</li>
<li> <a href="#Rat.Sign">func (x *Rat) Sign() int</a>
</li>
<li> <a href="#Rat.String">func (x *Rat) String() string</a>
</li>
<li> <a href="#Rat.Sub">func (z *Rat) Sub(x, y *Rat) *Rat</a>
</li>
<li> <a href="#Rat.UnmarshalText">func (z *Rat) UnmarshalText(text []byte) error</a>
</li>
<li><a href="#RoundingMode">type RoundingMode</a></li>
<li> <a href="#RoundingMode.String">func (i RoundingMode) String() string</a>
</li>
<li><a href="#Word">type Word</a></li>
</ul> <div id="pkg-examples"> <h3>Examples</h3>  <dl> <dd><a class="exampleLink" href="#example_Float_Add">Float.Add</a></dd> <dd><a class="exampleLink" href="#example_Float_Cmp">Float.Cmp</a></dd> <dd><a class="exampleLink" href="#example_Float_Scan">Float.Scan</a></dd> <dd><a class="exampleLink" href="#example_Float_SetString">Float.SetString</a></dd> <dd><a class="exampleLink" href="#example_Float_shift">Float (Shift)</a></dd> <dd><a class="exampleLink" href="#example_Int_Scan">Int.Scan</a></dd> <dd><a class="exampleLink" href="#example_Int_SetString">Int.SetString</a></dd> <dd><a class="exampleLink" href="#example_Rat_Scan">Rat.Scan</a></dd> <dd><a class="exampleLink" href="#example_Rat_SetString">Rat.SetString</a></dd> <dd><a class="exampleLink" href="#example_RoundingMode">RoundingMode</a></dd> <dd><a class="exampleLink" href="#example__eConvergents">Package (EConvergents)</a></dd> <dd><a class="exampleLink" href="#example__fibonacci">Package (Fibonacci)</a></dd> <dd><a class="exampleLink" href="#example__sqrt2">Package (Sqrt2)</a></dd> </dl> </div> <h3>Package files</h3> <p>  <span>accuracy_string.go</span> <span>arith.go</span> <span>arith_amd64.go</span> <span>arith_decl.go</span> <span>decimal.go</span> <span>doc.go</span> <span>float.go</span> <span>floatconv.go</span> <span>floatmarsh.go</span> <span>ftoa.go</span> <span>int.go</span> <span>intconv.go</span> <span>intmarsh.go</span> <span>nat.go</span> <span>natconv.go</span> <span>natdiv.go</span> <span>prime.go</span> <span>rat.go</span> <span>ratconv.go</span> <span>ratmarsh.go</span> <span>roundingmode_string.go</span> <span>sqrt.go</span>  </p>   <h2 id="pkg-constants">Constants</h2> <p>Exponent and precision limits. </p>
<pre data-language="go">const (
    MaxExp  = math.MaxInt32  // largest supported exponent
    MinExp  = math.MinInt32  // smallest supported exponent
    MaxPrec = math.MaxUint32 // largest (theoretically) supported precision; likely memory-limited
)</pre> <p>MaxBase is the largest number base accepted for string conversions. </p>
<pre data-language="go">const MaxBase = 10 + ('z' - 'a' + 1) + ('Z' - 'A' + 1)</pre> <h2 id="Jacobi">func <span>Jacobi</span>  <span title="Added in Go 1.5">1.5</span> </h2> <pre data-language="go">func Jacobi(x, y *Int) int</pre> <p>Jacobi returns the Jacobi symbol (x/y), either +1, -1, or 0. The y argument must be an odd integer. </p>
<h2 id="Accuracy">type <span>Accuracy</span>  <span title="Added in Go 1.5">1.5</span> </h2> <p>Accuracy describes the rounding error produced by the most recent operation that generated a <a href="#Float">Float</a> value, relative to the exact value. </p>
<pre data-language="go">type Accuracy int8</pre> <p>Constants describing the <a href="#Accuracy">Accuracy</a> of a <a href="#Float">Float</a>. </p>
<pre data-language="go">const (
    Below Accuracy = -1
    Exact Accuracy = 0
    Above Accuracy = +1
)</pre> <h3 id="Accuracy.String">func (Accuracy) <span>String</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (i Accuracy) String() string</pre> <h2 id="ErrNaN">type <span>ErrNaN</span>  <span title="Added in Go 1.5">1.5</span> </h2> <p>An ErrNaN panic is raised by a <a href="#Float">Float</a> operation that would lead to a NaN under IEEE-754 rules. An ErrNaN implements the error interface. </p>
<pre data-language="go">type ErrNaN struct {
    // contains filtered or unexported fields
}
</pre> <h3 id="ErrNaN.Error">func (ErrNaN) <span>Error</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (err ErrNaN) Error() string</pre> <h2 id="Float">type <span>Float</span>  <span title="Added in Go 1.5">1.5</span> </h2> <p>A nonzero finite Float represents a multi-precision floating point number </p>
<pre data-language="go">sign × mantissa × 2**exponent
</pre> <p>with 0.5 &lt;= mantissa &lt; 1.0, and MinExp &lt;= exponent &lt;= MaxExp. A Float may also be zero (+0, -0) or infinite (+Inf, -Inf). All Floats are ordered, and the ordering of two Floats x and y is defined by x.Cmp(y). </p>
<p>Each Float value also has a precision, rounding mode, and accuracy. The precision is the maximum number of mantissa bits available to represent the value. The rounding mode specifies how a result should be rounded to fit into the mantissa bits, and accuracy describes the rounding error with respect to the exact result. </p>
<p>Unless specified otherwise, all operations (including setters) that specify a *Float variable for the result (usually via the receiver with the exception of <a href="#Float.MantExp">Float.MantExp</a>), round the numeric result according to the precision and rounding mode of the result variable. </p>
<p>If the provided result precision is 0 (see below), it is set to the precision of the argument with the largest precision value before any rounding takes place, and the rounding mode remains unchanged. Thus, uninitialized Floats provided as result arguments will have their precision set to a reasonable value determined by the operands, and their mode is the zero value for RoundingMode (ToNearestEven). </p>
<p>By setting the desired precision to 24 or 53 and using matching rounding mode (typically <a href="#ToNearestEven">ToNearestEven</a>), Float operations produce the same results as the corresponding float32 or float64 IEEE-754 arithmetic for operands that correspond to normal (i.e., not denormal) float32 or float64 numbers. Exponent underflow and overflow lead to a 0 or an Infinity for different values than IEEE-754 because Float exponents have a much larger range. </p>
<p>The zero (uninitialized) value for a Float is ready to use and represents the number +0.0 exactly, with precision 0 and rounding mode <a href="#ToNearestEven">ToNearestEven</a>. </p>
<p>Operations always take pointer arguments (*Float) rather than Float values, and each unique Float value requires its own unique *Float pointer. To "copy" a Float value, an existing (or newly allocated) Float must be set to a new value using the <a href="#Float.Set">Float.Set</a> method; shallow copies of Floats are not supported and may lead to errors. </p>
<pre data-language="go">type Float struct {
    // contains filtered or unexported fields
}
</pre>    <h4 id="example_Float_shift"> <span class="text">Example (Shift)</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">// Implement Float "shift" by modifying the (binary) exponents directly.
for s := -5; s &lt;= 5; s++ {
    x := big.NewFloat(0.5)
    x.SetMantExp(x, x.MantExp(nil)+s) // shift x by s
    fmt.Println(x)
}
</pre> <p>Output:</p> <pre class="output" data-language="go">0.015625
0.03125
0.0625
0.125
0.25
0.5
1
2
4
8
16
</pre>   <h3 id="NewFloat">func <span>NewFloat</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func NewFloat(x float64) *Float</pre> <p>NewFloat allocates and returns a new <a href="#Float">Float</a> set to x, with precision 53 and rounding mode <a href="#ToNearestEven">ToNearestEven</a>. NewFloat panics with <a href="#ErrNaN">ErrNaN</a> if x is a NaN. </p>
<h3 id="ParseFloat">func <span>ParseFloat</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func ParseFloat(s string, base int, prec uint, mode RoundingMode) (f *Float, b int, err error)</pre> <p>ParseFloat is like f.Parse(s, base) with f set to the given precision and rounding mode. </p>
<h3 id="Float.Abs">func (*Float) <span>Abs</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (z *Float) Abs(x *Float) *Float</pre> <p>Abs sets z to the (possibly rounded) value |x| (the absolute value of x) and returns z. </p>
<h3 id="Float.Acc">func (*Float) <span>Acc</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (x *Float) Acc() Accuracy</pre> <p>Acc returns the accuracy of x produced by the most recent operation, unless explicitly documented otherwise by that operation. </p>
<h3 id="Float.Add">func (*Float) <span>Add</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (z *Float) Add(x, y *Float) *Float</pre> <p>Add sets z to the rounded sum x+y and returns z. If z's precision is 0, it is changed to the larger of x's or y's precision before the operation. Rounding is performed according to z's precision and rounding mode; and z's accuracy reports the result error relative to the exact (not rounded) result. Add panics with <a href="#ErrNaN">ErrNaN</a> if x and y are infinities with opposite signs. The value of z is undefined in that case. </p>   <h4 id="example_Float_Add"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">// Operate on numbers of different precision.
var x, y, z big.Float
x.SetInt64(1000)          // x is automatically set to 64bit precision
y.SetFloat64(2.718281828) // y is automatically set to 53bit precision
z.SetPrec(32)
z.Add(&amp;x, &amp;y)
fmt.Printf("x = %.10g (%s, prec = %d, acc = %s)\n", &amp;x, x.Text('p', 0), x.Prec(), x.Acc())
fmt.Printf("y = %.10g (%s, prec = %d, acc = %s)\n", &amp;y, y.Text('p', 0), y.Prec(), y.Acc())
fmt.Printf("z = %.10g (%s, prec = %d, acc = %s)\n", &amp;z, z.Text('p', 0), z.Prec(), z.Acc())
</pre> <p>Output:</p> <pre class="output" data-language="go">x = 1000 (0x.fap+10, prec = 64, acc = Exact)
y = 2.718281828 (0x.adf85458248cd8p+2, prec = 53, acc = Exact)
z = 1002.718282 (0x.faadf854p+10, prec = 32, acc = Below)
</pre>   <h3 id="Float.Append">func (*Float) <span>Append</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (x *Float) Append(buf []byte, fmt byte, prec int) []byte</pre> <p>Append appends to buf the string form of the floating-point number x, as generated by x.Text, and returns the extended buffer. </p>
<h3 id="Float.Cmp">func (*Float) <span>Cmp</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (x *Float) Cmp(y *Float) int</pre> <p>Cmp compares x and y and returns: </p>
<pre data-language="go">-1 if x &lt;  y
 0 if x == y (incl. -0 == 0, -Inf == -Inf, and +Inf == +Inf)
+1 if x &gt;  y
</pre>    <h4 id="example_Float_Cmp"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">inf := math.Inf(1)
zero := 0.0

operands := []float64{-inf, -1.2, -zero, 0, +1.2, +inf}

fmt.Println("   x     y  cmp")
fmt.Println("---------------")
for _, x64 := range operands {
    x := big.NewFloat(x64)
    for _, y64 := range operands {
        y := big.NewFloat(y64)
        fmt.Printf("%4g  %4g  %3d\n", x, y, x.Cmp(y))
    }
    fmt.Println()
}

</pre> <p>Output:</p> <pre class="output" data-language="go">   x     y  cmp
---------------
-Inf  -Inf    0
-Inf  -1.2   -1
-Inf    -0   -1
-Inf     0   -1
-Inf   1.2   -1
-Inf  +Inf   -1

-1.2  -Inf    1
-1.2  -1.2    0
-1.2    -0   -1
-1.2     0   -1
-1.2   1.2   -1
-1.2  +Inf   -1

  -0  -Inf    1
  -0  -1.2    1
  -0    -0    0
  -0     0    0
  -0   1.2   -1
  -0  +Inf   -1

   0  -Inf    1
   0  -1.2    1
   0    -0    0
   0     0    0
   0   1.2   -1
   0  +Inf   -1

 1.2  -Inf    1
 1.2  -1.2    1
 1.2    -0    1
 1.2     0    1
 1.2   1.2    0
 1.2  +Inf   -1

+Inf  -Inf    1
+Inf  -1.2    1
+Inf    -0    1
+Inf     0    1
+Inf   1.2    1
+Inf  +Inf    0
</pre>   <h3 id="Float.Copy">func (*Float) <span>Copy</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (z *Float) Copy(x *Float) *Float</pre> <p>Copy sets z to x, with the same precision, rounding mode, and accuracy as x, and returns z. x is not changed even if z and x are the same. </p>
<h3 id="Float.Float32">func (*Float) <span>Float32</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (x *Float) Float32() (float32, Accuracy)</pre> <p>Float32 returns the float32 value nearest to x. If x is too small to be represented by a float32 (|x| &lt; <span>math.SmallestNonzeroFloat32</span>), the result is (0, <a href="#Below">Below</a>) or (-0, <a href="#Above">Above</a>), respectively, depending on the sign of x. If x is too large to be represented by a float32 (|x| &gt; <span>math.MaxFloat32</span>), the result is (+Inf, <a href="#Above">Above</a>) or (-Inf, <a href="#Below">Below</a>), depending on the sign of x. </p>
<h3 id="Float.Float64">func (*Float) <span>Float64</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (x *Float) Float64() (float64, Accuracy)</pre> <p>Float64 returns the float64 value nearest to x. If x is too small to be represented by a float64 (|x| &lt; <span>math.SmallestNonzeroFloat64</span>), the result is (0, <a href="#Below">Below</a>) or (-0, <a href="#Above">Above</a>), respectively, depending on the sign of x. If x is too large to be represented by a float64 (|x| &gt; <span>math.MaxFloat64</span>), the result is (+Inf, <a href="#Above">Above</a>) or (-Inf, <a href="#Below">Below</a>), depending on the sign of x. </p>
<h3 id="Float.Format">func (*Float) <span>Format</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (x *Float) Format(s fmt.State, format rune)</pre> <p>Format implements <span>fmt.Formatter</span>. It accepts all the regular formats for floating-point numbers ('b', 'e', 'E', 'f', 'F', 'g', 'G', 'x') as well as 'p' and 'v'. See (*Float).Text for the interpretation of 'p'. The 'v' format is handled like 'g'. Format also supports specification of the minimum precision in digits, the output field width, as well as the format flags '+' and ' ' for sign control, '0' for space or zero padding, and '-' for left or right justification. See the fmt package for details. </p>
<h3 id="Float.GobDecode">func (*Float) <span>GobDecode</span>  <span title="Added in Go 1.7">1.7</span> </h3> <pre data-language="go">func (z *Float) GobDecode(buf []byte) error</pre> <p>GobDecode implements the <span>encoding/gob.GobDecoder</span> interface. The result is rounded per the precision and rounding mode of z unless z's precision is 0, in which case z is set exactly to the decoded value. </p>
<h3 id="Float.GobEncode">func (*Float) <span>GobEncode</span>  <span title="Added in Go 1.7">1.7</span> </h3> <pre data-language="go">func (x *Float) GobEncode() ([]byte, error)</pre> <p>GobEncode implements the <span>encoding/gob.GobEncoder</span> interface. The <a href="#Float">Float</a> value and all its attributes (precision, rounding mode, accuracy) are marshaled. </p>
<h3 id="Float.Int">func (*Float) <span>Int</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (x *Float) Int(z *Int) (*Int, Accuracy)</pre> <p>Int returns the result of truncating x towards zero; or nil if x is an infinity. The result is <a href="#Exact">Exact</a> if x.IsInt(); otherwise it is <a href="#Below">Below</a> for x &gt; 0, and <a href="#Above">Above</a> for x &lt; 0. If a non-nil *<a href="#Int">Int</a> argument z is provided, <a href="#Int">Int</a> stores the result in z instead of allocating a new <a href="#Int">Int</a>. </p>
<h3 id="Float.Int64">func (*Float) <span>Int64</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (x *Float) Int64() (int64, Accuracy)</pre> <p>Int64 returns the integer resulting from truncating x towards zero. If <span>math.MinInt64</span> &lt;= x &lt;= <span>math.MaxInt64</span>, the result is <a href="#Exact">Exact</a> if x is an integer, and <a href="#Above">Above</a> (x &lt; 0) or <a href="#Below">Below</a> (x &gt; 0) otherwise. The result is (<span>math.MinInt64</span>, <a href="#Above">Above</a>) for x &lt; <span>math.MinInt64</span>, and (<span>math.MaxInt64</span>, <a href="#Below">Below</a>) for x &gt; <span>math.MaxInt64</span>. </p>
<h3 id="Float.IsInf">func (*Float) <span>IsInf</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (x *Float) IsInf() bool</pre> <p>IsInf reports whether x is +Inf or -Inf. </p>
<h3 id="Float.IsInt">func (*Float) <span>IsInt</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (x *Float) IsInt() bool</pre> <p>IsInt reports whether x is an integer. ±Inf values are not integers. </p>
<h3 id="Float.MantExp">func (*Float) <span>MantExp</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (x *Float) MantExp(mant *Float) (exp int)</pre> <p>MantExp breaks x into its mantissa and exponent components and returns the exponent. If a non-nil mant argument is provided its value is set to the mantissa of x, with the same precision and rounding mode as x. The components satisfy x == mant × 2**exp, with 0.5 &lt;= |mant| &lt; 1.0. Calling MantExp with a nil argument is an efficient way to get the exponent of the receiver. </p>
<p>Special cases are: </p>
<pre data-language="go">(  ±0).MantExp(mant) = 0, with mant set to   ±0
(±Inf).MantExp(mant) = 0, with mant set to ±Inf
</pre> <p>x and mant may be the same in which case x is set to its mantissa value. </p>
<h3 id="Float.MarshalText">func (*Float) <span>MarshalText</span>  <span title="Added in Go 1.6">1.6</span> </h3> <pre data-language="go">func (x *Float) MarshalText() (text []byte, err error)</pre> <p>MarshalText implements the <span>encoding.TextMarshaler</span> interface. Only the <a href="#Float">Float</a> value is marshaled (in full precision), other attributes such as precision or accuracy are ignored. </p>
<h3 id="Float.MinPrec">func (*Float) <span>MinPrec</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (x *Float) MinPrec() uint</pre> <p>MinPrec returns the minimum precision required to represent x exactly (i.e., the smallest prec before x.SetPrec(prec) would start rounding x). The result is 0 for |x| == 0 and |x| == Inf. </p>
<h3 id="Float.Mode">func (*Float) <span>Mode</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (x *Float) Mode() RoundingMode</pre> <p>Mode returns the rounding mode of x. </p>
<h3 id="Float.Mul">func (*Float) <span>Mul</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (z *Float) Mul(x, y *Float) *Float</pre> <p>Mul sets z to the rounded product x*y and returns z. Precision, rounding, and accuracy reporting are as for <a href="#Float.Add">Float.Add</a>. Mul panics with <a href="#ErrNaN">ErrNaN</a> if one operand is zero and the other operand an infinity. The value of z is undefined in that case. </p>
<h3 id="Float.Neg">func (*Float) <span>Neg</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (z *Float) Neg(x *Float) *Float</pre> <p>Neg sets z to the (possibly rounded) value of x with its sign negated, and returns z. </p>
<h3 id="Float.Parse">func (*Float) <span>Parse</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (z *Float) Parse(s string, base int) (f *Float, b int, err error)</pre> <p>Parse parses s which must contain a text representation of a floating- point number with a mantissa in the given conversion base (the exponent is always a decimal number), or a string representing an infinite value. </p>
<p>For base 0, an underscore character “_” may appear between a base prefix and an adjacent digit, and between successive digits; such underscores do not change the value of the number, or the returned digit count. Incorrect placement of underscores is reported as an error if there are no other errors. If base != 0, underscores are not recognized and thus terminate scanning like any other character that is not a valid radix point or digit. </p>
<p>It sets z to the (possibly rounded) value of the corresponding floating- point value, and returns z, the actual base b, and an error err, if any. The entire string (not just a prefix) must be consumed for success. If z's precision is 0, it is changed to 64 before rounding takes effect. The number must be of the form: </p>
<pre data-language="go">number    = [ sign ] ( float | "inf" | "Inf" ) .
sign      = "+" | "-" .
float     = ( mantissa | prefix pmantissa ) [ exponent ] .
prefix    = "0" [ "b" | "B" | "o" | "O" | "x" | "X" ] .
mantissa  = digits "." [ digits ] | digits | "." digits .
pmantissa = [ "_" ] digits "." [ digits ] | [ "_" ] digits | "." digits .
exponent  = ( "e" | "E" | "p" | "P" ) [ sign ] digits .
digits    = digit { [ "_" ] digit } .
digit     = "0" ... "9" | "a" ... "z" | "A" ... "Z" .
</pre> <p>The base argument must be 0, 2, 8, 10, or 16. Providing an invalid base argument will lead to a run-time panic. </p>
<p>For base 0, the number prefix determines the actual base: A prefix of “0b” or “0B” selects base 2, “0o” or “0O” selects base 8, and “0x” or “0X” selects base 16. Otherwise, the actual base is 10 and no prefix is accepted. The octal prefix "0" is not supported (a leading "0" is simply considered a "0"). </p>
<p>A "p" or "P" exponent indicates a base 2 (rather than base 10) exponent; for instance, "0x1.fffffffffffffp1023" (using base 0) represents the maximum float64 value. For hexadecimal mantissae, the exponent character must be one of 'p' or 'P', if present (an "e" or "E" exponent indicator cannot be distinguished from a mantissa digit). </p>
<p>The returned *Float f is nil and the value of z is valid but not defined if an error is reported. </p>
<h3 id="Float.Prec">func (*Float) <span>Prec</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (x *Float) Prec() uint</pre> <p>Prec returns the mantissa precision of x in bits. The result may be 0 for |x| == 0 and |x| == Inf. </p>
<h3 id="Float.Quo">func (*Float) <span>Quo</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (z *Float) Quo(x, y *Float) *Float</pre> <p>Quo sets z to the rounded quotient x/y and returns z. Precision, rounding, and accuracy reporting are as for <a href="#Float.Add">Float.Add</a>. Quo panics with <a href="#ErrNaN">ErrNaN</a> if both operands are zero or infinities. The value of z is undefined in that case. </p>
<h3 id="Float.Rat">func (*Float) <span>Rat</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (x *Float) Rat(z *Rat) (*Rat, Accuracy)</pre> <p>Rat returns the rational number corresponding to x; or nil if x is an infinity. The result is <a href="#Exact">Exact</a> if x is not an Inf. If a non-nil *<a href="#Rat">Rat</a> argument z is provided, <a href="#Rat">Rat</a> stores the result in z instead of allocating a new <a href="#Rat">Rat</a>. </p>
<h3 id="Float.Scan">func (*Float) <span>Scan</span>  <span title="Added in Go 1.8">1.8</span> </h3> <pre data-language="go">func (z *Float) Scan(s fmt.ScanState, ch rune) error</pre> <p>Scan is a support routine for <span>fmt.Scanner</span>; it sets z to the value of the scanned number. It accepts formats whose verbs are supported by <span>fmt.Scan</span> for floating point values, which are: 'b' (binary), 'e', 'E', 'f', 'F', 'g' and 'G'. Scan doesn't handle ±Inf. </p>   <h4 id="example_Float_Scan"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">// The Scan function is rarely used directly;
// the fmt package recognizes it as an implementation of fmt.Scanner.
f := new(big.Float)
_, err := fmt.Sscan("1.19282e99", f)
if err != nil {
    log.Println("error scanning value:", err)
} else {
    fmt.Println(f)
}
</pre> <p>Output:</p> <pre class="output" data-language="go">1.19282e+99
</pre>   <h3 id="Float.Set">func (*Float) <span>Set</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (z *Float) Set(x *Float) *Float</pre> <p>Set sets z to the (possibly rounded) value of x and returns z. If z's precision is 0, it is changed to the precision of x before setting z (and rounding will have no effect). Rounding is performed according to z's precision and rounding mode; and z's accuracy reports the result error relative to the exact (not rounded) result. </p>
<h3 id="Float.SetFloat64">func (*Float) <span>SetFloat64</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (z *Float) SetFloat64(x float64) *Float</pre> <p>SetFloat64 sets z to the (possibly rounded) value of x and returns z. If z's precision is 0, it is changed to 53 (and rounding will have no effect). SetFloat64 panics with <a href="#ErrNaN">ErrNaN</a> if x is a NaN. </p>
<h3 id="Float.SetInf">func (*Float) <span>SetInf</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (z *Float) SetInf(signbit bool) *Float</pre> <p>SetInf sets z to the infinite Float -Inf if signbit is set, or +Inf if signbit is not set, and returns z. The precision of z is unchanged and the result is always <a href="#Exact">Exact</a>. </p>
<h3 id="Float.SetInt">func (*Float) <span>SetInt</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (z *Float) SetInt(x *Int) *Float</pre> <p>SetInt sets z to the (possibly rounded) value of x and returns z. If z's precision is 0, it is changed to the larger of x.BitLen() or 64 (and rounding will have no effect). </p>
<h3 id="Float.SetInt64">func (*Float) <span>SetInt64</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (z *Float) SetInt64(x int64) *Float</pre> <p>SetInt64 sets z to the (possibly rounded) value of x and returns z. If z's precision is 0, it is changed to 64 (and rounding will have no effect). </p>
<h3 id="Float.SetMantExp">func (*Float) <span>SetMantExp</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (z *Float) SetMantExp(mant *Float, exp int) *Float</pre> <p>SetMantExp sets z to mant × 2**exp and returns z. The result z has the same precision and rounding mode as mant. SetMantExp is an inverse of <a href="#Float.MantExp">Float.MantExp</a> but does not require 0.5 &lt;= |mant| &lt; 1.0. Specifically, for a given x of type *<a href="#Float">Float</a>, SetMantExp relates to <a href="#Float.MantExp">Float.MantExp</a> as follows: </p>
<pre data-language="go">mant := new(Float)
new(Float).SetMantExp(mant, x.MantExp(mant)).Cmp(x) == 0
</pre> <p>Special cases are: </p>
<pre data-language="go">z.SetMantExp(  ±0, exp) =   ±0
z.SetMantExp(±Inf, exp) = ±Inf
</pre> <p>z and mant may be the same in which case z's exponent is set to exp. </p>
<h3 id="Float.SetMode">func (*Float) <span>SetMode</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (z *Float) SetMode(mode RoundingMode) *Float</pre> <p>SetMode sets z's rounding mode to mode and returns an exact z. z remains unchanged otherwise. z.SetMode(z.Mode()) is a cheap way to set z's accuracy to <a href="#Exact">Exact</a>. </p>
<h3 id="Float.SetPrec">func (*Float) <span>SetPrec</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (z *Float) SetPrec(prec uint) *Float</pre> <p>SetPrec sets z's precision to prec and returns the (possibly) rounded value of z. Rounding occurs according to z's rounding mode if the mantissa cannot be represented in prec bits without loss of precision. SetPrec(0) maps all finite values to ±0; infinite values remain unchanged. If prec &gt; <a href="#MaxPrec">MaxPrec</a>, it is set to <a href="#MaxPrec">MaxPrec</a>. </p>
<h3 id="Float.SetRat">func (*Float) <span>SetRat</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (z *Float) SetRat(x *Rat) *Float</pre> <p>SetRat sets z to the (possibly rounded) value of x and returns z. If z's precision is 0, it is changed to the largest of a.BitLen(), b.BitLen(), or 64; with x = a/b. </p>
<h3 id="Float.SetString">func (*Float) <span>SetString</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (z *Float) SetString(s string) (*Float, bool)</pre> <p>SetString sets z to the value of s and returns z and a boolean indicating success. s must be a floating-point number of the same format as accepted by <a href="#Float.Parse">Float.Parse</a>, with base argument 0. The entire string (not just a prefix) must be valid for success. If the operation failed, the value of z is undefined but the returned value is nil. </p>   <h4 id="example_Float_SetString"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">f := new(big.Float)
f.SetString("3.14159")
fmt.Println(f)
</pre> <p>Output:</p> <pre class="output" data-language="go">3.14159
</pre>   <h3 id="Float.SetUint64">func (*Float) <span>SetUint64</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (z *Float) SetUint64(x uint64) *Float</pre> <p>SetUint64 sets z to the (possibly rounded) value of x and returns z. If z's precision is 0, it is changed to 64 (and rounding will have no effect). </p>
<h3 id="Float.Sign">func (*Float) <span>Sign</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (x *Float) Sign() int</pre> <p>Sign returns: </p>
<pre data-language="go">-1 if x &lt;   0
 0 if x is ±0
+1 if x &gt;   0
</pre> <h3 id="Float.Signbit">func (*Float) <span>Signbit</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (x *Float) Signbit() bool</pre> <p>Signbit reports whether x is negative or negative zero. </p>
<h3 id="Float.Sqrt">func (*Float) <span>Sqrt</span>  <span title="Added in Go 1.10">1.10</span> </h3> <pre data-language="go">func (z *Float) Sqrt(x *Float) *Float</pre> <p>Sqrt sets z to the rounded square root of x, and returns it. </p>
<p>If z's precision is 0, it is changed to x's precision before the operation. Rounding is performed according to z's precision and rounding mode, but z's accuracy is not computed. Specifically, the result of z.Acc() is undefined. </p>
<p>The function panics if z &lt; 0. The value of z is undefined in that case. </p>
<h3 id="Float.String">func (*Float) <span>String</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (x *Float) String() string</pre> <p>String formats x like x.Text('g', 10). (String must be called explicitly, <a href="#Float.Format">Float.Format</a> does not support %s verb.) </p>
<h3 id="Float.Sub">func (*Float) <span>Sub</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (z *Float) Sub(x, y *Float) *Float</pre> <p>Sub sets z to the rounded difference x-y and returns z. Precision, rounding, and accuracy reporting are as for <a href="#Float.Add">Float.Add</a>. Sub panics with <a href="#ErrNaN">ErrNaN</a> if x and y are infinities with equal signs. The value of z is undefined in that case. </p>
<h3 id="Float.Text">func (*Float) <span>Text</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (x *Float) Text(format byte, prec int) string</pre> <p>Text converts the floating-point number x to a string according to the given format and precision prec. The format is one of: </p>
<pre data-language="go">'e'	-d.dddde±dd, decimal exponent, at least two (possibly 0) exponent digits
'E'	-d.ddddE±dd, decimal exponent, at least two (possibly 0) exponent digits
'f'	-ddddd.dddd, no exponent
'g'	like 'e' for large exponents, like 'f' otherwise
'G'	like 'E' for large exponents, like 'f' otherwise
'x'	-0xd.dddddp±dd, hexadecimal mantissa, decimal power of two exponent
'p'	-0x.dddp±dd, hexadecimal mantissa, decimal power of two exponent (non-standard)
'b'	-ddddddp±dd, decimal mantissa, decimal power of two exponent (non-standard)
</pre> <p>For the power-of-two exponent formats, the mantissa is printed in normalized form: </p>
<pre data-language="go">'x'	hexadecimal mantissa in [1, 2), or 0
'p'	hexadecimal mantissa in [½, 1), or 0
'b'	decimal integer mantissa using x.Prec() bits, or 0
</pre> <p>Note that the 'x' form is the one used by most other languages and libraries. </p>
<p>If format is a different character, Text returns a "%" followed by the unrecognized format character. </p>
<p>The precision prec controls the number of digits (excluding the exponent) printed by the 'e', 'E', 'f', 'g', 'G', and 'x' formats. For 'e', 'E', 'f', and 'x', it is the number of digits after the decimal point. For 'g' and 'G' it is the total number of digits. A negative precision selects the smallest number of decimal digits necessary to identify the value x uniquely using x.Prec() mantissa bits. The prec value is ignored for the 'b' and 'p' formats. </p>
<h3 id="Float.Uint64">func (*Float) <span>Uint64</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (x *Float) Uint64() (uint64, Accuracy)</pre> <p>Uint64 returns the unsigned integer resulting from truncating x towards zero. If 0 &lt;= x &lt;= math.MaxUint64, the result is <a href="#Exact">Exact</a> if x is an integer and <a href="#Below">Below</a> otherwise. The result is (0, <a href="#Above">Above</a>) for x &lt; 0, and (<span>math.MaxUint64</span>, <a href="#Below">Below</a>) for x &gt; <span>math.MaxUint64</span>. </p>
<h3 id="Float.UnmarshalText">func (*Float) <span>UnmarshalText</span>  <span title="Added in Go 1.6">1.6</span> </h3> <pre data-language="go">func (z *Float) UnmarshalText(text []byte) error</pre> <p>UnmarshalText implements the <span>encoding.TextUnmarshaler</span> interface. The result is rounded per the precision and rounding mode of z. If z's precision is 0, it is changed to 64 before rounding takes effect. </p>
<h2 id="Int">type <span>Int</span>  </h2> <p>An Int represents a signed multi-precision integer. The zero value for an Int represents the value 0. </p>
<p>Operations always take pointer arguments (*Int) rather than Int values, and each unique Int value requires its own unique *Int pointer. To "copy" an Int value, an existing (or newly allocated) Int must be set to a new value using the <a href="#Int.Set">Int.Set</a> method; shallow copies of Ints are not supported and may lead to errors. </p>
<p>Note that methods may leak the Int's value through timing side-channels. Because of this and because of the scope and complexity of the implementation, Int is not well-suited to implement cryptographic operations. The standard library avoids exposing non-trivial Int methods to attacker-controlled inputs and the determination of whether a bug in math/big is considered a security vulnerability might depend on the impact on the standard library. </p>
<pre data-language="go">type Int struct {
    // contains filtered or unexported fields
}
</pre> <h3 id="NewInt">func <span>NewInt</span>  </h3> <pre data-language="go">func NewInt(x int64) *Int</pre> <p>NewInt allocates and returns a new <a href="#Int">Int</a> set to x. </p>
<h3 id="Int.Abs">func (*Int) <span>Abs</span>  </h3> <pre data-language="go">func (z *Int) Abs(x *Int) *Int</pre> <p>Abs sets z to |x| (the absolute value of x) and returns z. </p>
<h3 id="Int.Add">func (*Int) <span>Add</span>  </h3> <pre data-language="go">func (z *Int) Add(x, y *Int) *Int</pre> <p>Add sets z to the sum x+y and returns z. </p>
<h3 id="Int.And">func (*Int) <span>And</span>  </h3> <pre data-language="go">func (z *Int) And(x, y *Int) *Int</pre> <p>And sets z = x &amp; y and returns z. </p>
<h3 id="Int.AndNot">func (*Int) <span>AndNot</span>  </h3> <pre data-language="go">func (z *Int) AndNot(x, y *Int) *Int</pre> <p>AndNot sets z = x &amp;^ y and returns z. </p>
<h3 id="Int.Append">func (*Int) <span>Append</span>  <span title="Added in Go 1.6">1.6</span> </h3> <pre data-language="go">func (x *Int) Append(buf []byte, base int) []byte</pre> <p>Append appends the string representation of x, as generated by x.Text(base), to buf and returns the extended buffer. </p>
<h3 id="Int.Binomial">func (*Int) <span>Binomial</span>  </h3> <pre data-language="go">func (z *Int) Binomial(n, k int64) *Int</pre> <p>Binomial sets z to the binomial coefficient C(n, k) and returns z. </p>
<h3 id="Int.Bit">func (*Int) <span>Bit</span>  </h3> <pre data-language="go">func (x *Int) Bit(i int) uint</pre> <p>Bit returns the value of the i'th bit of x. That is, it returns (x&gt;&gt;i)&amp;1. The bit index i must be &gt;= 0. </p>
<h3 id="Int.BitLen">func (*Int) <span>BitLen</span>  </h3> <pre data-language="go">func (x *Int) BitLen() int</pre> <p>BitLen returns the length of the absolute value of x in bits. The bit length of 0 is 0. </p>
<h3 id="Int.Bits">func (*Int) <span>Bits</span>  </h3> <pre data-language="go">func (x *Int) Bits() []Word</pre> <p>Bits provides raw (unchecked but fast) access to x by returning its absolute value as a little-endian <a href="#Word">Word</a> slice. The result and x share the same underlying array. Bits is intended to support implementation of missing low-level <a href="#Int">Int</a> functionality outside this package; it should be avoided otherwise. </p>
<h3 id="Int.Bytes">func (*Int) <span>Bytes</span>  </h3> <pre data-language="go">func (x *Int) Bytes() []byte</pre> <p>Bytes returns the absolute value of x as a big-endian byte slice. </p>
<p>To use a fixed length slice, or a preallocated one, use <a href="#Int.FillBytes">Int.FillBytes</a>. </p>
<h3 id="Int.Cmp">func (*Int) <span>Cmp</span>  </h3> <pre data-language="go">func (x *Int) Cmp(y *Int) (r int)</pre> <p>Cmp compares x and y and returns: </p>
<pre data-language="go">-1 if x &lt;  y
 0 if x == y
+1 if x &gt;  y
</pre> <h3 id="Int.CmpAbs">func (*Int) <span>CmpAbs</span>  <span title="Added in Go 1.10">1.10</span> </h3> <pre data-language="go">func (x *Int) CmpAbs(y *Int) int</pre> <p>CmpAbs compares the absolute values of x and y and returns: </p>
<pre data-language="go">-1 if |x| &lt;  |y|
 0 if |x| == |y|
+1 if |x| &gt;  |y|
</pre> <h3 id="Int.Div">func (*Int) <span>Div</span>  </h3> <pre data-language="go">func (z *Int) Div(x, y *Int) *Int</pre> <p>Div sets z to the quotient x/y for y != 0 and returns z. If y == 0, a division-by-zero run-time panic occurs. Div implements Euclidean division (unlike Go); see <a href="#Int.DivMod">Int.DivMod</a> for more details. </p>
<h3 id="Int.DivMod">func (*Int) <span>DivMod</span>  </h3> <pre data-language="go">func (z *Int) DivMod(x, y, m *Int) (*Int, *Int)</pre> <p>DivMod sets z to the quotient x div y and m to the modulus x mod y and returns the pair (z, m) for y != 0. If y == 0, a division-by-zero run-time panic occurs. </p>
<p>DivMod implements Euclidean division and modulus (unlike Go): </p>
<pre data-language="go">q = x div y  such that
m = x - y*q  with 0 &lt;= m &lt; |y|
</pre> <p>(See Raymond T. Boute, “The Euclidean definition of the functions div and mod”. ACM Transactions on Programming Languages and Systems (TOPLAS), 14(2):127-144, New York, NY, USA, 4/1992. ACM press.) See <a href="#Int.QuoRem">Int.QuoRem</a> for T-division and modulus (like Go). </p>
<h3 id="Int.Exp">func (*Int) <span>Exp</span>  </h3> <pre data-language="go">func (z *Int) Exp(x, y, m *Int) *Int</pre> <p>Exp sets z = x**y mod |m| (i.e. the sign of m is ignored), and returns z. If m == nil or m == 0, z = x**y unless y &lt;= 0 then z = 1. If m != 0, y &lt; 0, and x and m are not relatively prime, z is unchanged and nil is returned. </p>
<p>Modular exponentiation of inputs of a particular size is not a cryptographically constant-time operation. </p>
<h3 id="Int.FillBytes">func (*Int) <span>FillBytes</span>  <span title="Added in Go 1.15">1.15</span> </h3> <pre data-language="go">func (x *Int) FillBytes(buf []byte) []byte</pre> <p>FillBytes sets buf to the absolute value of x, storing it as a zero-extended big-endian byte slice, and returns buf. </p>
<p>If the absolute value of x doesn't fit in buf, FillBytes will panic. </p>
<h3 id="Int.Float64">func (*Int) <span>Float64</span>  <span title="Added in Go 1.21">1.21</span> </h3> <pre data-language="go">func (x *Int) Float64() (float64, Accuracy)</pre> <p>Float64 returns the float64 value nearest x, and an indication of any rounding that occurred. </p>
<h3 id="Int.Format">func (*Int) <span>Format</span>  </h3> <pre data-language="go">func (x *Int) Format(s fmt.State, ch rune)</pre> <p>Format implements <span>fmt.Formatter</span>. It accepts the formats 'b' (binary), 'o' (octal with 0 prefix), 'O' (octal with 0o prefix), 'd' (decimal), 'x' (lowercase hexadecimal), and 'X' (uppercase hexadecimal). Also supported are the full suite of package fmt's format flags for integral types, including '+' and ' ' for sign control, '#' for leading zero in octal and for hexadecimal, a leading "0x" or "0X" for "%#x" and "%#X" respectively, specification of minimum digits precision, output field width, space or zero padding, and '-' for left or right justification. </p>
<h3 id="Int.GCD">func (*Int) <span>GCD</span>  </h3> <pre data-language="go">func (z *Int) GCD(x, y, a, b *Int) *Int</pre> <p>GCD sets z to the greatest common divisor of a and b and returns z. If x or y are not nil, GCD sets their value such that z = a*x + b*y. </p>
<p>a and b may be positive, zero or negative. (Before Go 1.14 both had to be &gt; 0.) Regardless of the signs of a and b, z is always &gt;= 0. </p>
<p>If a == b == 0, GCD sets z = x = y = 0. </p>
<p>If a == 0 and b != 0, GCD sets z = |b|, x = 0, y = sign(b) * 1. </p>
<p>If a != 0 and b == 0, GCD sets z = |a|, x = sign(a) * 1, y = 0. </p>
<h3 id="Int.GobDecode">func (*Int) <span>GobDecode</span>  </h3> <pre data-language="go">func (z *Int) GobDecode(buf []byte) error</pre> <p>GobDecode implements the <span>encoding/gob.GobDecoder</span> interface. </p>
<h3 id="Int.GobEncode">func (*Int) <span>GobEncode</span>  </h3> <pre data-language="go">func (x *Int) GobEncode() ([]byte, error)</pre> <p>GobEncode implements the <span>encoding/gob.GobEncoder</span> interface. </p>
<h3 id="Int.Int64">func (*Int) <span>Int64</span>  </h3> <pre data-language="go">func (x *Int) Int64() int64</pre> <p>Int64 returns the int64 representation of x. If x cannot be represented in an int64, the result is undefined. </p>
<h3 id="Int.IsInt64">func (*Int) <span>IsInt64</span>  <span title="Added in Go 1.9">1.9</span> </h3> <pre data-language="go">func (x *Int) IsInt64() bool</pre> <p>IsInt64 reports whether x can be represented as an int64. </p>
<h3 id="Int.IsUint64">func (*Int) <span>IsUint64</span>  <span title="Added in Go 1.9">1.9</span> </h3> <pre data-language="go">func (x *Int) IsUint64() bool</pre> <p>IsUint64 reports whether x can be represented as a uint64. </p>
<h3 id="Int.Lsh">func (*Int) <span>Lsh</span>  </h3> <pre data-language="go">func (z *Int) Lsh(x *Int, n uint) *Int</pre> <p>Lsh sets z = x &lt;&lt; n and returns z. </p>
<h3 id="Int.MarshalJSON">func (*Int) <span>MarshalJSON</span>  <span title="Added in Go 1.1">1.1</span> </h3> <pre data-language="go">func (x *Int) MarshalJSON() ([]byte, error)</pre> <p>MarshalJSON implements the <span>encoding/json.Marshaler</span> interface. </p>
<h3 id="Int.MarshalText">func (*Int) <span>MarshalText</span>  <span title="Added in Go 1.3">1.3</span> </h3> <pre data-language="go">func (x *Int) MarshalText() (text []byte, err error)</pre> <p>MarshalText implements the <span>encoding.TextMarshaler</span> interface. </p>
<h3 id="Int.Mod">func (*Int) <span>Mod</span>  </h3> <pre data-language="go">func (z *Int) Mod(x, y *Int) *Int</pre> <p>Mod sets z to the modulus x%y for y != 0 and returns z. If y == 0, a division-by-zero run-time panic occurs. Mod implements Euclidean modulus (unlike Go); see <a href="#Int.DivMod">Int.DivMod</a> for more details. </p>
<h3 id="Int.ModInverse">func (*Int) <span>ModInverse</span>  </h3> <pre data-language="go">func (z *Int) ModInverse(g, n *Int) *Int</pre> <p>ModInverse sets z to the multiplicative inverse of g in the ring ℤ/nℤ and returns z. If g and n are not relatively prime, g has no multiplicative inverse in the ring ℤ/nℤ. In this case, z is unchanged and the return value is nil. If n == 0, a division-by-zero run-time panic occurs. </p>
<h3 id="Int.ModSqrt">func (*Int) <span>ModSqrt</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (z *Int) ModSqrt(x, p *Int) *Int</pre> <p>ModSqrt sets z to a square root of x mod p if such a square root exists, and returns z. The modulus p must be an odd prime. If x is not a square mod p, ModSqrt leaves z unchanged and returns nil. This function panics if p is not an odd integer, its behavior is undefined if p is odd but not prime. </p>
<h3 id="Int.Mul">func (*Int) <span>Mul</span>  </h3> <pre data-language="go">func (z *Int) Mul(x, y *Int) *Int</pre> <p>Mul sets z to the product x*y and returns z. </p>
<h3 id="Int.MulRange">func (*Int) <span>MulRange</span>  </h3> <pre data-language="go">func (z *Int) MulRange(a, b int64) *Int</pre> <p>MulRange sets z to the product of all integers in the range [a, b] inclusively and returns z. If a &gt; b (empty range), the result is 1. </p>
<h3 id="Int.Neg">func (*Int) <span>Neg</span>  </h3> <pre data-language="go">func (z *Int) Neg(x *Int) *Int</pre> <p>Neg sets z to -x and returns z. </p>
<h3 id="Int.Not">func (*Int) <span>Not</span>  </h3> <pre data-language="go">func (z *Int) Not(x *Int) *Int</pre> <p>Not sets z = ^x and returns z. </p>
<h3 id="Int.Or">func (*Int) <span>Or</span>  </h3> <pre data-language="go">func (z *Int) Or(x, y *Int) *Int</pre> <p>Or sets z = x | y and returns z. </p>
<h3 id="Int.ProbablyPrime">func (*Int) <span>ProbablyPrime</span>  </h3> <pre data-language="go">func (x *Int) ProbablyPrime(n int) bool</pre> <p>ProbablyPrime reports whether x is probably prime, applying the Miller-Rabin test with n pseudorandomly chosen bases as well as a Baillie-PSW test. </p>
<p>If x is prime, ProbablyPrime returns true. If x is chosen randomly and not prime, ProbablyPrime probably returns false. The probability of returning true for a randomly chosen non-prime is at most ¼ⁿ. </p>
<p>ProbablyPrime is 100% accurate for inputs less than 2⁶⁴. See Menezes et al., Handbook of Applied Cryptography, 1997, pp. 145-149, and FIPS 186-4 Appendix F for further discussion of the error probabilities. </p>
<p>ProbablyPrime is not suitable for judging primes that an adversary may have crafted to fool the test. </p>
<p>As of Go 1.8, ProbablyPrime(0) is allowed and applies only a Baillie-PSW test. Before Go 1.8, ProbablyPrime applied only the Miller-Rabin tests, and ProbablyPrime(0) panicked. </p>
<h3 id="Int.Quo">func (*Int) <span>Quo</span>  </h3> <pre data-language="go">func (z *Int) Quo(x, y *Int) *Int</pre> <p>Quo sets z to the quotient x/y for y != 0 and returns z. If y == 0, a division-by-zero run-time panic occurs. Quo implements truncated division (like Go); see <a href="#Int.QuoRem">Int.QuoRem</a> for more details. </p>
<h3 id="Int.QuoRem">func (*Int) <span>QuoRem</span>  </h3> <pre data-language="go">func (z *Int) QuoRem(x, y, r *Int) (*Int, *Int)</pre> <p>QuoRem sets z to the quotient x/y and r to the remainder x%y and returns the pair (z, r) for y != 0. If y == 0, a division-by-zero run-time panic occurs. </p>
<p>QuoRem implements T-division and modulus (like Go): </p>
<pre data-language="go">q = x/y      with the result truncated to zero
r = x - y*q
</pre> <p>(See Daan Leijen, “Division and Modulus for Computer Scientists”.) See DivMod for Euclidean division and modulus (unlike Go). </p>
<h3 id="Int.Rand">func (*Int) <span>Rand</span>  </h3> <pre data-language="go">func (z *Int) Rand(rnd *rand.Rand, n *Int) *Int</pre> <p>Rand sets z to a pseudo-random number in [0, n) and returns z. </p>
<p>As this uses the <span>math/rand</span> package, it must not be used for security-sensitive work. Use <span>crypto/rand.Int</span> instead. </p>
<h3 id="Int.Rem">func (*Int) <span>Rem</span>  </h3> <pre data-language="go">func (z *Int) Rem(x, y *Int) *Int</pre> <p>Rem sets z to the remainder x%y for y != 0 and returns z. If y == 0, a division-by-zero run-time panic occurs. Rem implements truncated modulus (like Go); see <a href="#Int.QuoRem">Int.QuoRem</a> for more details. </p>
<h3 id="Int.Rsh">func (*Int) <span>Rsh</span>  </h3> <pre data-language="go">func (z *Int) Rsh(x *Int, n uint) *Int</pre> <p>Rsh sets z = x &gt;&gt; n and returns z. </p>
<h3 id="Int.Scan">func (*Int) <span>Scan</span>  </h3> <pre data-language="go">func (z *Int) Scan(s fmt.ScanState, ch rune) error</pre> <p>Scan is a support routine for <span>fmt.Scanner</span>; it sets z to the value of the scanned number. It accepts the formats 'b' (binary), 'o' (octal), 'd' (decimal), 'x' (lowercase hexadecimal), and 'X' (uppercase hexadecimal). </p>   <h4 id="example_Int_Scan"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">// The Scan function is rarely used directly;
// the fmt package recognizes it as an implementation of fmt.Scanner.
i := new(big.Int)
_, err := fmt.Sscan("18446744073709551617", i)
if err != nil {
    log.Println("error scanning value:", err)
} else {
    fmt.Println(i)
}
</pre> <p>Output:</p> <pre class="output" data-language="go">18446744073709551617
</pre>   <h3 id="Int.Set">func (*Int) <span>Set</span>  </h3> <pre data-language="go">func (z *Int) Set(x *Int) *Int</pre> <p>Set sets z to x and returns z. </p>
<h3 id="Int.SetBit">func (*Int) <span>SetBit</span>  </h3> <pre data-language="go">func (z *Int) SetBit(x *Int, i int, b uint) *Int</pre> <p>SetBit sets z to x, with x's i'th bit set to b (0 or 1). That is, if b is 1 SetBit sets z = x | (1 &lt;&lt; i); if b is 0 SetBit sets z = x &amp;^ (1 &lt;&lt; i). If b is not 0 or 1, SetBit will panic. </p>
<h3 id="Int.SetBits">func (*Int) <span>SetBits</span>  </h3> <pre data-language="go">func (z *Int) SetBits(abs []Word) *Int</pre> <p>SetBits provides raw (unchecked but fast) access to z by setting its value to abs, interpreted as a little-endian <a href="#Word">Word</a> slice, and returning z. The result and abs share the same underlying array. SetBits is intended to support implementation of missing low-level <a href="#Int">Int</a> functionality outside this package; it should be avoided otherwise. </p>
<h3 id="Int.SetBytes">func (*Int) <span>SetBytes</span>  </h3> <pre data-language="go">func (z *Int) SetBytes(buf []byte) *Int</pre> <p>SetBytes interprets buf as the bytes of a big-endian unsigned integer, sets z to that value, and returns z. </p>
<h3 id="Int.SetInt64">func (*Int) <span>SetInt64</span>  </h3> <pre data-language="go">func (z *Int) SetInt64(x int64) *Int</pre> <p>SetInt64 sets z to x and returns z. </p>
<h3 id="Int.SetString">func (*Int) <span>SetString</span>  </h3> <pre data-language="go">func (z *Int) SetString(s string, base int) (*Int, bool)</pre> <p>SetString sets z to the value of s, interpreted in the given base, and returns z and a boolean indicating success. The entire string (not just a prefix) must be valid for success. If SetString fails, the value of z is undefined but the returned value is nil. </p>
<p>The base argument must be 0 or a value between 2 and <a href="#MaxBase">MaxBase</a>. For base 0, the number prefix determines the actual base: A prefix of “0b” or “0B” selects base 2, “0”, “0o” or “0O” selects base 8, and “0x” or “0X” selects base 16. Otherwise, the selected base is 10 and no prefix is accepted. </p>
<p>For bases &lt;= 36, lower and upper case letters are considered the same: The letters 'a' to 'z' and 'A' to 'Z' represent digit values 10 to 35. For bases &gt; 36, the upper case letters 'A' to 'Z' represent the digit values 36 to 61. </p>
<p>For base 0, an underscore character “_” may appear between a base prefix and an adjacent digit, and between successive digits; such underscores do not change the value of the number. Incorrect placement of underscores is reported as an error if there are no other errors. If base != 0, underscores are not recognized and act like any other character that is not a valid digit. </p>   <h4 id="example_Int_SetString"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">i := new(big.Int)
i.SetString("644", 8) // octal
fmt.Println(i)
</pre> <p>Output:</p> <pre class="output" data-language="go">420
</pre>   <h3 id="Int.SetUint64">func (*Int) <span>SetUint64</span>  <span title="Added in Go 1.1">1.1</span> </h3> <pre data-language="go">func (z *Int) SetUint64(x uint64) *Int</pre> <p>SetUint64 sets z to x and returns z. </p>
<h3 id="Int.Sign">func (*Int) <span>Sign</span>  </h3> <pre data-language="go">func (x *Int) Sign() int</pre> <p>Sign returns: </p>
<pre data-language="go">-1 if x &lt;  0
 0 if x == 0
+1 if x &gt;  0
</pre> <h3 id="Int.Sqrt">func (*Int) <span>Sqrt</span>  <span title="Added in Go 1.8">1.8</span> </h3> <pre data-language="go">func (z *Int) Sqrt(x *Int) *Int</pre> <p>Sqrt sets z to ⌊√x⌋, the largest integer such that z² ≤ x, and returns z. It panics if x is negative. </p>
<h3 id="Int.String">func (*Int) <span>String</span>  </h3> <pre data-language="go">func (x *Int) String() string</pre> <p>String returns the decimal representation of x as generated by x.Text(10). </p>
<h3 id="Int.Sub">func (*Int) <span>Sub</span>  </h3> <pre data-language="go">func (z *Int) Sub(x, y *Int) *Int</pre> <p>Sub sets z to the difference x-y and returns z. </p>
<h3 id="Int.Text">func (*Int) <span>Text</span>  <span title="Added in Go 1.6">1.6</span> </h3> <pre data-language="go">func (x *Int) Text(base int) string</pre> <p>Text returns the string representation of x in the given base. Base must be between 2 and 62, inclusive. The result uses the lower-case letters 'a' to 'z' for digit values 10 to 35, and the upper-case letters 'A' to 'Z' for digit values 36 to 61. No prefix (such as "0x") is added to the string. If x is a nil pointer it returns "&lt;nil&gt;". </p>
<h3 id="Int.TrailingZeroBits">func (*Int) <span>TrailingZeroBits</span>  <span title="Added in Go 1.13">1.13</span> </h3> <pre data-language="go">func (x *Int) TrailingZeroBits() uint</pre> <p>TrailingZeroBits returns the number of consecutive least significant zero bits of |x|. </p>
<h3 id="Int.Uint64">func (*Int) <span>Uint64</span>  <span title="Added in Go 1.1">1.1</span> </h3> <pre data-language="go">func (x *Int) Uint64() uint64</pre> <p>Uint64 returns the uint64 representation of x. If x cannot be represented in a uint64, the result is undefined. </p>
<h3 id="Int.UnmarshalJSON">func (*Int) <span>UnmarshalJSON</span>  <span title="Added in Go 1.1">1.1</span> </h3> <pre data-language="go">func (z *Int) UnmarshalJSON(text []byte) error</pre> <p>UnmarshalJSON implements the <span>encoding/json.Unmarshaler</span> interface. </p>
<h3 id="Int.UnmarshalText">func (*Int) <span>UnmarshalText</span>  <span title="Added in Go 1.3">1.3</span> </h3> <pre data-language="go">func (z *Int) UnmarshalText(text []byte) error</pre> <p>UnmarshalText implements the <span>encoding.TextUnmarshaler</span> interface. </p>
<h3 id="Int.Xor">func (*Int) <span>Xor</span>  </h3> <pre data-language="go">func (z *Int) Xor(x, y *Int) *Int</pre> <p>Xor sets z = x ^ y and returns z. </p>
<h2 id="Rat">type <span>Rat</span>  </h2> <p>A Rat represents a quotient a/b of arbitrary precision. The zero value for a Rat represents the value 0. </p>
<p>Operations always take pointer arguments (*Rat) rather than Rat values, and each unique Rat value requires its own unique *Rat pointer. To "copy" a Rat value, an existing (or newly allocated) Rat must be set to a new value using the <a href="#Rat.Set">Rat.Set</a> method; shallow copies of Rats are not supported and may lead to errors. </p>
<pre data-language="go">type Rat struct {
    // contains filtered or unexported fields
}
</pre> <h3 id="NewRat">func <span>NewRat</span>  </h3> <pre data-language="go">func NewRat(a, b int64) *Rat</pre> <p>NewRat creates a new <a href="#Rat">Rat</a> with numerator a and denominator b. </p>
<h3 id="Rat.Abs">func (*Rat) <span>Abs</span>  </h3> <pre data-language="go">func (z *Rat) Abs(x *Rat) *Rat</pre> <p>Abs sets z to |x| (the absolute value of x) and returns z. </p>
<h3 id="Rat.Add">func (*Rat) <span>Add</span>  </h3> <pre data-language="go">func (z *Rat) Add(x, y *Rat) *Rat</pre> <p>Add sets z to the sum x+y and returns z. </p>
<h3 id="Rat.Cmp">func (*Rat) <span>Cmp</span>  </h3> <pre data-language="go">func (x *Rat) Cmp(y *Rat) int</pre> <p>Cmp compares x and y and returns: </p>
<pre data-language="go">-1 if x &lt;  y
 0 if x == y
+1 if x &gt;  y
</pre> <h3 id="Rat.Denom">func (*Rat) <span>Denom</span>  </h3> <pre data-language="go">func (x *Rat) Denom() *Int</pre> <p>Denom returns the denominator of x; it is always &gt; 0. The result is a reference to x's denominator, unless x is an uninitialized (zero value) <a href="#Rat">Rat</a>, in which case the result is a new <a href="#Int">Int</a> of value 1. (To initialize x, any operation that sets x will do, including x.Set(x).) If the result is a reference to x's denominator it may change if a new value is assigned to x, and vice versa. </p>
<h3 id="Rat.Float32">func (*Rat) <span>Float32</span>  <span title="Added in Go 1.4">1.4</span> </h3> <pre data-language="go">func (x *Rat) Float32() (f float32, exact bool)</pre> <p>Float32 returns the nearest float32 value for x and a bool indicating whether f represents x exactly. If the magnitude of x is too large to be represented by a float32, f is an infinity and exact is false. The sign of f always matches the sign of x, even if f == 0. </p>
<h3 id="Rat.Float64">func (*Rat) <span>Float64</span>  <span title="Added in Go 1.1">1.1</span> </h3> <pre data-language="go">func (x *Rat) Float64() (f float64, exact bool)</pre> <p>Float64 returns the nearest float64 value for x and a bool indicating whether f represents x exactly. If the magnitude of x is too large to be represented by a float64, f is an infinity and exact is false. The sign of f always matches the sign of x, even if f == 0. </p>
<h3 id="Rat.FloatPrec">func (*Rat) <span>FloatPrec</span>  <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func (x *Rat) FloatPrec() (n int, exact bool)</pre> <p>FloatPrec returns the number n of non-repeating digits immediately following the decimal point of the decimal representation of x. The boolean result indicates whether a decimal representation of x with that many fractional digits is exact or rounded. </p>
<p>Examples: </p>
<pre data-language="go">x      n    exact    decimal representation n fractional digits
0      0    true     0
1      0    true     1
1/2    1    true     0.5
1/3    0    false    0       (0.333... rounded)
1/4    2    true     0.25
1/6    1    false    0.2     (0.166... rounded)
</pre> <h3 id="Rat.FloatString">func (*Rat) <span>FloatString</span>  </h3> <pre data-language="go">func (x *Rat) FloatString(prec int) string</pre> <p>FloatString returns a string representation of x in decimal form with prec digits of precision after the radix point. The last digit is rounded to nearest, with halves rounded away from zero. </p>
<h3 id="Rat.GobDecode">func (*Rat) <span>GobDecode</span>  </h3> <pre data-language="go">func (z *Rat) GobDecode(buf []byte) error</pre> <p>GobDecode implements the <span>encoding/gob.GobDecoder</span> interface. </p>
<h3 id="Rat.GobEncode">func (*Rat) <span>GobEncode</span>  </h3> <pre data-language="go">func (x *Rat) GobEncode() ([]byte, error)</pre> <p>GobEncode implements the <span>encoding/gob.GobEncoder</span> interface. </p>
<h3 id="Rat.Inv">func (*Rat) <span>Inv</span>  </h3> <pre data-language="go">func (z *Rat) Inv(x *Rat) *Rat</pre> <p>Inv sets z to 1/x and returns z. If x == 0, Inv panics. </p>
<h3 id="Rat.IsInt">func (*Rat) <span>IsInt</span>  </h3> <pre data-language="go">func (x *Rat) IsInt() bool</pre> <p>IsInt reports whether the denominator of x is 1. </p>
<h3 id="Rat.MarshalText">func (*Rat) <span>MarshalText</span>  <span title="Added in Go 1.3">1.3</span> </h3> <pre data-language="go">func (x *Rat) MarshalText() (text []byte, err error)</pre> <p>MarshalText implements the <span>encoding.TextMarshaler</span> interface. </p>
<h3 id="Rat.Mul">func (*Rat) <span>Mul</span>  </h3> <pre data-language="go">func (z *Rat) Mul(x, y *Rat) *Rat</pre> <p>Mul sets z to the product x*y and returns z. </p>
<h3 id="Rat.Neg">func (*Rat) <span>Neg</span>  </h3> <pre data-language="go">func (z *Rat) Neg(x *Rat) *Rat</pre> <p>Neg sets z to -x and returns z. </p>
<h3 id="Rat.Num">func (*Rat) <span>Num</span>  </h3> <pre data-language="go">func (x *Rat) Num() *Int</pre> <p>Num returns the numerator of x; it may be &lt;= 0. The result is a reference to x's numerator; it may change if a new value is assigned to x, and vice versa. The sign of the numerator corresponds to the sign of x. </p>
<h3 id="Rat.Quo">func (*Rat) <span>Quo</span>  </h3> <pre data-language="go">func (z *Rat) Quo(x, y *Rat) *Rat</pre> <p>Quo sets z to the quotient x/y and returns z. If y == 0, Quo panics. </p>
<h3 id="Rat.RatString">func (*Rat) <span>RatString</span>  </h3> <pre data-language="go">func (x *Rat) RatString() string</pre> <p>RatString returns a string representation of x in the form "a/b" if b != 1, and in the form "a" if b == 1. </p>
<h3 id="Rat.Scan">func (*Rat) <span>Scan</span>  </h3> <pre data-language="go">func (z *Rat) Scan(s fmt.ScanState, ch rune) error</pre> <p>Scan is a support routine for fmt.Scanner. It accepts the formats 'e', 'E', 'f', 'F', 'g', 'G', and 'v'. All formats are equivalent. </p>   <h4 id="example_Rat_Scan"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">// The Scan function is rarely used directly;
// the fmt package recognizes it as an implementation of fmt.Scanner.
r := new(big.Rat)
_, err := fmt.Sscan("1.5000", r)
if err != nil {
    log.Println("error scanning value:", err)
} else {
    fmt.Println(r)
}
</pre> <p>Output:</p> <pre class="output" data-language="go">3/2
</pre>   <h3 id="Rat.Set">func (*Rat) <span>Set</span>  </h3> <pre data-language="go">func (z *Rat) Set(x *Rat) *Rat</pre> <p>Set sets z to x (by making a copy of x) and returns z. </p>
<h3 id="Rat.SetFloat64">func (*Rat) <span>SetFloat64</span>  <span title="Added in Go 1.1">1.1</span> </h3> <pre data-language="go">func (z *Rat) SetFloat64(f float64) *Rat</pre> <p>SetFloat64 sets z to exactly f and returns z. If f is not finite, SetFloat returns nil. </p>
<h3 id="Rat.SetFrac">func (*Rat) <span>SetFrac</span>  </h3> <pre data-language="go">func (z *Rat) SetFrac(a, b *Int) *Rat</pre> <p>SetFrac sets z to a/b and returns z. If b == 0, SetFrac panics. </p>
<h3 id="Rat.SetFrac64">func (*Rat) <span>SetFrac64</span>  </h3> <pre data-language="go">func (z *Rat) SetFrac64(a, b int64) *Rat</pre> <p>SetFrac64 sets z to a/b and returns z. If b == 0, SetFrac64 panics. </p>
<h3 id="Rat.SetInt">func (*Rat) <span>SetInt</span>  </h3> <pre data-language="go">func (z *Rat) SetInt(x *Int) *Rat</pre> <p>SetInt sets z to x (by making a copy of x) and returns z. </p>
<h3 id="Rat.SetInt64">func (*Rat) <span>SetInt64</span>  </h3> <pre data-language="go">func (z *Rat) SetInt64(x int64) *Rat</pre> <p>SetInt64 sets z to x and returns z. </p>
<h3 id="Rat.SetString">func (*Rat) <span>SetString</span>  </h3> <pre data-language="go">func (z *Rat) SetString(s string) (*Rat, bool)</pre> <p>SetString sets z to the value of s and returns z and a boolean indicating success. s can be given as a (possibly signed) fraction "a/b", or as a floating-point number optionally followed by an exponent. If a fraction is provided, both the dividend and the divisor may be a decimal integer or independently use a prefix of “0b”, “0” or “0o”, or “0x” (or their upper-case variants) to denote a binary, octal, or hexadecimal integer, respectively. The divisor may not be signed. If a floating-point number is provided, it may be in decimal form or use any of the same prefixes as above but for “0” to denote a non-decimal mantissa. A leading “0” is considered a decimal leading 0; it does not indicate octal representation in this case. An optional base-10 “e” or base-2 “p” (or their upper-case variants) exponent may be provided as well, except for hexadecimal floats which only accept an (optional) “p” exponent (because an “e” or “E” cannot be distinguished from a mantissa digit). If the exponent's absolute value is too large, the operation may fail. The entire string, not just a prefix, must be valid for success. If the operation failed, the value of z is undefined but the returned value is nil. </p>   <h4 id="example_Rat_SetString"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">r := new(big.Rat)
r.SetString("355/113")
fmt.Println(r.FloatString(3))
</pre> <p>Output:</p> <pre class="output" data-language="go">3.142
</pre>   <h3 id="Rat.SetUint64">func (*Rat) <span>SetUint64</span>  <span title="Added in Go 1.13">1.13</span> </h3> <pre data-language="go">func (z *Rat) SetUint64(x uint64) *Rat</pre> <p>SetUint64 sets z to x and returns z. </p>
<h3 id="Rat.Sign">func (*Rat) <span>Sign</span>  </h3> <pre data-language="go">func (x *Rat) Sign() int</pre> <p>Sign returns: </p>
<pre data-language="go">-1 if x &lt;  0
 0 if x == 0
+1 if x &gt;  0
</pre> <h3 id="Rat.String">func (*Rat) <span>String</span>  </h3> <pre data-language="go">func (x *Rat) String() string</pre> <p>String returns a string representation of x in the form "a/b" (even if b == 1). </p>
<h3 id="Rat.Sub">func (*Rat) <span>Sub</span>  </h3> <pre data-language="go">func (z *Rat) Sub(x, y *Rat) *Rat</pre> <p>Sub sets z to the difference x-y and returns z. </p>
<h3 id="Rat.UnmarshalText">func (*Rat) <span>UnmarshalText</span>  <span title="Added in Go 1.3">1.3</span> </h3> <pre data-language="go">func (z *Rat) UnmarshalText(text []byte) error</pre> <p>UnmarshalText implements the <span>encoding.TextUnmarshaler</span> interface. </p>
<h2 id="RoundingMode">type <span>RoundingMode</span>  <span title="Added in Go 1.5">1.5</span> </h2> <p>RoundingMode determines how a <a href="#Float">Float</a> value is rounded to the desired precision. Rounding may change the <a href="#Float">Float</a> value; the rounding error is described by the <a href="#Float">Float</a>'s <a href="#Accuracy">Accuracy</a>. </p>
<pre data-language="go">type RoundingMode byte</pre> <p>These constants define supported rounding modes. </p>
<pre data-language="go">const (
    ToNearestEven RoundingMode = iota // == IEEE 754-2008 roundTiesToEven
    ToNearestAway                     // == IEEE 754-2008 roundTiesToAway
    ToZero                            // == IEEE 754-2008 roundTowardZero
    AwayFromZero                      // no IEEE 754-2008 equivalent
    ToNegativeInf                     // == IEEE 754-2008 roundTowardNegative
    ToPositiveInf                     // == IEEE 754-2008 roundTowardPositive
)</pre>    <h4 id="example_RoundingMode"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">operands := []float64{2.6, 2.5, 2.1, -2.1, -2.5, -2.6}

fmt.Print("   x")
for mode := big.ToNearestEven; mode &lt;= big.ToPositiveInf; mode++ {
    fmt.Printf("  %s", mode)
}
fmt.Println()

for _, f64 := range operands {
    fmt.Printf("%4g", f64)
    for mode := big.ToNearestEven; mode &lt;= big.ToPositiveInf; mode++ {
        // sample operands above require 2 bits to represent mantissa
        // set binary precision to 2 to round them to integer values
        f := new(big.Float).SetPrec(2).SetMode(mode).SetFloat64(f64)
        fmt.Printf("  %*g", len(mode.String()), f)
    }
    fmt.Println()
}

</pre> <p>Output:</p> <pre class="output" data-language="go">   x  ToNearestEven  ToNearestAway  ToZero  AwayFromZero  ToNegativeInf  ToPositiveInf
 2.6              3              3       2             3              2              3
 2.5              2              3       2             3              2              3
 2.1              2              2       2             3              2              3
-2.1             -2             -2      -2            -3             -3             -2
-2.5             -2             -3      -2            -3             -3             -2
-2.6             -3             -3      -2            -3             -3             -2
</pre>   <h3 id="RoundingMode.String">func (RoundingMode) <span>String</span>  <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (i RoundingMode) String() string</pre> <h2 id="Word">type <span>Word</span>  </h2> <p>A Word represents a single digit of a multi-precision unsigned integer. </p>
<pre data-language="go">type Word uint</pre><div class="_attribution">
  <p class="_attribution-p">
    &copy; Google, Inc.<br>Licensed under the Creative Commons Attribution License 3.0.<br>
    <a href="http://golang.org/pkg/math/big/" class="_attribution-link">http://golang.org/pkg/math/big/</a>
  </p>
</div>