-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmpfr.go
More file actions
3482 lines (3188 loc) · 107 KB
/
mpfr.go
File metadata and controls
3482 lines (3188 loc) · 107 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2024 go-mpfr Authors
//
// Use of this source code is governed by a license that can be
// found in the LICENSE file.
// Package mpfr provides Go bindings for the MPFR multiple-precision
// floating-point library.
package mpfr
/*
#cgo LDFLAGS: -lmpfr -lgmp
#include <mpfr.h>
#include <stdlib.h>
// For now, we’ll call MPFR functions directly
// from Go via cgo.
*/
import "C"
import (
"math"
"math/big"
"runtime"
"strings"
"unsafe"
)
// Float represents a multiple-precision floating-point number.
// The zero value for Float is *not* valid until initialized
// (similar to how the GMP code works).
// Float represents a multiple-precision floating-point number.
type Float struct {
mpfr C.mpfr_t // Use C.mpfr_t directly (array of 1 struct)
init bool
RoundingMode Rnd
}
// finalizer is called by the garbage collector when there are no
// more references to this Float. It clears the mpfr_t and releases
// native memory.
func finalizer(f *Float) {
if f.init {
C.mpfr_clear(&f.mpfr[0]) // Pass a pointer to the first element
f.init = false
}
}
// doinit initializes f.mpfr if it isn’t already initialized.
func (f *Float) doinit() {
if f.init {
return
}
f.init = true
// Initialize the mpfr_t struct
C.mpfr_init(&f.mpfr[0])
// set the default rounding mode
f.RoundingMode = RoundToNearest
// Set the finalizer to clean up the memory when the object is garbage-collected
runtime.SetFinalizer(f, finalizer)
}
// Clear deallocates the native mpfr_t. After calling Clear,
// the Float must not be used again.
func (f *Float) Clear() {
if !f.init {
return
}
C.mpfr_clear(&f.mpfr[0]) // Pass a pointer to the first element
f.init = false
}
// Rnd is the type for MPFR rounding modes.
//
// TODO: MPFR has more rounding modes, need to test them.
type Rnd int
const (
RoundToNearest Rnd = Rnd(C.MPFR_RNDN) // Round to nearest, ties to even
RoundToward0 Rnd = Rnd(C.MPFR_RNDZ)
RoundUp Rnd = Rnd(C.MPFR_RNDU)
RoundDown Rnd = Rnd(C.MPFR_RNDD)
RoundAway Rnd = Rnd(C.MPFR_RNDA)
)
// NewFloat allocates and returns a new Float set to 0.0 with MPFR’s default precision.
func NewFloat() *Float {
f := &Float{}
f.doinit()
f.SetFloat64(0.0)
return f
}
func NewFloatWithPrec(prec uint) *Float {
f := &Float{}
f.doinit()
f.SetFloat64(0.0)
f.SetPrec(prec)
return f
}
// GetFloat64 returns the float64 approximation of f, using the specified rounding mode.
func (f *Float) GetFloat64() float64 {
f.doinit()
return float64(C.mpfr_get_d(&f.mpfr[0], C.mpfr_rnd_t(f.RoundingMode)))
}
// SetString parses a string into f.
func (f *Float) SetString(s string, base int) error {
f.doinit()
cstr := C.CString(s)
defer C.free(unsafe.Pointer(cstr))
ret := C.mpfr_set_str(&f.mpfr[0], cstr, C.int(base), C.mpfr_rnd_t(f.RoundingMode))
if ret != 0 {
return ErrInvalidString
}
return nil
}
// String returns f as a base-10 string representation.
func (f *Float) String() string {
f.doinit()
var exp C.mpfr_exp_t
base := 10
cstr := C.mpfr_get_str(nil, &exp, C.int(base), 0, &f.mpfr[0], C.mpfr_rnd_t(f.RoundingMode))
if cstr == nil {
return "<mpfr_get_str_error>"
}
defer C.mpfr_free_str(cstr)
mantissa := C.GoString(cstr)
intExp := int(exp)
if intExp >= 0 {
if intExp > len(mantissa) {
// pad with 0's
mantissa += strings.Repeat("0", intExp-len(mantissa))
return mantissa + ".0"
}
return mantissa[:intExp] + "." + mantissa[intExp:]
}
// pad with 0's
mantissa = strings.Repeat("0", int(-intExp)) + mantissa
return "0." + mantissa
}
// Copy sets f to x, copying the entire mpfr_t.
func (f *Float) Copy(x *Float) *Float {
x.doinit()
f.doinit()
C.mpfr_set(&f.mpfr[0], &x.mpfr[0], C.mpfr_rnd_t(RoundToNearest))
return f
}
// Add performs sequential addition on the receiver `f` and stores the result:
//
// - If called with one argument (`x`), the function computes f + x, where `f` is the current value
// of the receiver. This modifies `f` in place and returns it.
//
// - If called with multiple arguments (`x1, x2, ..., xn`), the function sequentially adds each argument
// to `f`, i.e., f = f + x1 + x2 + ... + xn. This modifies `f` in place and returns it.
//
// Example Usage:
//
// // Add x to f (in-place addition):
// f := NewFloat().SetFloat64(10.0)
// x := NewFloat().SetFloat64(3.0)
// f.Add(x) // f is now 10.0 + 3.0 = 13.0
//
// // Sequentially add multiple values to f:
// f.SetFloat64(5.0)
// x1 := NewFloat().SetFloat64(2.0)
// x2 := NewFloat().SetFloat64(3.0)
// x3 := NewFloat().SetFloat64(4.0)
// f.Add(x1, x2, x3) // f is now 5.0 + 2.0 + 3.0 + 4.0 = 14.0
//
// Notes:
// - At least one argument must be provided; otherwise, the function panics.
// - All arguments must be properly initialized before the call.
// - The computation uses the rounding mode specified by the receiver `f`'s RoundingMode.
//
// Returns:
//
// A pointer to the modified receiver `f`.
func (f *Float) Add(args ...*Float) *Float {
if len(args) == 0 {
// No arguments provided.
panic("Add requires at least 1 argument")
}
f.doinit()
// Sequentially add the arguments.
for _, addend := range args {
addend.doinit()
C.mpfr_add(&f.mpfr[0], &f.mpfr[0], &addend.mpfr[0], C.mpfr_rnd_t(f.RoundingMode))
}
return f
}
// Add sets f to x + y, with the given rounding mode, and returns f.
func Add(x, y *Float, rnd Rnd) *Float {
x.SetRoundMode(rnd)
return x.Add(y)
}
// Sub performs sequential subtraction on the receiver `f` and stores the result:
//
// - If called with one argument (`x`), the function computes f - x, where `f` is the current value
// of the receiver. This modifies `f` in place and returns it.
//
// - If called with multiple arguments (`x1, x2, ..., xn`), the function sequentially subtracts each argument
// from `f`, i.e., f = f - x1 - x2 - ... - xn. This modifies `f` in place and returns it.
//
// Example Usage:
//
// // Subtract x from f (in-place subtraction):
// f := NewFloat().SetFloat64(10.0)
// x := NewFloat().SetFloat64(3.0)
// f.Sub(x) // f is now 10.0 - 3.0 = 7.0
//
// // Sequentially subtract multiple values from f:
// f.SetFloat64(20.0)
// x1 := NewFloat().SetFloat64(5.0)
// x2 := NewFloat().SetFloat64(3.0)
// f.Sub(x1, x2) // f is now 20.0 - 5.0 - 3.0 = 12.0
//
// Notes:
// - At least one argument must be provided; otherwise, the function panics.
// - All arguments must be properly initialized before the call.
// - The computation uses the rounding mode specified by the receiver `f`'s RoundingMode.
//
// Returns:
//
// A pointer to the modified receiver `f`.
func (f *Float) Sub(args ...*Float) *Float {
if len(args) == 0 {
// No arguments provided.
panic("Sub requires at least 1 argument")
}
f.doinit()
// Sequentially subtract the arguments.
for _, subtrahend := range args {
subtrahend.doinit()
C.mpfr_sub(&f.mpfr[0], &f.mpfr[0], &subtrahend.mpfr[0], C.mpfr_rnd_t(f.RoundingMode))
}
return f
}
func Sub(x, y *Float, rnd Rnd) *Float {
x.SetRoundMode(rnd)
return x.Sub(y)
}
// Mul performs sequential multiplication on the receiver `f` and stores the result:
//
// - If called with one argument (`x`), the function computes f * x, where `f` is the current value
// of the receiver. This modifies `f` in place and returns it.
//
// - If called with multiple arguments (`x1, x2, ..., xn`), the function sequentially multiplies `f`
// by each argument in order, i.e., f = f * x1 * x2 * ... * xn. This modifies `f` in place and returns it.
//
// Example Usage:
//
// // Multiply f by x (in-place multiplication):
// f := NewFloat().SetFloat64(2.0)
// x := NewFloat().SetFloat64(3.0)
// f.Mul(x) // f is now 2.0 * 3.0 = 6.0
//
// // Sequentially multiply f by multiple values:
// f.SetFloat64(1.0)
// x1 := NewFloat().SetFloat64(2.0)
// x2 := NewFloat().SetFloat64(3.0)
// x3 := NewFloat().SetFloat64(4.0)
// f.Mul(x1, x2, x3) // f is now 1.0 * 2.0 * 3.0 * 4.0 = 24.0
//
// Notes:
// - At least one argument must be provided; otherwise, the function panics.
// - All arguments must be properly initialized before the call.
// - The computation uses the rounding mode specified by the receiver `f`'s RoundingMode.
//
// Returns:
//
// A pointer to the modified receiver `f`.
func (f *Float) Mul(args ...*Float) *Float {
if len(args) == 0 {
// No arguments provided.
panic("Mul requires at least 1 argument")
}
// Initialize the receiver.
f.doinit()
// Sequentially multiply by the arguments.
for _, multiplier := range args {
multiplier.doinit()
C.mpfr_mul(&f.mpfr[0], &f.mpfr[0], &multiplier.mpfr[0], C.mpfr_rnd_t(f.RoundingMode))
}
return f
}
func Mul(x, y *Float, rnd Rnd) *Float {
x.SetRoundMode(rnd)
return x.Mul(y)
}
// Div performs sequential division on the receiver `f` and stores the result:
//
// - If called with one argument (`x`), the function computes f / x, where `f` is the current value
// of the receiver. This modifies `f` in place and returns it.
//
// - If called with multiple arguments (`x1, x2, ..., xn`), the function sequentially divides `f`
// by each argument in order, i.e., f = f / x1 / x2 / ... / xn. This modifies `f` in place and returns it.
//
// Example Usage:
//
// // Divide f by x (in-place division):
// f := NewFloat().SetFloat64(10.0)
// x := NewFloat().SetFloat64(2.0)
// f.Div(x) // f is now 10.0 / 2.0 = 5.0
//
// // Sequentially divide f by multiple values:
// f.SetFloat64(100.0)
// x1 := NewFloat().SetFloat64(2.0)
// x2 := NewFloat().SetFloat64(5.0)
// f.Div(x1, x2) // f is now 100.0 / 2.0 / 5.0 = 10.0
//
// Notes:
// - At least one argument must be provided; otherwise, the function panics.
// - All arguments must be properly initialized before the call.
// - Division by zero will result in behavior as defined by the MPFR library (e.g., Inf or NaN).
// - The computation uses the rounding mode specified by the receiver `f`'s RoundingMode.
//
// Returns:
//
// A pointer to the modified receiver `f`.
func (f *Float) Div(args ...*Float) *Float {
if len(args) == 0 {
// No arguments provided.
panic("Div requires at least 1 argument")
}
f.doinit()
// Sequentially divide by the arguments.
for _, divisor := range args {
divisor.doinit()
C.mpfr_div(&f.mpfr[0], &f.mpfr[0], &divisor.mpfr[0], C.mpfr_rnd_t(f.RoundingMode))
}
return f
}
func Div(x, y *Float, rnd Rnd) *Float {
x.SetRoundMode(rnd)
return x.Div(y)
}
// Quo sets f to the quotient of x / y with the specified rounding mode and returns f.
// If y == 0, it panics with a division-by-zero error.
func (f *Float) Quo(x, y *Float) *Float {
x.doinit()
y.doinit()
f.doinit()
// Check for division by zero
if C.mpfr_zero_p(&y.mpfr[0]) != 0 { // mpfr_zero_p returns nonzero if y is zero
panic("Quo: division by zero")
}
C.mpfr_div(&f.mpfr[0], &x.mpfr[0], &y.mpfr[0], C.mpfr_rnd_t(f.RoundingMode))
return f
}
// Quo sets f to the quotient of x / y with the specified rounding mode and returns f.
func Quo(x, y *Float, rnd Rnd) *Float {
f := NewFloat()
f.SetRoundMode(rnd)
return f.Quo(x, y)
}
// Pow computes the power function and stores the result in the receiver `f`:
//
// - If called with one argument (`y`), the function computes f^y (where `f` is the current value
// of the receiver) and stores the result in `f`.
//
// - If called with two arguments (`x`, `y`), the function computes x^y and stores the result in the receiver `f`
// using the receiver's RoundingMode.
//
// - If called with three arguments (`x`, `y`, `rnd`), the function computes x^y and stores the result in the receiver `f`
// using the specified rounding mode `rnd`.
//
// Example Usage:
//
// // Compute f^y (receiver raised to the power of y):
// f := NewFloat().SetFloat64(2.0)
// y := NewFloat().SetFloat64(3.0)
// f.Pow(y) // f is now 2.0^3.0 = 8.0
//
// // Compute x^y using the receiver's rounding mode:
// x := NewFloat().SetFloat64(2.0)
// y.SetFloat64(3.0)
// f.Pow(x, y) // f is now 2.0^3.0 = 8.0
//
// Notes:
// - If only one argument is provided, `y` must be initialized before the call.
// - If two or three arguments are provided, both `x` and `y` must be initialized before the call.
// - If `rnd` is not provided, the rounding mode of the receiver `f` is used.
// - The function handles special cases like x = 0 or y = 0 according to the MPFR library rules.
// - Behavior for non-integer exponents and negative bases depends on MPFR.
//
// Returns:
//
// A pointer to the modified receiver `f`.
func (f *Float) Pow(args ...interface{}) *Float {
if len(args) == 1 {
// Compute f^y (receiver raised to the power of y).
y, yOk := args[0].(*Float)
if !yOk {
panic("Pow expects a *Float as the single argument")
}
y.doinit()
f.doinit()
C.mpfr_pow(&f.mpfr[0], &f.mpfr[0], &y.mpfr[0], C.mpfr_rnd_t(f.RoundingMode))
} else if len(args) > 1 {
// Compute x^y using the receiver's rounding mode.
x, xOk := args[0].(*Float)
y, yOk := args[1].(*Float)
if !xOk || !yOk {
panic("Pow expects two *Float arguments (x, y)")
}
x.doinit()
y.doinit()
f.doinit()
C.mpfr_pow(&f.mpfr[0], &x.mpfr[0], &y.mpfr[0], C.mpfr_rnd_t(f.RoundingMode))
} else {
panic("Pow expects at least one argument")
}
return f
}
// Pow sets f to x^y (x raised to the power y), with the given rounding mode, and returns f.
func Pow(x, y *Float, rnd Rnd) *Float {
f := NewFloat()
f.SetRoundMode(rnd)
return f.Pow(x, y)
}
// Exp computes the exponential function e^x and stores the result in the receiver `f`.
//
// - If called with no arguments, the function computes e^f, where `f` is the current value
// of the receiver. This modifies `f` in place and returns it.
//
// - If called with one argument `x`, the function computes e^x and stores the result in the receiver `f`.
// This modifies `f` and returns it.
//
// The result is computed using the rounding mode specified by the receiver `f`'s RoundingMode.
//
// Example Usage:
//
// // Compute the exponential function of a specific value:
// x := NewFloat().SetFloat64(1.0)
// f := NewFloat()
// f.Exp(x) // f is now e^1.0 ≈ 2.718
//
// // Compute the exponential function of the receiver's value in place:
// f.SetFloat64(2.0)
// f.Exp() // f is now e^2.0 ≈ 7.389
//
// Notes:
// - If called with one argument, `x` must be initialized before the call.
// - If called with no arguments, only the receiver `f` must be initialized.
// - The computation uses the `RoundingMode` of the receiver `f`.
// - The exponential function e^x is defined for all real numbers.
//
// Returns:
//
// A pointer to the modified receiver `f`.
func (f *Float) Exp(args ...*Float) *Float {
f.doinit()
if len(args) == 0 {
// Compute e^f in place.
C.mpfr_exp(&f.mpfr[0], &f.mpfr[0], C.mpfr_rnd_t(f.RoundingMode))
} else {
// Compute e^x and store the result in `f`.
x := args[0]
x.doinit()
C.mpfr_exp(&f.mpfr[0], &x.mpfr[0], C.mpfr_rnd_t(f.RoundingMode))
}
return f
}
func Exp(x *Float, rnd Rnd) *Float {
f := NewFloat()
f.SetRoundMode(rnd)
return f.Exp(x)
}
// Log computes the natural logarithm (ln) of a value and stores the result in the receiver `f`.
//
// - If called with no arguments, the function computes ln(f), where `f` is the current value
// of the receiver. This modifies `f` in place and returns it.
//
// - If called with one argument `x`, the function computes ln(x) and stores the result in the receiver `f`.
// This modifies `f` and returns it.
//
// The result is computed using the rounding mode specified by the receiver `f`'s RoundingMode.
//
// Example Usage:
//
// // Compute the natural logarithm of a specific value:
// x := NewFloat().SetFloat64(2.718)
// f := NewFloat()
// f.Log(x) // f is now ln(2.718) ≈ 1.0
//
// // Compute the natural logarithm of the receiver's value in place:
// f.SetFloat64(10.0)
// f.Log() // f is now ln(10.0)
//
// Notes:
// - If called with one argument, `x` must be initialized before the call.
// - If called with no arguments, only the receiver `f` must be initialized.
// - The computation uses the `RoundingMode` of the receiver `f`.
// - For inputs `x <= 0`, the result will be NaN (if x < 0) or -Inf (if x == 0), depending on the MPFR library.
//
// Returns:
//
// A pointer to the modified receiver `f`.
func (f *Float) Log(args ...*Float) *Float {
f.doinit()
if len(args) == 0 {
// Compute ln(f) in place.
C.mpfr_log(&f.mpfr[0], &f.mpfr[0], C.mpfr_rnd_t(f.RoundingMode))
} else {
// Compute ln(x) and store the result in `f`.
x := args[0]
x.doinit()
C.mpfr_log(&f.mpfr[0], &x.mpfr[0], C.mpfr_rnd_t(f.RoundingMode))
}
return f
}
func Log(x *Float, rnd Rnd) *Float {
f := NewFloat()
f.SetRoundMode(rnd)
return f.Log(x)
}
// Cmp compares f and x and returns -1 if f < x, 0 if f == x, +1 if f > x.
func (f *Float) Cmp(x *Float) int {
f.doinit()
x.doinit()
return int(C.mpfr_cmp(&f.mpfr[0], &x.mpfr[0]))
}
// Cmp compares x and y and returns -1 if x < y, 0 if x == y, +1 if x > y.
func Cmp(x, y *Float) int {
x.doinit()
y.doinit()
return int(C.mpfr_cmp(&x.mpfr[0], &y.mpfr[0]))
}
// Abs computes the absolute value of a value, |x|, and stores the result in the receiver `f`.
//
// - If called with no arguments, the function computes |f|, where `f` is the current value
// of the receiver. This modifies `f` in place and returns it.
//
// - If called with one argument `x`, the function computes |x| and stores the result in the receiver `f`.
// This modifies `f` and returns it.
//
// The result is computed using the rounding mode specified by the receiver `f`'s RoundingMode.
//
// Example Usage:
//
// // Compute the absolute value of a specific value:
// x := NewFloat().SetFloat64(-3.5)
// f := NewFloat()
// f.Abs(x) // f is now 3.5
//
// // Compute the absolute value of the receiver's value in place:
// f.SetFloat64(-2.5)
// f.Abs() // f is now 2.5
//
// Notes:
// - If called with one argument, `x` must be initialized before the call.
// - If called with no arguments, only the receiver `f` must be initialized.
// - The computation uses the `RoundingMode` of the receiver `f`.
// - The absolute value function |x| is defined for all real numbers.
//
// Returns:
//
// A pointer to the modified receiver `f`.
func (f *Float) Abs(args ...*Float) *Float {
f.doinit()
if len(args) == 0 {
// Compute |f| in place.
C.mpfr_abs(&f.mpfr[0], &f.mpfr[0], C.mpfr_rnd_t(f.RoundingMode))
} else {
// Compute |x| and store the result in `f`.
x := args[0]
x.doinit()
C.mpfr_abs(&f.mpfr[0], &x.mpfr[0], C.mpfr_rnd_t(f.RoundingMode))
}
return f
}
func Abs(x *Float, rnd Rnd) *Float {
f := NewFloat()
f.SetRoundMode(rnd)
return f.Abs(x)
}
// Acos computes the arccosine of a value, arccos(x), and stores the result in the receiver `f`.
//
// - If called with no arguments, the function computes arccos(f), where `f` is the current value
// of the receiver. This modifies `f` in place and returns it.
//
// - If called with one argument `x`, the function computes arccos(x) and stores the result in the receiver `f`.
// This modifies `f` and returns it.
//
// The result is computed using the rounding mode specified by the receiver `f`'s RoundingMode.
//
// Example Usage:
//
// // Compute the arccosine of a specific value:
// x := NewFloat().SetFloat64(0.5)
// f := NewFloat()
// f.Acos(x) // f is now arccos(0.5)
//
// // Compute the arccosine of the receiver's value in place:
// f.SetFloat64(0.3)
// f.Acos() // f is now arccos(0.3)
//
// Notes:
// - If called with one argument, `x` must be initialized before the call.
// - If called with no arguments, only the receiver `f` must be initialized.
// - The computation uses the `RoundingMode` of the receiver `f`.
// - The arccosine function is defined only for values in the range [-1, 1]; behavior outside this range
// depends on the MPFR library.
//
// Returns:
//
// A pointer to the modified receiver `f`.
func (f *Float) Acos(args ...*Float) *Float {
f.doinit()
if len(args) == 0 {
// Compute arccos(f) in place.
C.mpfr_acos(&f.mpfr[0], &f.mpfr[0], C.mpfr_rnd_t(f.RoundingMode))
} else {
// Compute arccos(x) and store the result in `f`.
x := args[0]
x.doinit()
C.mpfr_acos(&f.mpfr[0], &x.mpfr[0], C.mpfr_rnd_t(f.RoundingMode))
}
return f
}
// Acos sets f = arccos(x) with rounding mode rnd.
func Acos(x *Float, rnd Rnd) *Float {
f := NewFloat()
f.SetRoundMode(rnd)
return f.Acos(x)
}
// Acosh computes the inverse hyperbolic cosine of a value, arcosh(x), and stores the result in the receiver `f`.
//
// - If called with no arguments, the function computes arcosh(f), where `f` is the current value
// of the receiver. This modifies `f` in place and returns it.
//
// - If called with one argument `x`, the function computes arcosh(x) and stores the result in the receiver `f`.
// This modifies `f` and returns it.
//
// The result is computed using the rounding mode specified by the receiver `f`'s RoundingMode.
//
// Example Usage:
//
// // Compute the inverse hyperbolic cosine of a specific value:
// x := NewFloat().SetFloat64(1.5)
// f := NewFloat()
// f.Acosh(x) // f is now arcosh(1.5)
//
// // Compute the inverse hyperbolic cosine of the receiver's value in place:
// f.SetFloat64(2.0)
// f.Acosh() // f is now arcosh(2.0)
//
// Notes:
// - If called with one argument, `x` must be initialized before the call.
// - If called with no arguments, only the receiver `f` must be initialized.
// - The computation uses the `RoundingMode` of the receiver `f`.
// - The inverse hyperbolic cosine function, arcosh(x), is defined only for x >= 1; behavior
// for x < 1 depends on the MPFR library.
//
// Returns:
//
// A pointer to the modified receiver `f`.
func (f *Float) Acosh(args ...*Float) *Float {
f.doinit()
if len(args) == 0 {
// Compute arcosh(f) in place.
C.mpfr_acosh(&f.mpfr[0], &f.mpfr[0], C.mpfr_rnd_t(f.RoundingMode))
} else {
// Compute arcosh(x) and store the result in `f`.
x := args[0]
x.doinit()
C.mpfr_acosh(&f.mpfr[0], &x.mpfr[0], C.mpfr_rnd_t(f.RoundingMode))
}
return f
}
// Acosh sets f = arcosh(x) with rounding mode rnd.
func Acosh(x *Float, rnd Rnd) *Float {
f := NewFloat()
f.SetRoundMode(rnd)
return f.Acosh(x)
}
// Agm sets f = AGM(x, y) (arithmetic-geometric mean), using rnd.
func (f *Float) Agm(x, y *Float) *Float {
x.doinit()
y.doinit()
f.doinit()
C.mpfr_agm(&f.mpfr[0], &x.mpfr[0], &y.mpfr[0], C.mpfr_rnd_t(f.RoundingMode))
return f
}
// Agm returns AGM(x, y) (arithmetic-geometric mean), using rnd.
func Agm(x, y *Float, rnd Rnd) *Float {
f := NewFloat()
f.SetRoundMode(rnd)
return f.Agm(x, y)
}
// Asin computes the arcsine of a value, arcsin(x), and stores the result in the receiver `f`.
//
// - If called with no arguments, the function computes arcsin(f), where `f` is the current value
// of the receiver. This modifies `f` in place and returns it.
//
// - If called with one argument `x`, the function computes arcsin(x) and stores the result in the receiver `f`.
// This modifies `f` and returns it.
//
// The result is computed using the rounding mode specified by the receiver `f`'s RoundingMode.
//
// Example Usage:
//
// // Compute the arcsine of a specific value:
// x := NewFloat().SetFloat64(0.5)
// f := NewFloat()
// f.Asin(x) // f is now arcsin(0.5)
//
// // Compute the arcsine of the receiver's value in place:
// f.SetFloat64(0.3)
// f.Asin() // f is now arcsin(0.3)
//
// Notes:
// - If called with one argument, `x` must be initialized before the call.
// - If called with no arguments, only the receiver `f` must be initialized.
// - The computation uses the `RoundingMode` of the receiver `f`.
// - The arcsine function is defined only for values in the range [-1, 1]; behavior outside this range
// depends on the MPFR library.
//
// Returns:
//
// A pointer to the modified receiver `f`.
func (f *Float) Asin(args ...*Float) *Float {
f.doinit()
if len(args) == 0 {
// Compute arcsin(f) in place.
C.mpfr_asin(&f.mpfr[0], &f.mpfr[0], C.mpfr_rnd_t(f.RoundingMode))
} else {
// Compute arcsin(x) and store the result in `f`.
x := args[0]
x.doinit()
C.mpfr_asin(&f.mpfr[0], &x.mpfr[0], C.mpfr_rnd_t(f.RoundingMode))
}
return f
}
// Asin returns arcsin(x), using rnd.
func Asin(x *Float, rnd Rnd) *Float {
f := NewFloat()
f.SetRoundMode(rnd)
return f.Asin(x)
}
// Asinh computes the inverse hyperbolic sine of a value, arsinh(x), and stores the result in the receiver `f`.
//
// - If called with no arguments, the function computes arsinh(f), where `f` is the current value
// of the receiver. This modifies `f` in place and returns it.
//
// - If called with one argument `x`, the function computes arsinh(x) and stores the result in the receiver `f`.
// This modifies `f` and returns it.
//
// The result is computed using the rounding mode specified by the receiver `f`'s RoundingMode.
//
// Example Usage:
//
// // Compute the inverse hyperbolic sine of a specific value:
// x := NewFloat().SetFloat64(1.0)
// f := NewFloat()
// f.Asinh(x) // f is now arsinh(1.0)
//
// // Compute the inverse hyperbolic sine of the receiver's value in place:
// f.SetFloat64(0.5)
// f.Asinh() // f is now arsinh(0.5)
//
// Notes:
// - If called with one argument, `x` must be initialized before the call.
// - If called with no arguments, only the receiver `f` must be initialized.
// - The computation uses the `RoundingMode` of the receiver `f`.
// - The inverse hyperbolic sine function arsinh(x) is well-defined for all real numbers.
//
// Returns:
//
// A pointer to the modified receiver `f`.
func (f *Float) Asinh(args ...*Float) *Float {
f.doinit()
if len(args) == 0 {
// Compute arsinh(f) in place.
C.mpfr_asinh(&f.mpfr[0], &f.mpfr[0], C.mpfr_rnd_t(f.RoundingMode))
} else {
// Compute arsinh(x) and store the result in `f`.
x := args[0]
x.doinit()
C.mpfr_asinh(&f.mpfr[0], &x.mpfr[0], C.mpfr_rnd_t(f.RoundingMode))
}
return f
}
// Asinh returns arsinh(x), using rnd.
func Asinh(x *Float, rnd Rnd) *Float {
f := NewFloat()
f.SetRoundMode(rnd)
return f.Asinh(x)
}
// Atan computes the arctangent of a value, arctan(x), and stores the result in the receiver `f`.
//
// - If called with no arguments, the function computes arctan(f), where `f` is the current value
// of the receiver. This modifies `f` in place and returns it.
//
// - If called with one argument `x`, the function computes arctan(x) and stores the result in the receiver `f`.
// This modifies `f` and returns it.
//
// The result is computed using the rounding mode specified by the receiver `f`'s RoundingMode.
//
// Example Usage:
//
// // Compute the arctangent of a specific value:
// x := NewFloat().SetFloat64(1.0)
// f := NewFloat()
// f.Atan(x) // f is now arctan(1.0) = π/4
//
// // Compute the arctangent of the receiver's value in place:
// f.SetFloat64(0.5)
// f.Atan() // f is now arctan(0.5)
//
// Notes:
// - If called with one argument, `x` must be initialized before the call.
// - If called with no arguments, only the receiver `f` must be initialized.
// - The computation uses the `RoundingMode` of the receiver `f`.
// - The arctangent function is well-defined for all real numbers.
//
// Returns:
//
// A pointer to the modified receiver `f`.
func (f *Float) Atan(args ...*Float) *Float {
f.doinit()
if len(args) == 0 {
// Compute arctan(f) in place.
C.mpfr_atan(&f.mpfr[0], &f.mpfr[0], C.mpfr_rnd_t(f.RoundingMode))
} else {
// Compute arctan(x) and store the result in `f`.
x := args[0]
x.doinit()
C.mpfr_atan(&f.mpfr[0], &x.mpfr[0], C.mpfr_rnd_t(f.RoundingMode))
}
return f
}
// Atan returns arctan(x), using rnd.
func Atan(x *Float, rnd Rnd) *Float {
f := NewFloat()
f.SetRoundMode(rnd)
return f.Atan(x)
}
// Atan2 sets f = arctan2(y, x) = angle whose tangent is y/x, using rnd.
func (f *Float) Atan2(y, x *Float) *Float {
y.doinit()
x.doinit()
f.doinit()
C.mpfr_atan2(&f.mpfr[0], &y.mpfr[0], &x.mpfr[0], C.mpfr_rnd_t(f.RoundingMode))
return f
}
// Atan2 returns arctan2(y, x) = angle whose tangent is y/x, using rnd.
func Atan2(y, x *Float, rnd Rnd) *Float {
f := NewFloat()
f.SetRoundMode(rnd)
return f.Atan2(y, x)
}
// Atanh computes the inverse hyperbolic tangent of a value, artanh(x), and stores the result in the receiver `f`.
//
// - If called with no arguments, the function computes artanh(f), where `f` is the current value
// of the receiver. This modifies `f` in place and returns it.
//
// - If called with one argument `x`, the function computes artanh(x) and stores the result in the receiver `f`.
// This modifies `f` and returns it.
//
// The result is computed using the rounding mode specified by the receiver `f`'s RoundingMode.
//
// Example Usage:
//
// // Compute the inverse hyperbolic tangent of a specific value:
// x := NewFloat().SetFloat64(0.5)
// f := NewFloat()
// f.Atanh(x) // f is now artanh(0.5)
//
// // Compute the inverse hyperbolic tangent of the receiver's value in place:
// f.SetFloat64(0.3)
// f.Atanh() // f is now artanh(0.3)
//
// Notes:
// - If called with one argument, `x` must be initialized before the call.
// - If called with no arguments, only the receiver `f` must be initialized.
// - The computation uses the `RoundingMode` of the receiver `f`.
// - The function is undefined for |x| ≥ 1 (i.e., x <= -1 or x >= 1); behavior in such cases depends on the MPFR library.
//
// Returns:
//
// A pointer to the modified receiver `f`.
func (f *Float) Atanh(args ...*Float) *Float {
f.doinit()
if len(args) == 0 {
// Compute artanh(f) in place.
C.mpfr_atanh(&f.mpfr[0], &f.mpfr[0], C.mpfr_rnd_t(f.RoundingMode))
} else {
// Compute artanh(x) and store the result in `f`.
x := args[0]
x.doinit()
C.mpfr_atanh(&f.mpfr[0], &x.mpfr[0], C.mpfr_rnd_t(f.RoundingMode))
}
return f
}
// Atanh returns artanh(x), using rnd.
func Atanh(x *Float, rnd Rnd) *Float {
f := NewFloat()
f.SetRoundMode(rnd)
return f.Atanh(x)
}
// Cbrt computes the cube root of a Float.
//
// If no arguments are passed, it computes the cube root of the receiver `f`
// in place, modifying `f` and returning it.
//
// If a single argument `x` is passed, it computes the cube root of `x` and
// stores the result in the receiver `f`, modifying `f` and returning it.
//
// Example Usage:
//
// f := NewFloat().SetFloat64(8.0)
// f.Cbrt() // f is now the cube root of 8 (2.0)
//
// x := NewFloat().SetFloat64(27.0)
// result := NewFloat().Cbrt(x) // result is now the cube root of 27 (3.0)
//
// Returns:
//
// A pointer to the modified receiver `f`.
func (f *Float) Cbrt(args ...*Float) *Float {
f.doinit()
if len(args) == 0 {