2 * Copyright (C) 1987-2008 Sun Microsystems, Inc. All Rights Reserved.
3 * Copyright (C) 2008-2011 Robert Ancell
5 * This program is free software: you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation, either version 2 of the License, or (at your option) any later
8 * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
18 #include "mp-private.h"
20 static MPNumber eulers_number
;
21 static gboolean have_eulers_number
= FALSE
;
23 // FIXME: Re-add overflow and underflow detection
25 char *mp_error
= NULL
;
27 /* THIS ROUTINE IS CALLED WHEN AN ERROR CONDITION IS ENCOUNTERED, AND
28 * AFTER A MESSAGE HAS BEEN WRITTEN TO STDERR.
31 mperr(const char *format
, ...)
36 va_start(args
, format
);
37 vsnprintf(text
, 1024, format
, args
);
42 mp_error
= strdup(text
);
61 /* ROUTINE CALLED BY MP_DIVIDE AND MP_SQRT TO ENSURE THAT
62 * RESULTS ARE REPRESENTED EXACTLY IN T-2 DIGITS IF THEY
63 * CAN BE. X IS AN MP NUMBER, I AND J ARE INTEGERS.
66 mp_ext(int i
, int j
, MPNumber
*x
)
70 if (mp_is_zero(x
) || MP_T
<= 2 || i
== 0)
73 /* COMPUTE MAXIMUM POSSIBLE ERROR IN THE LAST PLACE */
75 s
= MP_BASE
* x
->fraction
[MP_T
- 2] + x
->fraction
[MP_T
- 1];
77 /* SET LAST TWO DIGITS TO ZERO */
79 x
->fraction
[MP_T
- 2] = 0;
80 x
->fraction
[MP_T
- 1] = 0;
84 if (s
+ q
< MP_BASE
* MP_BASE
)
88 x
->fraction
[MP_T
- 2] = MP_BASE
- 1;
89 x
->fraction
[MP_T
- 1] = MP_BASE
;
91 /* NORMALIZE X (LAST DIGIT B IS OK IN MP_MULTIPLY_INTEGER) */
92 mp_multiply_integer(x
, 1, x
);
97 mp_get_eulers(MPNumber
*z
)
99 if (!have_eulers_number
) {
101 mp_set_from_integer(1, &t
);
102 mp_epowy(&t
, &eulers_number
);
103 have_eulers_number
= TRUE
;
105 mp_set_from_mp(&eulers_number
, z
);
110 mp_get_i(MPNumber
*z
)
112 mp_set_from_integer(0, z
);
115 z
->im_fraction
[0] = 1;
120 mp_abs(const MPNumber
*x
, MPNumber
*z
)
122 if (mp_is_complex(x
)){
123 MPNumber x_real
, x_im
;
125 mp_real_component(x
, &x_real
);
126 mp_imaginary_component(x
, &x_im
);
128 mp_multiply(&x_real
, &x_real
, &x_real
);
129 mp_multiply(&x_im
, &x_im
, &x_im
);
130 mp_add(&x_real
, &x_im
, z
);
134 mp_set_from_mp(x
, z
);
142 mp_arg(const MPNumber
*x
, MPAngleUnit unit
, MPNumber
*z
)
144 MPNumber x_real
, x_im
, pi
;
147 /* Translators: Error display when attempting to take argument of zero */
148 mperr(_("Argument not defined for zero"));
149 mp_set_from_integer(0, z
);
153 mp_real_component(x
, &x_real
);
154 mp_imaginary_component(x
, &x_im
);
157 if (mp_is_zero(&x_im
)) {
158 if (mp_is_negative(&x_real
))
159 convert_from_radians(&pi
, MP_RADIANS
, z
);
161 mp_set_from_integer(0, z
);
163 else if (mp_is_zero(&x_real
)) {
164 mp_set_from_mp(&pi
, z
);
165 if (mp_is_negative(&x_im
))
166 mp_divide_integer(z
, -2, z
);
168 mp_divide_integer(z
, 2, z
);
170 else if (mp_is_negative(&x_real
)) {
171 mp_divide(&x_im
, &x_real
, z
);
172 mp_atan(z
, MP_RADIANS
, z
);
173 if (mp_is_negative(&x_im
))
174 mp_subtract(z
, &pi
, z
);
179 mp_divide(&x_im
, &x_real
, z
);
180 mp_atan(z
, MP_RADIANS
, z
);
183 convert_from_radians(z
, unit
, z
);
188 mp_conjugate(const MPNumber
*x
, MPNumber
*z
)
190 mp_set_from_mp(x
, z
);
191 z
->im_sign
= -z
->im_sign
;
196 mp_real_component(const MPNumber
*x
, MPNumber
*z
)
198 mp_set_from_mp(x
, z
);
200 /* Clear imaginary component */
203 memset(z
->im_fraction
, 0, sizeof(int) * MP_SIZE
);
208 mp_imaginary_component(const MPNumber
*x
, MPNumber
*z
)
210 /* Copy imaginary component to real component */
211 z
->sign
= x
->im_sign
;
212 z
->exponent
= x
->im_exponent
;
213 memcpy(z
->fraction
, x
->im_fraction
, sizeof(int) * MP_SIZE
);
215 /* Clear (old) imaginary component */
218 memset(z
->im_fraction
, 0, sizeof(int) * MP_SIZE
);
223 mp_add_real(const MPNumber
*x
, int y_sign
, const MPNumber
*y
, MPNumber
*z
)
227 bool x_largest
= false;
228 const int *big_fraction
, *small_fraction
;
229 MPNumber x_copy
, y_copy
;
233 mp_set_from_mp(y
, z
);
238 else if (mp_is_zero(y
)) {
239 mp_set_from_mp(x
, z
);
243 sign_prod
= y_sign
* x
->sign
;
244 exp_diff
= x
->exponent
- y
->exponent
;
248 } else if (exp_diff
> 0) {
251 /* EXPONENTS EQUAL SO COMPARE SIGNS, THEN FRACTIONS IF NEC. */
253 /* Signs are not equal. find out which mantissa is larger. */
255 for (j
= 0; j
< MP_T
; j
++) {
256 int i
= x
->fraction
[j
] - y
->fraction
[j
];
266 /* Both mantissas equal, so result is zero. */
268 mp_set_from_integer(0, z
);
274 mp_set_from_mp(x
, &x_copy
);
275 mp_set_from_mp(y
, &y_copy
);
276 mp_set_from_integer(0, z
);
279 z
->sign
= x_copy
.sign
;
280 z
->exponent
= x_copy
.exponent
;
281 big_fraction
= x_copy
.fraction
;
282 small_fraction
= y_copy
.fraction
;
285 z
->exponent
= y_copy
.exponent
;
286 big_fraction
= y_copy
.fraction
;
287 small_fraction
= x_copy
.fraction
;
290 /* CLEAR GUARD DIGITS TO RIGHT OF X DIGITS */
291 for(i
= 3; i
>= med
; i
--)
292 z
->fraction
[MP_T
+ i
] = 0;
294 if (sign_prod
>= 0) {
295 /* This is probably insufficient overflow detection, but it makes us
296 * not crash at least.
298 if (MP_T
+ 3 < med
) {
299 mperr(_("Overflow: the result couldn't be calculated"));
300 mp_set_from_integer(0, z
);
304 /* HERE DO ADDITION, EXPONENT(Y) >= EXPONENT(X) */
305 for (i
= MP_T
+ 3; i
>= MP_T
; i
--)
306 z
->fraction
[i
] = small_fraction
[i
- med
];
309 for (; i
>= med
; i
--) {
310 c
= big_fraction
[i
] + small_fraction
[i
- med
] + c
;
313 /* NO CARRY GENERATED HERE */
317 /* CARRY GENERATED HERE */
318 z
->fraction
[i
] = c
- MP_BASE
;
325 c
= big_fraction
[i
] + c
;
330 /* NO CARRY POSSIBLE HERE */
332 z
->fraction
[i
] = big_fraction
[i
];
342 /* MUST SHIFT RIGHT HERE AS CARRY OFF END */
344 for (i
= MP_T
+ 3; i
> 0; i
--)
345 z
->fraction
[i
] = z
->fraction
[i
- 1];
352 for (i
= MP_T
+ med
- 1; i
>= MP_T
; i
--) {
353 /* HERE DO SUBTRACTION, ABS(Y) > ABS(X) */
354 z
->fraction
[i
] = c
- small_fraction
[i
- med
];
357 /* BORROW GENERATED HERE */
358 if (z
->fraction
[i
] < 0) {
360 z
->fraction
[i
] += MP_BASE
;
364 for(; i
>= med
; i
--) {
365 c
= big_fraction
[i
] + c
- small_fraction
[i
- med
];
367 /* NO BORROW GENERATED HERE */
371 /* BORROW GENERATED HERE */
372 z
->fraction
[i
] = c
+ MP_BASE
;
377 for (; i
>= 0; i
--) {
378 c
= big_fraction
[i
] + c
;
384 /* NO CARRY POSSIBLE HERE */
386 z
->fraction
[i
] = big_fraction
[i
];
391 z
->fraction
[i
] = c
+ MP_BASE
;
401 mp_add_with_sign(const MPNumber
*x
, int y_sign
, const MPNumber
*y
, MPNumber
*z
)
403 if (mp_is_complex(x
) || mp_is_complex(y
)) {
404 MPNumber real_x
, real_y
, im_x
, im_y
, real_z
, im_z
;
406 mp_real_component(x
, &real_x
);
407 mp_imaginary_component(x
, &im_x
);
408 mp_real_component(y
, &real_y
);
409 mp_imaginary_component(y
, &im_y
);
411 mp_add_real(&real_x
, y_sign
* y
->sign
, &real_y
, &real_z
);
412 mp_add_real(&im_x
, y_sign
* y
->im_sign
, &im_y
, &im_z
);
414 mp_set_from_complex(&real_z
, &im_z
, z
);
417 mp_add_real(x
, y_sign
* y
->sign
, y
, z
);
422 mp_add(const MPNumber
*x
, const MPNumber
*y
, MPNumber
*z
)
424 mp_add_with_sign(x
, 1, y
, z
);
429 mp_add_integer(const MPNumber
*x
, int64_t y
, MPNumber
*z
)
432 mp_set_from_integer(y
, &t
);
438 mp_add_fraction(const MPNumber
*x
, int64_t i
, int64_t j
, MPNumber
*y
)
441 mp_set_from_fraction(i
, j
, &t
);
447 mp_subtract(const MPNumber
*x
, const MPNumber
*y
, MPNumber
*z
)
449 mp_add_with_sign(x
, -1, y
, z
);
454 mp_sgn(const MPNumber
*x
, MPNumber
*z
)
457 mp_set_from_integer(0, z
);
458 else if (mp_is_negative(x
))
459 mp_set_from_integer(-1, z
);
461 mp_set_from_integer(1, z
);
465 mp_integer_component(const MPNumber
*x
, MPNumber
*z
)
470 mp_set_from_mp(x
, z
);
471 for (i
= z
->exponent
; i
< MP_SIZE
; i
++)
475 memset(z
->im_fraction
, 0, sizeof(int) * MP_SIZE
);
479 mp_fractional_component(const MPNumber
*x
, MPNumber
*z
)
483 /* Fractional component of zero is 0 */
485 mp_set_from_integer(0, z
);
490 if (x
->exponent
<= 0) {
491 mp_set_from_mp(x
, z
);
495 /* Shift fractional component */
497 for (i
= shift
; i
< MP_SIZE
&& x
->fraction
[i
] == 0; i
++)
500 z
->exponent
= x
->exponent
- shift
;
501 for (i
= 0; i
< MP_SIZE
; i
++) {
502 if (i
+ shift
>= MP_SIZE
)
505 z
->fraction
[i
] = x
->fraction
[i
+ shift
];
507 if (z
->fraction
[0] == 0)
512 memset(z
->im_fraction
, 0, sizeof(int) * MP_SIZE
);
517 mp_fractional_part(const MPNumber
*x
, MPNumber
*z
)
521 mp_subtract(x
, &f
, z
);
526 mp_floor(const MPNumber
*x
, MPNumber
*z
)
529 bool have_fraction
= false, is_negative
;
531 /* Integer component of zero = 0 */
533 mp_set_from_mp(x
, z
);
537 /* If all fractional then no integer component */
538 if (x
->exponent
<= 0) {
539 mp_set_from_integer(0, z
);
543 is_negative
= mp_is_negative(x
);
546 mp_set_from_mp(x
, z
);
547 for (i
= z
->exponent
; i
< MP_SIZE
; i
++) {
549 have_fraction
= true;
554 memset(z
->im_fraction
, 0, sizeof(int) * MP_SIZE
);
556 if (have_fraction
&& is_negative
)
557 mp_add_integer(z
, -1, z
);
562 mp_ceiling(const MPNumber
*x
, MPNumber
*z
)
567 mp_fractional_component(x
, &f
);
570 mp_add_integer(z
, 1, z
);
575 mp_round(const MPNumber
*x
, MPNumber
*z
)
580 do_floor
= !mp_is_negative(x
);
582 mp_fractional_component(x
, &f
);
583 mp_multiply_integer(&f
, 2, &f
);
585 mp_set_from_integer(1, &one
);
586 if (mp_is_greater_equal(&f
, &one
))
587 do_floor
= !do_floor
;
596 mp_compare_mp_to_mp(const MPNumber
*x
, const MPNumber
*y
)
600 if (x
->sign
!= y
->sign
) {
601 if (x
->sign
> y
->sign
)
611 /* See if numbers are of different magnitude */
612 if (x
->exponent
!= y
->exponent
) {
613 if (x
->exponent
> y
->exponent
)
619 /* Compare fractions */
620 for (i
= 0; i
< MP_SIZE
; i
++) {
621 if (x
->fraction
[i
] == y
->fraction
[i
])
624 if (x
->fraction
[i
] > y
->fraction
[i
])
636 mp_divide(const MPNumber
*x
, const MPNumber
*y
, MPNumber
*z
)
643 /* Translators: Error displayed attempted to divide by zero */
644 mperr(_("Division by zero is undefined"));
645 mp_set_from_integer(0, z
);
651 mp_set_from_integer(0, z
);
656 /* FIXME: Set exponent to zero to avoid overflow in mp_multiply??? */
657 mp_reciprocal(y
, &t
);
661 mp_multiply(x
, &t
, z
);
662 mp_ext(i
, z
->fraction
[0], z
);
668 mp_divide_integer_real(const MPNumber
*x
, int64_t y
, MPNumber
*z
)
670 int c
, i
, k
, b2
, c2
, j1
, j2
;
675 /* Translators: Error displayed attempted to divide by zero */
676 mperr(_("Division by zero is undefined"));
677 mp_set_from_integer(0, z
);
683 mp_set_from_integer(0, z
);
687 /* Division by -1 or 1 just changes sign */
688 if (y
== 1 || y
== -1) {
690 mp_invert_sign(x
, z
);
692 mp_set_from_mp(x
, z
);
696 /* Copy x as z may also refer to x */
697 mp_set_from_mp(x
, &x_copy
);
698 mp_set_from_integer(0, z
);
702 z
->sign
= -x_copy
.sign
;
705 z
->sign
= x_copy
.sign
;
706 z
->exponent
= x_copy
.exponent
;
711 /* IF y*B NOT REPRESENTABLE AS AN INTEGER HAVE TO SIMULATE
712 * LONG DIVISION. ASSUME AT LEAST 16-BIT WORD.
716 b2
= max(MP_BASE
<< 3, 32767 / MP_BASE
);
720 /* LOOK FOR FIRST NONZERO DIGIT IN QUOTIENT */
724 c
+= x_copy
.fraction
[i
];
731 /* ADJUST EXPONENT AND GET T+4 DIGITS IN QUOTIENT */
732 z
->exponent
+= 1 - i
;
734 c
= MP_BASE
* (c
- y
* r1
);
738 for (k
= 1; k
< kh
; k
++) {
739 c
+= x_copy
.fraction
[i
];
740 z
->fraction
[k
] = c
/ y
;
741 c
= MP_BASE
* (c
- y
* z
->fraction
[k
]);
748 for (k
= kh
; k
< MP_T
+ 4; k
++) {
749 z
->fraction
[k
] = c
/ y
;
750 c
= MP_BASE
* (c
- y
* z
->fraction
[k
]);
759 /* HERE NEED SIMULATED DOUBLE-PRECISION DIVISION */
761 j2
= y
- j1
* MP_BASE
;
763 /* LOOK FOR FIRST NONZERO DIGIT */
766 c
= MP_BASE
* c
+ c2
;
767 c2
= i
< MP_T
? x_copy
.fraction
[i
] : 0;
769 } while (c
< j1
|| (c
== j1
&& c2
< j2
));
771 /* COMPUTE T+4 QUOTIENT DIGITS */
772 z
->exponent
+= 1 - i
;
775 /* MAIN LOOP FOR LARGE ABS(y) CASE */
776 for (k
= 1; k
<= MP_T
+ 4; k
++) {
779 /* GET APPROXIMATE QUOTIENT FIRST */
782 /* NOW REDUCE SO OVERFLOW DOES NOT OCCUR */
785 /* HERE IQ*B WOULD POSSIBLY OVERFLOW SO INCREASE IR */
790 iq
= iq
* MP_BASE
- ir
* j2
;
792 /* HERE IQ NEGATIVE SO IR WAS TOO LARGE */
798 iq
+= x_copy
.fraction
[i
];
802 /* R(K) = QUOTIENT, C = REMAINDER */
803 z
->fraction
[k
- 1] = iqj
+ ir
;
813 /* CARRY NEGATIVE SO OVERFLOW MUST HAVE OCCURRED */
814 mperr("*** INTEGER OVERFLOW IN MP_DIVIDE_INTEGER, B TOO LARGE ***");
815 mp_set_from_integer(0, z
);
820 mp_divide_integer(const MPNumber
*x
, int64_t y
, MPNumber
*z
)
822 if (mp_is_complex(x
)) {
825 mp_real_component(x
, &re_z
);
826 mp_imaginary_component(x
, &im_z
);
827 mp_divide_integer_real(&re_z
, y
, &re_z
);
828 mp_divide_integer_real(&im_z
, y
, &im_z
);
829 mp_set_from_complex(&re_z
, &im_z
, z
);
832 mp_divide_integer_real(x
, y
, z
);
837 mp_is_integer(const MPNumber
*x
)
841 if (mp_is_complex(x
))
844 /* This fix is required for 1/3 repiprocal not being detected as an integer */
845 /* Multiplication and division by 10000 is used to get around a
846 * limitation to the "fix" for Sun bugtraq bug #4006391 in the
847 * mp_floor() routine in mp.c, when the exponent is less than 1.
849 mp_set_from_integer(10000, &t3
);
850 mp_multiply(x
, &t3
, &t1
);
851 mp_divide(&t1
, &t3
, &t1
);
853 return mp_is_equal(&t1
, &t2
);
855 /* Correct way to check for integer */
858 // Zero is an integer
863 if (x->exponent <= 0)
866 // Look for fractional components
867 for (i = x->exponent; i < MP_SIZE; i++) {
868 if (x->fraction[i] != 0)
877 mp_is_positive_integer(const MPNumber
*x
)
879 if (mp_is_complex(x
))
882 return x
->sign
>= 0 && mp_is_integer(x
);
887 mp_is_natural(const MPNumber
*x
)
889 if (mp_is_complex(x
))
892 return x
->sign
> 0 && mp_is_integer(x
);
897 mp_is_complex(const MPNumber
*x
)
899 return x
->im_sign
!= 0;
904 mp_is_equal(const MPNumber
*x
, const MPNumber
*y
)
906 return mp_compare_mp_to_mp(x
, y
) == 0;
910 /* Return e^x for |x| < 1 USING AN O(SQRT(T).M(T)) ALGORITHM
911 * DESCRIBED IN - R. P. BRENT, THE COMPLEXITY OF MULTIPLE-
912 * PRECISION ARITHMETIC (IN COMPLEXITY OF COMPUTATIONAL PROBLEM
913 * SOLVING, UNIV. OF QUEENSLAND PRESS, BRISBANE, 1976, 126-165).
914 * ASYMPTOTICALLY FASTER METHODS EXIST, BUT ARE NOT USEFUL
915 * UNLESS T IS VERY LARGE. SEE COMMENTS TO MP_ATAN AND MPPIGL.
918 mp_exp(const MPNumber
*x
, MPNumber
*z
)
926 mp_set_from_integer(1, z
);
930 /* Only defined for |x| < 1 */
931 if (x
->exponent
> 0) {
932 mperr("*** ABS(X) NOT LESS THAN 1 IN CALL TO MP_EXP ***");
933 mp_set_from_integer(0, z
);
937 mp_set_from_mp(x
, &t1
);
938 rlb
= log((float)MP_BASE
);
940 /* Compute approximately optimal q (and divide x by 2^q) */
941 q
= (int)(sqrt((float)MP_T
* 0.48f
* rlb
) + (float) x
->exponent
* 1.44f
* rlb
);
949 for (i
= 1; i
<= q
; ++i
) {
951 if (ic
< ib
&& ic
!= MP_BASE
&& i
< q
)
953 mp_divide_integer(&t1
, ic
, &t1
);
958 if (mp_is_zero(&t1
)) {
959 mp_set_from_integer(0, z
);
963 /* Sum series, reducing t where possible */
964 mp_set_from_mp(&t1
, z
);
965 mp_set_from_mp(&t1
, &t2
);
966 for (i
= 2; MP_T
+ t2
.exponent
- z
->exponent
> 0; i
++) {
967 mp_multiply(&t1
, &t2
, &t2
);
968 mp_divide_integer(&t2
, i
, &t2
);
974 /* Apply (x+1)^2 - 1 = x(2 + x) for q iterations */
975 for (i
= 1; i
<= q
; ++i
) {
976 mp_add_integer(z
, 2, &t1
);
977 mp_multiply(&t1
, z
, z
);
980 mp_add_integer(z
, 1, z
);
985 mp_epowy_real(const MPNumber
*x
, MPNumber
*z
)
994 mp_set_from_integer(1, z
);
998 /* If |x| < 1 use mp_exp */
999 if (x
->exponent
<= 0) {
1004 /* NOW SAFE TO CONVERT X TO REAL */
1005 rx
= mp_cast_to_float(x
);
1007 /* SAVE SIGN AND WORK WITH ABS(X) */
1011 /* GET FRACTIONAL AND INTEGER PARTS OF ABS(X) */
1012 ix
= mp_cast_to_int(&t2
);
1013 mp_fractional_component(&t2
, &t2
);
1015 /* ATTACH SIGN TO FRACTIONAL PART AND COMPUTE EXP OF IT */
1019 /* COMPUTE E-2 OR 1/E USING TWO EXTRA DIGITS IN CASE ABS(X) LARGE
1020 * (BUT ONLY ONE EXTRA DIGIT IF T < 4)
1027 /* LOOP FOR E COMPUTATION. DECREASE T IF POSSIBLE. */
1029 mp_set_from_integer(xs
, &t1
);
1032 for (i
= 2 ; ; i
++) {
1033 if (min(tss
, tss
+ 2 + t1
.exponent
) <= 2)
1036 mp_divide_integer(&t1
, i
* xs
, &t1
);
1037 mp_add(&t2
, &t1
, &t2
);
1038 if (mp_is_zero(&t1
))
1042 /* RAISE E OR 1/E TO POWER IX */
1044 mp_add_integer(&t2
, 2, &t2
);
1045 mp_xpowy_integer(&t2
, ix
, &t2
);
1047 /* MULTIPLY EXPS OF INTEGER AND FRACTIONAL PARTS */
1048 mp_multiply(z
, &t2
, z
);
1050 /* CHECK THAT RELATIVE ERROR LESS THAN 0.01 UNLESS ABS(X) LARGE
1051 * (WHEN EXP MIGHT OVERFLOW OR UNDERFLOW)
1053 if (fabs(rx
) > 10.0f
)
1056 rz
= mp_cast_to_float(z
);
1057 r__1
= rz
- exp(rx
);
1058 if (fabs(r__1
) < rz
* 0.01f
)
1061 /* THE FOLLOWING MESSAGE MAY INDICATE THAT
1062 * B**(T-1) IS TOO SMALL, OR THAT M IS TOO SMALL SO THE
1063 * RESULT UNDERFLOWED.
1065 mperr("*** ERROR OCCURRED IN MP_EPOWY, RESULT INCORRECT ***");
1070 mp_epowy(const MPNumber
*x
, MPNumber
*z
)
1073 if (mp_is_zero(x
)) {
1074 mp_set_from_integer(1, z
);
1078 if (mp_is_complex(x
)) {
1079 MPNumber x_real
, r
, theta
;
1081 mp_real_component(x
, &x_real
);
1082 mp_imaginary_component(x
, &theta
);
1084 mp_epowy_real(&x_real
, &r
);
1085 mp_set_from_polar(&r
, MP_RADIANS
, &theta
, z
);
1088 mp_epowy_real(x
, z
);
1092 /* RETURNS K = K/GCD AND L = L/GCD, WHERE GCD IS THE
1093 * GREATEST COMMON DIVISOR OF K AND L.
1094 * SAVE INPUT PARAMETERS IN LOCAL VARIABLES
1097 mp_gcd(int64_t *k
, int64_t *l
)
1104 /* IF J = 0 RETURN (1, 0) UNLESS I = 0, THEN (0, 0) */
1112 /* EUCLIDEAN ALGORITHM LOOP */
1123 /* HERE J IS THE GCD OF K AND L */
1130 mp_is_zero(const MPNumber
*x
)
1132 return x
->sign
== 0 && x
->im_sign
== 0;
1137 mp_is_negative(const MPNumber
*x
)
1144 mp_is_greater_equal(const MPNumber
*x
, const MPNumber
*y
)
1146 return mp_compare_mp_to_mp(x
, y
) >= 0;
1151 mp_is_greater_than(const MPNumber
*x
, const MPNumber
*y
)
1153 return mp_compare_mp_to_mp(x
, y
) > 0;
1158 mp_is_less_equal(const MPNumber
*x
, const MPNumber
*y
)
1160 return mp_compare_mp_to_mp(x
, y
) <= 0;
1164 /* RETURNS MP Y = LN(1+X) IF X IS AN MP NUMBER SATISFYING THE
1165 * CONDITION ABS(X) < 1/B, ERROR OTHERWISE.
1166 * USES NEWTONS METHOD TO SOLVE THE EQUATION
1167 * EXP1(-Y) = X, THEN REVERSES SIGN OF Y.
1170 mp_lns(const MPNumber
*x
, MPNumber
*z
)
1173 MPNumber t1
, t2
, t3
;
1176 if (mp_is_zero(x
)) {
1177 mp_set_from_integer(0, z
);
1181 /* Get starting approximation -ln(1+x) ~= -x + x^2/2 - x^3/3 + x^4/4 */
1182 mp_set_from_mp(x
, &t2
);
1183 mp_divide_integer(x
, 4, &t1
);
1184 mp_add_fraction(&t1
, -1, 3, &t1
);
1185 mp_multiply(x
, &t1
, &t1
);
1186 mp_add_fraction(&t1
, 1, 2, &t1
);
1187 mp_multiply(x
, &t1
, &t1
);
1188 mp_add_integer(&t1
, -1, &t1
);
1189 mp_multiply(x
, &t1
, z
);
1191 /* Solve using Newtons method */
1197 /* t3 = (e^t3 - 1) */
1198 /* z = z - (t2 + t3 + (t2 * t3)) */
1200 mp_add_integer(&t3
, -1, &t3
);
1201 mp_multiply(&t2
, &t3
, &t1
);
1202 mp_add(&t3
, &t1
, &t3
);
1203 mp_add(&t2
, &t3
, &t3
);
1204 mp_subtract(z
, &t3
, z
);
1208 /* FOLLOWING LOOP COMPUTES NEXT VALUE OF T TO USE.
1209 * BECAUSE NEWTONS METHOD HAS 2ND ORDER CONVERGENCE,
1210 * WE CAN ALMOST DOUBLE T EACH TIME.
1221 /* CHECK THAT NEWTON ITERATION WAS CONVERGING AS EXPECTED */
1222 if (t3
.sign
!= 0 && t3
.exponent
<< 1 > it0
- MP_T
) {
1223 mperr("*** ERROR OCCURRED IN MP_LNS, NEWTON ITERATION NOT CONVERGING PROPERLY ***");
1231 mp_ln_real(const MPNumber
*x
, MPNumber
*z
)
1237 /* LOOP TO GET APPROXIMATE LN(X) USING SINGLE-PRECISION */
1238 mp_set_from_mp(x
, &t1
);
1239 mp_set_from_integer(0, z
);
1240 for(k
= 0; k
< 10; k
++)
1242 /* COMPUTE FINAL CORRECTION ACCURATELY USING MP_LNS */
1243 mp_add_integer(&t1
, -1, &t2
);
1244 if (mp_is_zero(&t2
) || t2
.exponent
+ 1 <= 0) {
1250 /* REMOVE EXPONENT TO AVOID FLOATING-POINT OVERFLOW */
1253 rx
= mp_cast_to_float(&t1
);
1255 rlx
= log(rx
) + (float)e
* log((float)MP_BASE
);
1256 mp_set_from_float(-(double)rlx
, &t2
);
1258 /* UPDATE Z AND COMPUTE ACCURATE EXP OF APPROXIMATE LOG */
1259 mp_subtract(z
, &t2
, z
);
1262 /* COMPUTE RESIDUAL WHOSE LOG IS STILL TO BE FOUND */
1263 mp_multiply(&t1
, &t2
, &t1
);
1266 mperr("*** ERROR IN MP_LN, ITERATION NOT CONVERGING ***");
1271 mp_ln(const MPNumber
*x
, MPNumber
*z
)
1273 /* ln(0) undefined */
1274 if (mp_is_zero(x
)) {
1275 /* Translators: Error displayed when attempting to take logarithm of zero */
1276 mperr(_("Logarithm of zero is undefined"));
1277 mp_set_from_integer(0, z
);
1281 /* ln(-x) complex */
1282 /* FIXME: Make complex numbers optional */
1283 /*if (mp_is_negative(x)) {
1284 // Translators: Error displayed attempted to take logarithm of negative value
1285 mperr(_("Logarithm of negative values is undefined"));
1286 mp_set_from_integer(0, z);
1290 if (mp_is_complex(x
) || mp_is_negative(x
)) {
1291 MPNumber r
, theta
, z_real
;
1293 /* ln(re^iθ) = e^(ln(r)+iθ) */
1295 mp_arg(x
, MP_RADIANS
, &theta
);
1297 mp_ln_real(&r
, &z_real
);
1298 mp_set_from_complex(&z_real
, &theta
, z
);
1306 mp_logarithm(int64_t n
, const MPNumber
*x
, MPNumber
*z
)
1310 /* log(0) undefined */
1311 if (mp_is_zero(x
)) {
1312 /* Translators: Error displayed when attempting to take logarithm of zero */
1313 mperr(_("Logarithm of zero is undefined"));
1314 mp_set_from_integer(0, z
);
1318 /* logn(x) = ln(x) / ln(n) */
1319 mp_set_from_integer(n
, &t1
);
1322 mp_divide(&t2
, &t1
, z
);
1327 mp_is_less_than(const MPNumber
*x
, const MPNumber
*y
)
1329 return mp_compare_mp_to_mp(x
, y
) < 0;
1334 mp_multiply_real(const MPNumber
*x
, const MPNumber
*y
, MPNumber
*z
)
1340 if (x
->sign
== 0 || y
->sign
== 0) {
1341 mp_set_from_integer(0, z
);
1345 z
->sign
= x
->sign
* y
->sign
;
1346 z
->exponent
= x
->exponent
+ y
->exponent
;
1347 memset(&r
, 0, sizeof(MPNumber
));
1349 /* PERFORM MULTIPLICATION */
1351 for (i
= 0; i
< MP_T
; i
++) {
1354 xi
= x
->fraction
[i
];
1356 /* FOR SPEED, PUT THE NUMBER WITH MANY ZEROS FIRST */
1361 for (j
= 0; j
< min(MP_T
, MP_T
+ 3 - i
); j
++)
1362 r
.fraction
[i
+j
+1] += xi
* y
->fraction
[j
];
1367 /* CHECK FOR LEGAL BASE B DIGIT */
1368 if (xi
< 0 || xi
>= MP_BASE
) {
1369 mperr("*** ILLEGAL BASE B DIGIT IN CALL TO MP_MULTIPLY, POSSIBLE OVERWRITING PROBLEM ***");
1370 mp_set_from_integer(0, z
);
1374 /* PROPAGATE CARRIES AT END AND EVERY EIGHTH TIME,
1375 * FASTER THAN DOING IT EVERY TIME.
1377 for (j
= MP_T
+ 3; j
>= 0; j
--) {
1378 int ri
= r
.fraction
[j
] + c
;
1380 mperr("*** INTEGER OVERFLOW IN MP_MULTIPLY, B TOO LARGE ***");
1381 mp_set_from_integer(0, z
);
1385 r
.fraction
[j
] = ri
- MP_BASE
* c
;
1388 mperr("*** ILLEGAL BASE B DIGIT IN CALL TO MP_MULTIPLY, POSSIBLE OVERWRITING PROBLEM ***");
1389 mp_set_from_integer(0, z
);
1396 if (xi
< 0 || xi
>= MP_BASE
) {
1397 mperr("*** ILLEGAL BASE B DIGIT IN CALL TO MP_MULTIPLY, POSSIBLE OVERWRITING PROBLEM ***");
1398 mp_set_from_integer(0, z
);
1403 for (i
= MP_T
+ 3; i
>= 0; i
--) {
1404 int ri
= r
.fraction
[i
] + c
;
1406 mperr("*** INTEGER OVERFLOW IN MP_MULTIPLY, B TOO LARGE ***");
1407 mp_set_from_integer(0, z
);
1411 r
.fraction
[i
] = ri
- MP_BASE
* c
;
1415 mperr("*** ILLEGAL BASE B DIGIT IN CALL TO MP_MULTIPLY, POSSIBLE OVERWRITING PROBLEM ***");
1416 mp_set_from_integer(0, z
);
1421 /* Clear complex part */
1424 memset(z
->im_fraction
, 0, sizeof(int) * MP_SIZE
);
1426 /* NORMALIZE AND ROUND RESULT */
1427 // FIXME: Use stack variable because of mp_normalize brokeness
1428 for (i
= 0; i
< MP_SIZE
; i
++)
1429 z
->fraction
[i
] = r
.fraction
[i
];
1435 mp_multiply(const MPNumber
*x
, const MPNumber
*y
, MPNumber
*z
)
1438 if (mp_is_zero(x
) || mp_is_zero(y
)) {
1439 mp_set_from_integer(0, z
);
1443 /* (a+bi)(c+di) = (ac-bd)+(ad+bc)i */
1444 if (mp_is_complex(x
) || mp_is_complex(y
)) {
1445 MPNumber real_x
, real_y
, im_x
, im_y
, t1
, t2
, real_z
, im_z
;
1447 mp_real_component(x
, &real_x
);
1448 mp_imaginary_component(x
, &im_x
);
1449 mp_real_component(y
, &real_y
);
1450 mp_imaginary_component(y
, &im_y
);
1452 mp_multiply_real(&real_x
, &real_y
, &t1
);
1453 mp_multiply_real(&im_x
, &im_y
, &t2
);
1454 mp_subtract(&t1
, &t2
, &real_z
);
1456 mp_multiply_real(&real_x
, &im_y
, &t1
);
1457 mp_multiply_real(&im_x
, &real_y
, &t2
);
1458 mp_add(&t1
, &t2
, &im_z
);
1460 mp_set_from_complex(&real_z
, &im_z
, z
);
1463 mp_multiply_real(x
, y
, z
);
1469 mp_multiply_integer_real(const MPNumber
*x
, int64_t y
, MPNumber
*z
)
1475 if (mp_is_zero(x
) || y
== 0) {
1476 mp_set_from_integer(0, z
);
1480 /* x*1 = x, x*-1 = -x */
1481 // FIXME: Why is this not working? mp_ext is using this function to do a normalization
1482 /*if (y == 1 || y == -1) {
1484 mp_invert_sign(x, z);
1486 mp_set_from_mp(x, z);
1490 /* Copy x as z may also refer to x */
1491 mp_set_from_mp(x
, &x_copy
);
1492 mp_set_from_integer(0, z
);
1496 z
->sign
= -x_copy
.sign
;
1499 z
->sign
= x_copy
.sign
;
1500 z
->exponent
= x_copy
.exponent
+ 4;
1502 /* FORM PRODUCT IN ACCUMULATOR */
1505 /* IF y*B NOT REPRESENTABLE AS AN INTEGER WE HAVE TO SIMULATE
1506 * DOUBLE-PRECISION MULTIPLICATION.
1510 if (y
>= max(MP_BASE
<< 3, 32767 / MP_BASE
)) {
1513 /* HERE J IS TOO LARGE FOR SINGLE-PRECISION MULTIPLICATION */
1515 j2
= y
- j1
* MP_BASE
;
1518 for (i
= MP_T
+ 3; i
>= 0; i
--) {
1519 int64_t c1
, c2
, is
, ix
, t
;
1522 c2
= c
- MP_BASE
* c1
;
1525 ix
= x_copy
.fraction
[i
- 4];
1529 c
= j1
* ix
+ c1
+ is
;
1530 z
->fraction
[i
] = t
- MP_BASE
* is
;
1537 for (i
= MP_T
+ 3; i
>= 4; i
--) {
1538 ri
= y
* x_copy
.fraction
[i
- 4] + c
;
1540 z
->fraction
[i
] = ri
- MP_BASE
* c
;
1543 /* CHECK FOR INTEGER OVERFLOW */
1545 mperr("*** INTEGER OVERFLOW IN mp_multiply_integer, B TOO LARGE ***");
1546 mp_set_from_integer(0, z
);
1550 /* HAVE TO TREAT FIRST FOUR WORDS OF R SEPARATELY */
1551 for (i
= 3; i
>= 0; i
--) {
1556 z
->fraction
[i
] = t
- MP_BASE
* c
;
1560 /* HAVE TO SHIFT RIGHT HERE AS CARRY OFF END */
1564 for (i
= MP_T
+ 3; i
>= 1; i
--)
1565 z
->fraction
[i
] = z
->fraction
[i
- 1];
1568 z
->fraction
[0] = t
- MP_BASE
* c
;
1573 mperr("*** INTEGER OVERFLOW IN mp_multiply_integer, B TOO LARGE ***");
1574 mp_set_from_integer(0, z
);
1580 memset(z
->im_fraction
, 0, sizeof(int) * MP_SIZE
);
1586 mp_multiply_integer(const MPNumber
*x
, int64_t y
, MPNumber
*z
)
1588 if (mp_is_complex(x
)) {
1589 MPNumber re_z
, im_z
;
1590 mp_real_component(x
, &re_z
);
1591 mp_imaginary_component(x
, &im_z
);
1592 mp_multiply_integer_real(&re_z
, y
, &re_z
);
1593 mp_multiply_integer_real(&im_z
, y
, &im_z
);
1594 mp_set_from_complex(&re_z
, &im_z
, z
);
1597 mp_multiply_integer_real(x
, y
, z
);
1602 mp_multiply_fraction(const MPNumber
*x
, int64_t numerator
, int64_t denominator
, MPNumber
*z
)
1604 if (denominator
== 0) {
1605 mperr(_("Division by zero is undefined"));
1606 mp_set_from_integer(0, z
);
1610 if (numerator
== 0) {
1611 mp_set_from_integer(0, z
);
1615 /* Reduce to lowest terms */
1616 mp_gcd(&numerator
, &denominator
);
1617 mp_divide_integer(x
, denominator
, z
);
1618 mp_multiply_integer(z
, numerator
, z
);
1623 mp_invert_sign(const MPNumber
*x
, MPNumber
*z
)
1625 mp_set_from_mp(x
, z
);
1627 z
->im_sign
= -z
->im_sign
;
1631 // FIXME: Is r->fraction large enough? It seems to be in practise but it may be MP_T+4 instead of MP_T
1632 // FIXME: There is some sort of stack corruption/use of unitialised variables here. Some functions are
1633 // using stack variables as x otherwise there are corruption errors. e.g. "Cos(45) - 1/Sqrt(2) = -0"
1634 // (try in scientific mode)
1636 mp_normalize(MPNumber
*x
)
1640 /* Find first non-zero digit */
1641 for (start_index
= 0; start_index
< MP_SIZE
&& x
->fraction
[start_index
] == 0; start_index
++);
1644 if (start_index
>= MP_SIZE
) {
1650 /* Shift left so first digit is non-zero */
1651 if (start_index
> 0) {
1654 x
->exponent
-= start_index
;
1655 for (i
= 0; (i
+ start_index
) < MP_SIZE
; i
++)
1656 x
->fraction
[i
] = x
->fraction
[i
+ start_index
];
1657 for (; i
< MP_SIZE
; i
++)
1664 mp_pwr(const MPNumber
*x
, const MPNumber
*y
, MPNumber
*z
)
1668 /* (-x)^y imaginary */
1669 /* FIXME: Make complex numbers optional */
1670 /*if (x->sign < 0) {
1671 mperr(_("The power of negative numbers is only defined for integer exponents"));
1672 mp_set_from_integer(0, z);
1676 /* 0^y = 0, 0^-y undefined */
1677 if (mp_is_zero(x
)) {
1678 mp_set_from_integer(0, z
);
1680 mperr(_("The power of zero is undefined for a negative exponent"));
1685 if (mp_is_zero(y
)) {
1686 mp_set_from_integer(1, z
);
1691 mp_multiply(y
, &t
, z
);
1697 mp_reciprocal_real(const MPNumber
*x
, MPNumber
*z
)
1703 if (mp_is_zero(x
)) {
1704 mperr(_("Reciprocal of zero is undefined"));
1705 mp_set_from_integer(0, z
);
1709 /* Start by approximating value using floating point */
1710 mp_set_from_mp(x
, &t1
);
1712 mp_set_from_float(1.0f
/ mp_cast_to_float(&t1
), &t1
);
1713 t1
.exponent
-= x
->exponent
;
1719 /* t1 = t1 - (t1 * ((x * t1) - 1)) (2*t1 - t1^2*x) */
1720 mp_multiply(x
, &t1
, &t2
);
1721 mp_add_integer(&t2
, -1, &t2
);
1722 mp_multiply(&t1
, &t2
, &t2
);
1723 mp_subtract(&t1
, &t2
, &t1
);
1727 /* FOLLOWING LOOP ALMOST DOUBLES T (POSSIBLE
1728 * BECAUSE NEWTONS METHOD HAS 2ND ORDER CONVERGENCE).
1739 /* RETURN IF NEWTON ITERATION WAS CONVERGING */
1740 if (t2
.sign
!= 0 && (t1
.exponent
- t2
.exponent
) << 1 < MP_T
- it0
) {
1741 /* THE FOLLOWING MESSAGE MAY INDICATE THAT B**(T-1) IS TOO SMALL,
1742 * OR THAT THE STARTING APPROXIMATION IS NOT ACCURATE ENOUGH.
1744 mperr("*** ERROR OCCURRED IN MP_RECIPROCAL, NEWTON ITERATION NOT CONVERGING PROPERLY ***");
1747 mp_set_from_mp(&t1
, z
);
1752 mp_reciprocal(const MPNumber
*x
, MPNumber
*z
)
1754 if (mp_is_complex(x
)) {
1756 MPNumber real_x
, im_x
;
1758 mp_real_component(x
, &real_x
);
1759 mp_imaginary_component(x
, &im_x
);
1761 /* 1/(a+bi) = (a-bi)/(a+bi)(a-bi) = (a-bi)/(a²+b²) */
1762 mp_multiply(&real_x
, &real_x
, &t1
);
1763 mp_multiply(&im_x
, &im_x
, &t2
);
1764 mp_add(&t1
, &t2
, &t1
);
1765 mp_reciprocal_real(&t1
, z
);
1766 mp_conjugate(x
, &t1
);
1767 mp_multiply(&t1
, z
, z
);
1770 mp_reciprocal_real(x
, z
);
1775 mp_root_real(const MPNumber
*x
, int64_t n
, MPNumber
*z
)
1777 float approximation
;
1783 mp_set_from_mp(x
, z
);
1787 /* x^(1/0) invalid */
1789 mperr(_("Root must be non-zero"));
1790 mp_set_from_integer(0, z
);
1796 /* LOSS OF ACCURACY IF NP LARGE, SO ONLY ALLOW NP <= MAX (B, 64) */
1797 if (np
> max(MP_BASE
, 64)) {
1798 mperr("*** ABS(N) TOO LARGE IN CALL TO MP_ROOT ***");
1799 mp_set_from_integer(0, z
);
1803 /* 0^(1/n) = 0 for positive n */
1804 if (mp_is_zero(x
)) {
1805 mp_set_from_integer(0, z
);
1807 mperr(_("Negative root of zero is undefined"));
1811 // FIXME: Imaginary root
1812 if (x
->sign
< 0 && np
% 2 == 0) {
1813 mperr(_("nth root of negative number is undefined for even n"));
1814 mp_set_from_integer(0, z
);
1818 /* DIVIDE EXPONENT BY NP */
1819 ex
= x
->exponent
/ np
;
1821 /* Get initial approximation */
1822 mp_set_from_mp(x
, &t1
);
1824 approximation
= exp(((float)(np
* ex
- x
->exponent
) * log((float)MP_BASE
) -
1825 log((fabs(mp_cast_to_float(&t1
))))) / (float)np
);
1826 mp_set_from_float(approximation
, &t1
);
1830 /* MAIN ITERATION LOOP */
1835 /* t1 = t1 - ((t1 * ((x * t1^np) - 1)) / np) */
1836 mp_xpowy_integer(&t1
, np
, &t2
);
1837 mp_multiply(x
, &t2
, &t2
);
1838 mp_add_integer(&t2
, -1, &t2
);
1839 mp_multiply(&t1
, &t2
, &t2
);
1840 mp_divide_integer(&t2
, np
, &t2
);
1841 mp_subtract(&t1
, &t2
, &t1
);
1843 /* FOLLOWING LOOP ALMOST DOUBLES T (POSSIBLE BECAUSE
1844 * NEWTONS METHOD HAS 2ND ORDER CONVERGENCE).
1858 /* NOW R(I2) IS X**(-1/NP)
1859 * CHECK THAT NEWTON ITERATION WAS CONVERGING
1861 if (t2
.sign
!= 0 && (t1
.exponent
- t2
.exponent
) << 1 < MP_T
- it0
) {
1862 /* THE FOLLOWING MESSAGE MAY INDICATE THAT B**(T-1) IS TOO SMALL,
1863 * OR THAT THE INITIAL APPROXIMATION OBTAINED USING ALOG AND EXP
1864 * IS NOT ACCURATE ENOUGH.
1866 mperr("*** ERROR OCCURRED IN MP_ROOT, NEWTON ITERATION NOT CONVERGING PROPERLY ***");
1870 mp_xpowy_integer(&t1
, n
- 1, &t1
);
1871 mp_multiply(x
, &t1
, z
);
1875 mp_set_from_mp(&t1
, z
);
1880 mp_root(const MPNumber
*x
, int64_t n
, MPNumber
*z
)
1882 if (!mp_is_complex(x
) && mp_is_negative(x
) && n
% 2 == 1) {
1884 mp_root_real(z
, n
, z
);
1885 mp_invert_sign(z
, z
);
1887 else if (mp_is_complex(x
) || mp_is_negative(x
)) {
1891 mp_arg(x
, MP_RADIANS
, &theta
);
1893 mp_root_real(&r
, n
, &r
);
1894 mp_divide_integer(&theta
, n
, &theta
);
1895 mp_set_from_polar(&r
, MP_RADIANS
, &theta
, z
);
1898 mp_root_real(x
, n
, z
);
1903 mp_sqrt(const MPNumber
*x
, MPNumber
*z
)
1906 mp_set_from_integer(0, z
);
1907 /* FIXME: Make complex numbers optional */
1908 /*else if (x->sign < 0) {
1909 mperr(_("Square root is undefined for negative values"));
1910 mp_set_from_integer(0, z);
1916 mp_multiply(x
, &t
, z
);
1917 mp_ext(t
.fraction
[0], z
->fraction
[0], z
);
1923 mp_factorial(const MPNumber
*x
, MPNumber
*z
)
1928 if (mp_is_zero(x
)) {
1929 mp_set_from_integer(1, z
);
1932 if (!mp_is_natural(x
)) {
1933 /* Translators: Error displayed when attempted take the factorial of a fractional number */
1934 mperr(_("Factorial is only defined for natural numbers"));
1935 mp_set_from_integer(0, z
);
1939 /* Convert to integer - if couldn't be converted then the factorial would be too big anyway */
1940 value
= mp_cast_to_int(x
);
1941 mp_set_from_mp(x
, z
);
1942 for (i
= 2; i
< value
; i
++)
1943 mp_multiply_integer(z
, i
, z
);
1948 mp_modulus_divide(const MPNumber
*x
, const MPNumber
*y
, MPNumber
*z
)
1952 if (!mp_is_integer(x
) || !mp_is_integer(y
)) {
1953 /* Translators: Error displayed when attemping to do a modulus division on non-integer numbers */
1954 mperr(_("Modulus division is only defined for integers"));
1955 mp_set_from_integer(0, z
);
1958 mp_divide(x
, y
, &t1
);
1960 mp_multiply(&t1
, y
, &t2
);
1961 mp_subtract(x
, &t2
, z
);
1963 mp_set_from_integer(0, &t1
);
1964 if ((mp_is_less_than(y
, &t1
) && mp_is_greater_than(z
, &t1
)) || mp_is_less_than(z
, &t1
))
1970 mp_xpowy(const MPNumber
*x
, const MPNumber
*y
, MPNumber
*z
)
1972 if (mp_is_integer(y
)) {
1973 mp_xpowy_integer(x
, mp_cast_to_int(y
), z
);
1975 MPNumber reciprocal
;
1976 mp_reciprocal(y
, &reciprocal
);
1977 if (mp_is_integer(&reciprocal
))
1978 mp_root(x
, mp_cast_to_int(&reciprocal
), z
);
1986 mp_xpowy_integer(const MPNumber
*x
, int64_t n
, MPNumber
*z
)
1992 if (mp_is_zero(x
) && n
< 0) {
1993 /* Translators: Error displayed when attempted to raise 0 to a negative exponent */
1994 mperr(_("The power of zero is undefined for a negative exponent"));
1995 mp_set_from_integer(0, z
);
2001 mp_set_from_integer(1, z
);
2006 if (mp_is_zero(x
)) {
2007 mp_set_from_integer(0, z
);
2013 mp_set_from_mp(x
, z
);
2018 mp_reciprocal(x
, &t
);
2022 mp_set_from_mp(x
, &t
);
2024 /* Multply x n times */
2025 // FIXME: Can do mp_multiply(z, z, z) until close to answer (each call doubles number of multiples) */
2026 mp_set_from_integer(1, z
);
2027 for (i
= 0; i
< n
; i
++)
2028 mp_multiply(z
, &t
, z
);
2033 mp_factorize(const MPNumber
*x
)
2036 MPNumber
*factor
= g_slice_alloc0(sizeof(MPNumber
));
2037 MPNumber value
, tmp
, divisor
, root
;
2041 if (mp_is_zero(&value
)) {
2042 mp_set_from_mp(&value
, factor
);
2043 list
= g_list_append(list
, factor
);
2047 mp_set_from_integer(1, &tmp
);
2048 if (mp_is_equal(&value
, &tmp
)) {
2049 mp_set_from_mp(x
, factor
);
2050 list
= g_list_append(list
, factor
);
2054 mp_set_from_integer(2, &divisor
);
2056 mp_divide(&value
, &divisor
, &tmp
);
2057 if (mp_is_integer(&tmp
)) {
2059 mp_set_from_mp(&divisor
, factor
);
2060 list
= g_list_append(list
, factor
);
2061 factor
= g_slice_alloc0(sizeof(MPNumber
));
2067 mp_set_from_integer(3, &divisor
);
2068 mp_sqrt(&value
, &root
);
2069 while (mp_is_less_equal(&divisor
, &root
)) {
2070 mp_divide(&value
, &divisor
, &tmp
);
2071 if (mp_is_integer(&tmp
)) {
2073 mp_sqrt(&value
, &root
);
2074 mp_set_from_mp(&divisor
, factor
);
2075 list
= g_list_append(list
, factor
);
2076 factor
= g_slice_alloc0(sizeof(MPNumber
));
2078 mp_add_integer(&divisor
, 2, &tmp
);
2083 mp_set_from_integer(1, &tmp
);
2084 if (mp_is_greater_than(&value
, &tmp
)) {
2085 mp_set_from_mp(&value
, factor
);
2086 list
= g_list_append(list
, factor
);
2088 g_slice_free(MPNumber
, factor
);
2091 if (mp_is_negative(x
)) {
2092 mp_invert_sign(list
->data
, list
->data
);