1 /* Copyright (c) 1987-2008 Sun Microsystems, Inc. All Rights Reserved.
2 * Copyright (c) 2008-2009 Robert Ancell
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 #include "mp-private.h"
28 static MPNumber eulers_number
;
29 static gboolean have_eulers_number
= FALSE
;
31 // FIXME: Re-add overflow and underflow detection
33 char *mp_error
= NULL
;
35 /* THIS ROUTINE IS CALLED WHEN AN ERROR CONDITION IS ENCOUNTERED, AND
36 * AFTER A MESSAGE HAS BEEN WRITTEN TO STDERR.
39 mperr(const char *format
, ...)
44 va_start(args
, format
);
45 vsnprintf(text
, 1024, format
, args
);
50 mp_error
= strdup(text
);
69 /* ROUTINE CALLED BY MP_DIVIDE AND MP_SQRT TO ENSURE THAT
70 * RESULTS ARE REPRESENTED EXACTLY IN T-2 DIGITS IF THEY
71 * CAN BE. X IS AN MP NUMBER, I AND J ARE INTEGERS.
74 mp_ext(int i
, int j
, MPNumber
*x
)
78 if (mp_is_zero(x
) || MP_T
<= 2 || i
== 0)
81 /* COMPUTE MAXIMUM POSSIBLE ERROR IN THE LAST PLACE */
83 s
= MP_BASE
* x
->fraction
[MP_T
- 2] + x
->fraction
[MP_T
- 1];
85 /* SET LAST TWO DIGITS TO ZERO */
87 x
->fraction
[MP_T
- 2] = 0;
88 x
->fraction
[MP_T
- 1] = 0;
92 if (s
+ q
< MP_BASE
* MP_BASE
)
96 x
->fraction
[MP_T
- 2] = MP_BASE
- 1;
97 x
->fraction
[MP_T
- 1] = MP_BASE
;
99 /* NORMALIZE X (LAST DIGIT B IS OK IN MP_MULTIPLY_INTEGER) */
100 mp_multiply_integer(x
, 1, x
);
105 mp_get_eulers(MPNumber
*z
)
107 if (!have_eulers_number
) {
109 mp_set_from_integer(1, &t
);
110 mp_epowy(&t
, &eulers_number
);
111 have_eulers_number
= TRUE
;
113 mp_set_from_mp(&eulers_number
, z
);
118 mp_get_i(MPNumber
*z
)
120 mp_set_from_integer(0, z
);
123 z
->im_fraction
[0] = 1;
128 mp_abs(const MPNumber
*x
, MPNumber
*z
)
130 if (mp_is_complex(x
)){
131 MPNumber x_real
, x_im
;
133 mp_real_component(x
, &x_real
);
134 mp_imaginary_component(x
, &x_im
);
136 mp_multiply(&x_real
, &x_real
, &x_real
);
137 mp_multiply(&x_im
, &x_im
, &x_im
);
138 mp_add(&x_real
, &x_im
, z
);
142 mp_set_from_mp(x
, z
);
150 mp_arg(const MPNumber
*x
, MPAngleUnit unit
, MPNumber
*z
)
152 MPNumber x_real
, x_im
, pi
;
155 /* Translators: Error display when attempting to take argument of zero */
156 mperr(_("Argument not defined for zero"));
157 mp_set_from_integer(0, z
);
161 mp_real_component(x
, &x_real
);
162 mp_imaginary_component(x
, &x_im
);
165 if (mp_is_zero(&x_im
)) {
166 if (mp_is_negative(&x_real
))
167 convert_from_radians(&pi
, MP_RADIANS
, z
);
169 mp_set_from_integer(0, z
);
171 else if (mp_is_zero(&x_real
)) {
172 mp_set_from_mp(&pi
, z
);
173 if (mp_is_negative(&x_im
))
174 mp_divide_integer(z
, -2, z
);
176 mp_divide_integer(z
, 2, z
);
178 else if (mp_is_negative(&x_real
)) {
179 mp_divide(&x_im
, &x_real
, z
);
180 mp_atan(z
, MP_RADIANS
, z
);
181 if (mp_is_negative(&x_im
))
182 mp_subtract(z
, &pi
, z
);
187 mp_divide(&x_im
, &x_real
, z
);
188 mp_atan(z
, MP_RADIANS
, z
);
191 convert_from_radians(z
, unit
, z
);
196 mp_conjugate(const MPNumber
*x
, MPNumber
*z
)
198 mp_set_from_mp(x
, z
);
199 z
->im_sign
= -z
->im_sign
;
204 mp_real_component(const MPNumber
*x
, MPNumber
*z
)
206 mp_set_from_mp(x
, z
);
208 /* Clear imaginary component */
211 memset(z
->im_fraction
, 0, sizeof(int) * MP_SIZE
);
216 mp_imaginary_component(const MPNumber
*x
, MPNumber
*z
)
218 /* Copy imaginary component to real component */
219 z
->sign
= x
->im_sign
;
220 z
->exponent
= x
->im_exponent
;
221 memcpy(z
->fraction
, x
->im_fraction
, sizeof(int) * MP_SIZE
);
223 /* Clear (old) imaginary component */
226 memset(z
->im_fraction
, 0, sizeof(int) * MP_SIZE
);
231 mp_add_real(const MPNumber
*x
, int y_sign
, const MPNumber
*y
, MPNumber
*z
)
235 bool x_largest
= false;
236 const int *big_fraction
, *small_fraction
;
237 MPNumber x_copy
, y_copy
;
241 mp_set_from_mp(y
, z
);
246 else if (mp_is_zero(y
)) {
247 mp_set_from_mp(x
, z
);
251 sign_prod
= y_sign
* x
->sign
;
252 exp_diff
= x
->exponent
- y
->exponent
;
256 } else if (exp_diff
> 0) {
259 /* EXPONENTS EQUAL SO COMPARE SIGNS, THEN FRACTIONS IF NEC. */
261 /* Signs are not equal. find out which mantissa is larger. */
263 for (j
= 0; j
< MP_T
; j
++) {
264 int i
= x
->fraction
[j
] - y
->fraction
[j
];
274 /* Both mantissas equal, so result is zero. */
276 mp_set_from_integer(0, z
);
282 mp_set_from_mp(x
, &x_copy
);
283 mp_set_from_mp(y
, &y_copy
);
284 mp_set_from_integer(0, z
);
287 z
->sign
= x_copy
.sign
;
288 z
->exponent
= x_copy
.exponent
;
289 big_fraction
= x_copy
.fraction
;
290 small_fraction
= y_copy
.fraction
;
293 z
->exponent
= y_copy
.exponent
;
294 big_fraction
= y_copy
.fraction
;
295 small_fraction
= x_copy
.fraction
;
298 /* CLEAR GUARD DIGITS TO RIGHT OF X DIGITS */
299 for(i
= 3; i
>= med
; i
--)
300 z
->fraction
[MP_T
+ i
] = 0;
302 if (sign_prod
>= 0) {
303 /* This is probably insufficient overflow detection, but it makes us
304 * not crash at least.
306 if (MP_T
+ 3 < med
) {
307 mperr(_("Overflow: the result couldn't be calculated"));
308 mp_set_from_integer(0, z
);
312 /* HERE DO ADDITION, EXPONENT(Y) >= EXPONENT(X) */
313 for (i
= MP_T
+ 3; i
>= MP_T
; i
--)
314 z
->fraction
[i
] = small_fraction
[i
- med
];
317 for (; i
>= med
; i
--) {
318 c
= big_fraction
[i
] + small_fraction
[i
- med
] + c
;
321 /* NO CARRY GENERATED HERE */
325 /* CARRY GENERATED HERE */
326 z
->fraction
[i
] = c
- MP_BASE
;
333 c
= big_fraction
[i
] + c
;
338 /* NO CARRY POSSIBLE HERE */
340 z
->fraction
[i
] = big_fraction
[i
];
350 /* MUST SHIFT RIGHT HERE AS CARRY OFF END */
352 for (i
= MP_T
+ 3; i
> 0; i
--)
353 z
->fraction
[i
] = z
->fraction
[i
- 1];
360 for (i
= MP_T
+ med
- 1; i
>= MP_T
; i
--) {
361 /* HERE DO SUBTRACTION, ABS(Y) > ABS(X) */
362 z
->fraction
[i
] = c
- small_fraction
[i
- med
];
365 /* BORROW GENERATED HERE */
366 if (z
->fraction
[i
] < 0) {
368 z
->fraction
[i
] += MP_BASE
;
372 for(; i
>= med
; i
--) {
373 c
= big_fraction
[i
] + c
- small_fraction
[i
- med
];
375 /* NO BORROW GENERATED HERE */
379 /* BORROW GENERATED HERE */
380 z
->fraction
[i
] = c
+ MP_BASE
;
385 for (; i
>= 0; i
--) {
386 c
= big_fraction
[i
] + c
;
392 /* NO CARRY POSSIBLE HERE */
394 z
->fraction
[i
] = big_fraction
[i
];
399 z
->fraction
[i
] = c
+ MP_BASE
;
409 mp_add_with_sign(const MPNumber
*x
, int y_sign
, const MPNumber
*y
, MPNumber
*z
)
411 if (mp_is_complex(x
) || mp_is_complex(y
)) {
412 MPNumber real_x
, real_y
, im_x
, im_y
, real_z
, im_z
;
414 mp_real_component(x
, &real_x
);
415 mp_imaginary_component(x
, &im_x
);
416 mp_real_component(y
, &real_y
);
417 mp_imaginary_component(y
, &im_y
);
419 mp_add_real(&real_x
, y_sign
* y
->sign
, &real_y
, &real_z
);
420 mp_add_real(&im_x
, y_sign
* y
->im_sign
, &im_y
, &im_z
);
422 mp_set_from_complex(&real_z
, &im_z
, z
);
425 mp_add_real(x
, y_sign
* y
->sign
, y
, z
);
430 mp_add(const MPNumber
*x
, const MPNumber
*y
, MPNumber
*z
)
432 mp_add_with_sign(x
, 1, y
, z
);
437 mp_add_integer(const MPNumber
*x
, int64_t y
, MPNumber
*z
)
440 mp_set_from_integer(y
, &t
);
446 mp_add_fraction(const MPNumber
*x
, int64_t i
, int64_t j
, MPNumber
*y
)
449 mp_set_from_fraction(i
, j
, &t
);
455 mp_subtract(const MPNumber
*x
, const MPNumber
*y
, MPNumber
*z
)
457 mp_add_with_sign(x
, -1, y
, z
);
462 mp_sgn(const MPNumber
*x
, MPNumber
*z
)
465 mp_set_from_integer(0, z
);
466 else if (mp_is_negative(x
))
467 mp_set_from_integer(-1, z
);
469 mp_set_from_integer(1, z
);
473 mp_integer_component(const MPNumber
*x
, MPNumber
*z
)
478 mp_set_from_mp(x
, z
);
479 for (i
= z
->exponent
; i
< MP_SIZE
; i
++)
483 memset(z
->im_fraction
, 0, sizeof(int) * MP_SIZE
);
487 mp_fractional_component(const MPNumber
*x
, MPNumber
*z
)
491 /* Fractional component of zero is 0 */
493 mp_set_from_integer(0, z
);
498 if (x
->exponent
<= 0) {
499 mp_set_from_mp(x
, z
);
503 /* Shift fractional component */
505 for (i
= shift
; i
< MP_SIZE
&& x
->fraction
[i
] == 0; i
++)
508 z
->exponent
= x
->exponent
- shift
;
509 for (i
= 0; i
< MP_SIZE
; i
++) {
510 if (i
+ shift
>= MP_SIZE
)
513 z
->fraction
[i
] = x
->fraction
[i
+ shift
];
515 if (z
->fraction
[0] == 0)
520 memset(z
->im_fraction
, 0, sizeof(int) * MP_SIZE
);
525 mp_fractional_part(const MPNumber
*x
, MPNumber
*z
)
529 mp_subtract(x
, &f
, z
);
534 mp_floor(const MPNumber
*x
, MPNumber
*z
)
537 bool have_fraction
= false, is_negative
;
539 /* Integer component of zero = 0 */
541 mp_set_from_mp(x
, z
);
545 /* If all fractional then no integer component */
546 if (x
->exponent
<= 0) {
547 mp_set_from_integer(0, z
);
551 is_negative
= mp_is_negative(x
);
554 mp_set_from_mp(x
, z
);
555 for (i
= z
->exponent
; i
< MP_SIZE
; i
++) {
557 have_fraction
= true;
562 memset(z
->im_fraction
, 0, sizeof(int) * MP_SIZE
);
564 if (have_fraction
&& is_negative
)
565 mp_add_integer(z
, -1, z
);
570 mp_ceiling(const MPNumber
*x
, MPNumber
*z
)
575 mp_fractional_component(x
, &f
);
578 mp_add_integer(z
, 1, z
);
583 mp_round(const MPNumber
*x
, MPNumber
*z
)
588 do_floor
= !mp_is_negative(x
);
590 mp_fractional_component(x
, &f
);
591 mp_multiply_integer(&f
, 2, &f
);
593 mp_set_from_integer(1, &one
);
594 if (mp_is_greater_equal(&f
, &one
))
595 do_floor
= !do_floor
;
604 mp_compare_mp_to_mp(const MPNumber
*x
, const MPNumber
*y
)
608 if (x
->sign
!= y
->sign
) {
609 if (x
->sign
> y
->sign
)
619 /* See if numbers are of different magnitude */
620 if (x
->exponent
!= y
->exponent
) {
621 if (x
->exponent
> y
->exponent
)
627 /* Compare fractions */
628 for (i
= 0; i
< MP_SIZE
; i
++) {
629 if (x
->fraction
[i
] == y
->fraction
[i
])
632 if (x
->fraction
[i
] > y
->fraction
[i
])
644 mp_divide(const MPNumber
*x
, const MPNumber
*y
, MPNumber
*z
)
651 /* Translators: Error displayed attempted to divide by zero */
652 mperr(_("Division by zero is undefined"));
653 mp_set_from_integer(0, z
);
659 mp_set_from_integer(0, z
);
664 /* FIXME: Set exponent to zero to avoid overflow in mp_multiply??? */
665 mp_reciprocal(y
, &t
);
669 mp_multiply(x
, &t
, z
);
670 mp_ext(i
, z
->fraction
[0], z
);
676 mp_divide_integer_real(const MPNumber
*x
, int64_t y
, MPNumber
*z
)
678 int c
, i
, k
, b2
, c2
, j1
, j2
;
683 /* Translators: Error displayed attempted to divide by zero */
684 mperr(_("Division by zero is undefined"));
685 mp_set_from_integer(0, z
);
691 mp_set_from_integer(0, z
);
695 /* Division by -1 or 1 just changes sign */
696 if (y
== 1 || y
== -1) {
698 mp_invert_sign(x
, z
);
700 mp_set_from_mp(x
, z
);
704 /* Copy x as z may also refer to x */
705 mp_set_from_mp(x
, &x_copy
);
706 mp_set_from_integer(0, z
);
710 z
->sign
= -x_copy
.sign
;
713 z
->sign
= x_copy
.sign
;
714 z
->exponent
= x_copy
.exponent
;
719 /* IF y*B NOT REPRESENTABLE AS AN INTEGER HAVE TO SIMULATE
720 * LONG DIVISION. ASSUME AT LEAST 16-BIT WORD.
724 b2
= max(MP_BASE
<< 3, 32767 / MP_BASE
);
728 /* LOOK FOR FIRST NONZERO DIGIT IN QUOTIENT */
732 c
+= x_copy
.fraction
[i
];
739 /* ADJUST EXPONENT AND GET T+4 DIGITS IN QUOTIENT */
740 z
->exponent
+= 1 - i
;
742 c
= MP_BASE
* (c
- y
* r1
);
746 for (k
= 1; k
< kh
; k
++) {
747 c
+= x_copy
.fraction
[i
];
748 z
->fraction
[k
] = c
/ y
;
749 c
= MP_BASE
* (c
- y
* z
->fraction
[k
]);
756 for (k
= kh
; k
< MP_T
+ 4; k
++) {
757 z
->fraction
[k
] = c
/ y
;
758 c
= MP_BASE
* (c
- y
* z
->fraction
[k
]);
767 /* HERE NEED SIMULATED DOUBLE-PRECISION DIVISION */
769 j2
= y
- j1
* MP_BASE
;
771 /* LOOK FOR FIRST NONZERO DIGIT */
774 c
= MP_BASE
* c
+ c2
;
775 c2
= i
< MP_T
? x_copy
.fraction
[i
] : 0;
777 } while (c
< j1
|| (c
== j1
&& c2
< j2
));
779 /* COMPUTE T+4 QUOTIENT DIGITS */
780 z
->exponent
+= 1 - i
;
783 /* MAIN LOOP FOR LARGE ABS(y) CASE */
784 for (k
= 1; k
<= MP_T
+ 4; k
++) {
787 /* GET APPROXIMATE QUOTIENT FIRST */
790 /* NOW REDUCE SO OVERFLOW DOES NOT OCCUR */
793 /* HERE IQ*B WOULD POSSIBLY OVERFLOW SO INCREASE IR */
798 iq
= iq
* MP_BASE
- ir
* j2
;
800 /* HERE IQ NEGATIVE SO IR WAS TOO LARGE */
806 iq
+= x_copy
.fraction
[i
];
810 /* R(K) = QUOTIENT, C = REMAINDER */
811 z
->fraction
[k
- 1] = iqj
+ ir
;
821 /* CARRY NEGATIVE SO OVERFLOW MUST HAVE OCCURRED */
822 mperr("*** INTEGER OVERFLOW IN MP_DIVIDE_INTEGER, B TOO LARGE ***");
823 mp_set_from_integer(0, z
);
828 mp_divide_integer(const MPNumber
*x
, int64_t y
, MPNumber
*z
)
830 if (mp_is_complex(x
)) {
833 mp_real_component(x
, &re_z
);
834 mp_imaginary_component(x
, &im_z
);
835 mp_divide_integer_real(&re_z
, y
, &re_z
);
836 mp_divide_integer_real(&im_z
, y
, &im_z
);
837 mp_set_from_complex(&re_z
, &im_z
, z
);
840 mp_divide_integer_real(x
, y
, z
);
845 mp_is_integer(const MPNumber
*x
)
849 if (mp_is_complex(x
))
852 /* This fix is required for 1/3 repiprocal not being detected as an integer */
853 /* Multiplication and division by 10000 is used to get around a
854 * limitation to the "fix" for Sun bugtraq bug #4006391 in the
855 * mp_floor() routine in mp.c, when the exponent is less than 1.
857 mp_set_from_integer(10000, &t3
);
858 mp_multiply(x
, &t3
, &t1
);
859 mp_divide(&t1
, &t3
, &t1
);
861 return mp_is_equal(&t1
, &t2
);
863 /* Correct way to check for integer */
866 // Zero is an integer
871 if (x->exponent <= 0)
874 // Look for fractional components
875 for (i = x->exponent; i < MP_SIZE; i++) {
876 if (x->fraction[i] != 0)
885 mp_is_positive_integer(const MPNumber
*x
)
887 if (mp_is_complex(x
))
890 return x
->sign
>= 0 && mp_is_integer(x
);
895 mp_is_natural(const MPNumber
*x
)
897 if (mp_is_complex(x
))
900 return x
->sign
> 0 && mp_is_integer(x
);
905 mp_is_complex(const MPNumber
*x
)
907 return x
->im_sign
!= 0;
912 mp_is_equal(const MPNumber
*x
, const MPNumber
*y
)
914 return mp_compare_mp_to_mp(x
, y
) == 0;
918 /* Return e^x for |x| < 1 USING AN O(SQRT(T).M(T)) ALGORITHM
919 * DESCRIBED IN - R. P. BRENT, THE COMPLEXITY OF MULTIPLE-
920 * PRECISION ARITHMETIC (IN COMPLEXITY OF COMPUTATIONAL PROBLEM
921 * SOLVING, UNIV. OF QUEENSLAND PRESS, BRISBANE, 1976, 126-165).
922 * ASYMPTOTICALLY FASTER METHODS EXIST, BUT ARE NOT USEFUL
923 * UNLESS T IS VERY LARGE. SEE COMMENTS TO MP_ATAN AND MPPIGL.
926 mp_exp(const MPNumber
*x
, MPNumber
*z
)
934 mp_set_from_integer(1, z
);
938 /* Only defined for |x| < 1 */
939 if (x
->exponent
> 0) {
940 mperr("*** ABS(X) NOT LESS THAN 1 IN CALL TO MP_EXP ***");
941 mp_set_from_integer(0, z
);
945 mp_set_from_mp(x
, &t1
);
946 rlb
= log((float)MP_BASE
);
948 /* Compute approximately optimal q (and divide x by 2^q) */
949 q
= (int)(sqrt((float)MP_T
* 0.48f
* rlb
) + (float) x
->exponent
* 1.44f
* rlb
);
957 for (i
= 1; i
<= q
; ++i
) {
959 if (ic
< ib
&& ic
!= MP_BASE
&& i
< q
)
961 mp_divide_integer(&t1
, ic
, &t1
);
966 if (mp_is_zero(&t1
)) {
967 mp_set_from_integer(0, z
);
971 /* Sum series, reducing t where possible */
972 mp_set_from_mp(&t1
, z
);
973 mp_set_from_mp(&t1
, &t2
);
974 for (i
= 2; MP_T
+ t2
.exponent
- z
->exponent
> 0; i
++) {
975 mp_multiply(&t1
, &t2
, &t2
);
976 mp_divide_integer(&t2
, i
, &t2
);
982 /* Apply (x+1)^2 - 1 = x(2 + x) for q iterations */
983 for (i
= 1; i
<= q
; ++i
) {
984 mp_add_integer(z
, 2, &t1
);
985 mp_multiply(&t1
, z
, z
);
988 mp_add_integer(z
, 1, z
);
993 mp_epowy_real(const MPNumber
*x
, MPNumber
*z
)
1001 if (mp_is_zero(x
)) {
1002 mp_set_from_integer(1, z
);
1006 /* If |x| < 1 use mp_exp */
1007 if (x
->exponent
<= 0) {
1012 /* SEE IF ABS(X) SO LARGE THAT EXP(X) WILL CERTAINLY OVERFLOW
1013 * OR UNDERFLOW. 1.01 IS TO ALLOW FOR ERRORS IN ALOG.
1015 rlb
= log((float)MP_BASE
) * 1.01f
;
1017 /* NOW SAFE TO CONVERT X TO REAL */
1018 rx
= mp_cast_to_float(x
);
1020 /* SAVE SIGN AND WORK WITH ABS(X) */
1024 /* GET FRACTIONAL AND INTEGER PARTS OF ABS(X) */
1025 ix
= mp_cast_to_int(&t2
);
1026 mp_fractional_component(&t2
, &t2
);
1028 /* ATTACH SIGN TO FRACTIONAL PART AND COMPUTE EXP OF IT */
1032 /* COMPUTE E-2 OR 1/E USING TWO EXTRA DIGITS IN CASE ABS(X) LARGE
1033 * (BUT ONLY ONE EXTRA DIGIT IF T < 4)
1040 /* LOOP FOR E COMPUTATION. DECREASE T IF POSSIBLE. */
1042 mp_set_from_integer(xs
, &t1
);
1045 for (i
= 2 ; ; i
++) {
1046 if (min(tss
, tss
+ 2 + t1
.exponent
) <= 2)
1049 mp_divide_integer(&t1
, i
* xs
, &t1
);
1050 mp_add(&t2
, &t1
, &t2
);
1051 if (mp_is_zero(&t1
))
1055 /* RAISE E OR 1/E TO POWER IX */
1057 mp_add_integer(&t2
, 2, &t2
);
1058 mp_xpowy_integer(&t2
, ix
, &t2
);
1060 /* MULTIPLY EXPS OF INTEGER AND FRACTIONAL PARTS */
1061 mp_multiply(z
, &t2
, z
);
1063 /* CHECK THAT RELATIVE ERROR LESS THAN 0.01 UNLESS ABS(X) LARGE
1064 * (WHEN EXP MIGHT OVERFLOW OR UNDERFLOW)
1066 if (fabs(rx
) > 10.0f
)
1069 rz
= mp_cast_to_float(z
);
1070 r__1
= rz
- exp(rx
);
1071 if (fabs(r__1
) < rz
* 0.01f
)
1074 /* THE FOLLOWING MESSAGE MAY INDICATE THAT
1075 * B**(T-1) IS TOO SMALL, OR THAT M IS TOO SMALL SO THE
1076 * RESULT UNDERFLOWED.
1078 mperr("*** ERROR OCCURRED IN MP_EPOWY, RESULT INCORRECT ***");
1083 mp_epowy(const MPNumber
*x
, MPNumber
*z
)
1086 if (mp_is_zero(x
)) {
1087 mp_set_from_integer(1, z
);
1091 if (mp_is_complex(x
)) {
1092 MPNumber x_real
, r
, theta
;
1094 mp_real_component(x
, &x_real
);
1095 mp_imaginary_component(x
, &theta
);
1097 mp_epowy_real(&x_real
, &r
);
1098 mp_set_from_polar(&r
, MP_RADIANS
, &theta
, z
);
1101 mp_epowy_real(x
, z
);
1105 /* RETURNS K = K/GCD AND L = L/GCD, WHERE GCD IS THE
1106 * GREATEST COMMON DIVISOR OF K AND L.
1107 * SAVE INPUT PARAMETERS IN LOCAL VARIABLES
1110 mp_gcd(int64_t *k
, int64_t *l
)
1117 /* IF J = 0 RETURN (1, 0) UNLESS I = 0, THEN (0, 0) */
1125 /* EUCLIDEAN ALGORITHM LOOP */
1136 /* HERE J IS THE GCD OF K AND L */
1143 mp_is_zero(const MPNumber
*x
)
1145 return x
->sign
== 0 && x
->im_sign
== 0;
1150 mp_is_negative(const MPNumber
*x
)
1157 mp_is_greater_equal(const MPNumber
*x
, const MPNumber
*y
)
1159 return mp_compare_mp_to_mp(x
, y
) >= 0;
1164 mp_is_greater_than(const MPNumber
*x
, const MPNumber
*y
)
1166 return mp_compare_mp_to_mp(x
, y
) > 0;
1171 mp_is_less_equal(const MPNumber
*x
, const MPNumber
*y
)
1173 return mp_compare_mp_to_mp(x
, y
) <= 0;
1177 /* RETURNS MP Y = LN(1+X) IF X IS AN MP NUMBER SATISFYING THE
1178 * CONDITION ABS(X) < 1/B, ERROR OTHERWISE.
1179 * USES NEWTONS METHOD TO SOLVE THE EQUATION
1180 * EXP1(-Y) = X, THEN REVERSES SIGN OF Y.
1183 mp_lns(const MPNumber
*x
, MPNumber
*z
)
1186 MPNumber t1
, t2
, t3
;
1189 if (mp_is_zero(x
)) {
1190 mp_set_from_integer(0, z
);
1194 /* Get starting approximation -ln(1+x) ~= -x + x^2/2 - x^3/3 + x^4/4 */
1195 mp_set_from_mp(x
, &t2
);
1196 mp_divide_integer(x
, 4, &t1
);
1197 mp_add_fraction(&t1
, -1, 3, &t1
);
1198 mp_multiply(x
, &t1
, &t1
);
1199 mp_add_fraction(&t1
, 1, 2, &t1
);
1200 mp_multiply(x
, &t1
, &t1
);
1201 mp_add_integer(&t1
, -1, &t1
);
1202 mp_multiply(x
, &t1
, z
);
1204 /* Solve using Newtons method */
1210 /* t3 = (e^t3 - 1) */
1211 /* z = z - (t2 + t3 + (t2 * t3)) */
1213 mp_add_integer(&t3
, -1, &t3
);
1214 mp_multiply(&t2
, &t3
, &t1
);
1215 mp_add(&t3
, &t1
, &t3
);
1216 mp_add(&t2
, &t3
, &t3
);
1217 mp_subtract(z
, &t3
, z
);
1221 /* FOLLOWING LOOP COMPUTES NEXT VALUE OF T TO USE.
1222 * BECAUSE NEWTONS METHOD HAS 2ND ORDER CONVERGENCE,
1223 * WE CAN ALMOST DOUBLE T EACH TIME.
1234 /* CHECK THAT NEWTON ITERATION WAS CONVERGING AS EXPECTED */
1235 if (t3
.sign
!= 0 && t3
.exponent
<< 1 > it0
- MP_T
) {
1236 mperr("*** ERROR OCCURRED IN MP_LNS, NEWTON ITERATION NOT CONVERGING PROPERLY ***");
1244 mp_ln_real(const MPNumber
*x
, MPNumber
*z
)
1250 /* LOOP TO GET APPROXIMATE LN(X) USING SINGLE-PRECISION */
1251 mp_set_from_mp(x
, &t1
);
1252 mp_set_from_integer(0, z
);
1253 for(k
= 0; k
< 10; k
++)
1255 /* COMPUTE FINAL CORRECTION ACCURATELY USING MP_LNS */
1256 mp_add_integer(&t1
, -1, &t2
);
1257 if (mp_is_zero(&t2
) || t2
.exponent
+ 1 <= 0) {
1263 /* REMOVE EXPONENT TO AVOID FLOATING-POINT OVERFLOW */
1266 rx
= mp_cast_to_float(&t1
);
1268 rlx
= log(rx
) + (float)e
* log((float)MP_BASE
);
1269 mp_set_from_float(-(double)rlx
, &t2
);
1271 /* UPDATE Z AND COMPUTE ACCURATE EXP OF APPROXIMATE LOG */
1272 mp_subtract(z
, &t2
, z
);
1275 /* COMPUTE RESIDUAL WHOSE LOG IS STILL TO BE FOUND */
1276 mp_multiply(&t1
, &t2
, &t1
);
1279 mperr("*** ERROR IN MP_LN, ITERATION NOT CONVERGING ***");
1284 mp_ln(const MPNumber
*x
, MPNumber
*z
)
1286 /* ln(0) undefined */
1287 if (mp_is_zero(x
)) {
1288 /* Translators: Error displayed when attempting to take logarithm of zero */
1289 mperr(_("Logarithm of zero is undefined"));
1290 mp_set_from_integer(0, z
);
1294 /* ln(-x) complex */
1295 /* FIXME: Make complex numbers optional */
1296 /*if (mp_is_negative(x)) {
1297 // Translators: Error displayed attempted to take logarithm of negative value
1298 mperr(_("Logarithm of negative values is undefined"));
1299 mp_set_from_integer(0, z);
1303 if (mp_is_complex(x
) || mp_is_negative(x
)) {
1304 MPNumber r
, theta
, z_real
;
1306 /* ln(re^iθ) = e^(ln(r)+iθ) */
1308 mp_arg(x
, MP_RADIANS
, &theta
);
1310 mp_ln_real(&r
, &z_real
);
1311 mp_set_from_complex(&z_real
, &theta
, z
);
1319 mp_logarithm(int64_t n
, const MPNumber
*x
, MPNumber
*z
)
1323 /* log(0) undefined */
1324 if (mp_is_zero(x
)) {
1325 /* Translators: Error displayed when attempting to take logarithm of zero */
1326 mperr(_("Logarithm of zero is undefined"));
1327 mp_set_from_integer(0, z
);
1331 /* logn(x) = ln(x) / ln(n) */
1332 mp_set_from_integer(n
, &t1
);
1335 mp_divide(&t2
, &t1
, z
);
1340 mp_is_less_than(const MPNumber
*x
, const MPNumber
*y
)
1342 return mp_compare_mp_to_mp(x
, y
) < 0;
1347 mp_multiply_real(const MPNumber
*x
, const MPNumber
*y
, MPNumber
*z
)
1353 if (x
->sign
== 0 || y
->sign
== 0) {
1354 mp_set_from_integer(0, z
);
1358 z
->sign
= x
->sign
* y
->sign
;
1359 z
->exponent
= x
->exponent
+ y
->exponent
;
1360 memset(&r
, 0, sizeof(MPNumber
));
1362 /* PERFORM MULTIPLICATION */
1364 for (i
= 0; i
< MP_T
; i
++) {
1367 xi
= x
->fraction
[i
];
1369 /* FOR SPEED, PUT THE NUMBER WITH MANY ZEROS FIRST */
1374 for (j
= 0; j
< min(MP_T
, MP_T
+ 3 - i
); j
++)
1375 r
.fraction
[i
+j
+1] += xi
* y
->fraction
[j
];
1380 /* CHECK FOR LEGAL BASE B DIGIT */
1381 if (xi
< 0 || xi
>= MP_BASE
) {
1382 mperr("*** ILLEGAL BASE B DIGIT IN CALL TO MP_MULTIPLY, POSSIBLE OVERWRITING PROBLEM ***");
1383 mp_set_from_integer(0, z
);
1387 /* PROPAGATE CARRIES AT END AND EVERY EIGHTH TIME,
1388 * FASTER THAN DOING IT EVERY TIME.
1390 for (j
= MP_T
+ 3; j
>= 0; j
--) {
1391 int ri
= r
.fraction
[j
] + c
;
1393 mperr("*** INTEGER OVERFLOW IN MP_MULTIPLY, B TOO LARGE ***");
1394 mp_set_from_integer(0, z
);
1398 r
.fraction
[j
] = ri
- MP_BASE
* c
;
1401 mperr("*** ILLEGAL BASE B DIGIT IN CALL TO MP_MULTIPLY, POSSIBLE OVERWRITING PROBLEM ***");
1402 mp_set_from_integer(0, z
);
1409 if (xi
< 0 || xi
>= MP_BASE
) {
1410 mperr("*** ILLEGAL BASE B DIGIT IN CALL TO MP_MULTIPLY, POSSIBLE OVERWRITING PROBLEM ***");
1411 mp_set_from_integer(0, z
);
1416 for (i
= MP_T
+ 3; i
>= 0; i
--) {
1417 int ri
= r
.fraction
[i
] + c
;
1419 mperr("*** INTEGER OVERFLOW IN MP_MULTIPLY, B TOO LARGE ***");
1420 mp_set_from_integer(0, z
);
1424 r
.fraction
[i
] = ri
- MP_BASE
* c
;
1428 mperr("*** ILLEGAL BASE B DIGIT IN CALL TO MP_MULTIPLY, POSSIBLE OVERWRITING PROBLEM ***");
1429 mp_set_from_integer(0, z
);
1434 /* Clear complex part */
1437 memset(z
->im_fraction
, 0, sizeof(int) * MP_SIZE
);
1439 /* NORMALIZE AND ROUND RESULT */
1440 // FIXME: Use stack variable because of mp_normalize brokeness
1441 for (i
= 0; i
< MP_SIZE
; i
++)
1442 z
->fraction
[i
] = r
.fraction
[i
];
1448 mp_multiply(const MPNumber
*x
, const MPNumber
*y
, MPNumber
*z
)
1451 if (mp_is_zero(x
) || mp_is_zero(y
)) {
1452 mp_set_from_integer(0, z
);
1456 /* (a+bi)(c+di) = (ac-bd)+(ad+bc)i */
1457 if (mp_is_complex(x
) || mp_is_complex(y
)) {
1458 MPNumber real_x
, real_y
, im_x
, im_y
, t1
, t2
, real_z
, im_z
;
1460 mp_real_component(x
, &real_x
);
1461 mp_imaginary_component(x
, &im_x
);
1462 mp_real_component(y
, &real_y
);
1463 mp_imaginary_component(y
, &im_y
);
1465 mp_multiply_real(&real_x
, &real_y
, &t1
);
1466 mp_multiply_real(&im_x
, &im_y
, &t2
);
1467 mp_subtract(&t1
, &t2
, &real_z
);
1469 mp_multiply_real(&real_x
, &im_y
, &t1
);
1470 mp_multiply_real(&im_x
, &real_y
, &t2
);
1471 mp_add(&t1
, &t2
, &im_z
);
1473 mp_set_from_complex(&real_z
, &im_z
, z
);
1476 mp_multiply_real(x
, y
, z
);
1482 mp_multiply_integer_real(const MPNumber
*x
, int64_t y
, MPNumber
*z
)
1488 if (mp_is_zero(x
) || y
== 0) {
1489 mp_set_from_integer(0, z
);
1493 /* x*1 = x, x*-1 = -x */
1494 // FIXME: Why is this not working? mp_ext is using this function to do a normalization
1495 /*if (y == 1 || y == -1) {
1497 mp_invert_sign(x, z);
1499 mp_set_from_mp(x, z);
1503 /* Copy x as z may also refer to x */
1504 mp_set_from_mp(x
, &x_copy
);
1505 mp_set_from_integer(0, z
);
1509 z
->sign
= -x_copy
.sign
;
1512 z
->sign
= x_copy
.sign
;
1513 z
->exponent
= x_copy
.exponent
+ 4;
1515 /* FORM PRODUCT IN ACCUMULATOR */
1518 /* IF y*B NOT REPRESENTABLE AS AN INTEGER WE HAVE TO SIMULATE
1519 * DOUBLE-PRECISION MULTIPLICATION.
1523 if (y
>= max(MP_BASE
<< 3, 32767 / MP_BASE
)) {
1526 /* HERE J IS TOO LARGE FOR SINGLE-PRECISION MULTIPLICATION */
1528 j2
= y
- j1
* MP_BASE
;
1531 for (i
= MP_T
+ 3; i
>= 0; i
--) {
1532 int64_t c1
, c2
, is
, ix
, t
;
1535 c2
= c
- MP_BASE
* c1
;
1538 ix
= x_copy
.fraction
[i
- 4];
1542 c
= j1
* ix
+ c1
+ is
;
1543 z
->fraction
[i
] = t
- MP_BASE
* is
;
1550 for (i
= MP_T
+ 3; i
>= 4; i
--) {
1551 ri
= y
* x_copy
.fraction
[i
- 4] + c
;
1553 z
->fraction
[i
] = ri
- MP_BASE
* c
;
1556 /* CHECK FOR INTEGER OVERFLOW */
1558 mperr("*** INTEGER OVERFLOW IN mp_multiply_integer, B TOO LARGE ***");
1559 mp_set_from_integer(0, z
);
1563 /* HAVE TO TREAT FIRST FOUR WORDS OF R SEPARATELY */
1564 for (i
= 3; i
>= 0; i
--) {
1569 z
->fraction
[i
] = t
- MP_BASE
* c
;
1573 /* HAVE TO SHIFT RIGHT HERE AS CARRY OFF END */
1577 for (i
= MP_T
+ 3; i
>= 1; i
--)
1578 z
->fraction
[i
] = z
->fraction
[i
- 1];
1581 z
->fraction
[0] = t
- MP_BASE
* c
;
1586 mperr("*** INTEGER OVERFLOW IN mp_multiply_integer, B TOO LARGE ***");
1587 mp_set_from_integer(0, z
);
1593 memset(z
->im_fraction
, 0, sizeof(int) * MP_SIZE
);
1599 mp_multiply_integer(const MPNumber
*x
, int64_t y
, MPNumber
*z
)
1601 if (mp_is_complex(x
)) {
1602 MPNumber re_z
, im_z
;
1603 mp_real_component(x
, &re_z
);
1604 mp_imaginary_component(x
, &im_z
);
1605 mp_multiply_integer_real(&re_z
, y
, &re_z
);
1606 mp_multiply_integer_real(&im_z
, y
, &im_z
);
1607 mp_set_from_complex(&re_z
, &im_z
, z
);
1610 mp_multiply_integer_real(x
, y
, z
);
1615 mp_multiply_fraction(const MPNumber
*x
, int64_t numerator
, int64_t denominator
, MPNumber
*z
)
1617 if (denominator
== 0) {
1618 mperr(_("Division by zero is undefined"));
1619 mp_set_from_integer(0, z
);
1623 if (numerator
== 0) {
1624 mp_set_from_integer(0, z
);
1628 /* Reduce to lowest terms */
1629 mp_gcd(&numerator
, &denominator
);
1630 mp_divide_integer(x
, denominator
, z
);
1631 mp_multiply_integer(z
, numerator
, z
);
1636 mp_invert_sign(const MPNumber
*x
, MPNumber
*z
)
1638 mp_set_from_mp(x
, z
);
1640 z
->im_sign
= -z
->im_sign
;
1644 // FIXME: Is r->fraction large enough? It seems to be in practise but it may be MP_T+4 instead of MP_T
1645 // FIXME: There is some sort of stack corruption/use of unitialised variables here. Some functions are
1646 // using stack variables as x otherwise there are corruption errors. e.g. "Cos(45) - 1/Sqrt(2) = -0"
1647 // (try in scientific mode)
1649 mp_normalize(MPNumber
*x
)
1653 /* Find first non-zero digit */
1654 for (start_index
= 0; start_index
< MP_SIZE
&& x
->fraction
[start_index
] == 0; start_index
++);
1657 if (start_index
>= MP_SIZE
) {
1663 /* Shift left so first digit is non-zero */
1664 if (start_index
> 0) {
1667 x
->exponent
-= start_index
;
1668 for (i
= 0; (i
+ start_index
) < MP_SIZE
; i
++)
1669 x
->fraction
[i
] = x
->fraction
[i
+ start_index
];
1670 for (; i
< MP_SIZE
; i
++)
1677 mp_pwr(const MPNumber
*x
, const MPNumber
*y
, MPNumber
*z
)
1681 /* (-x)^y imaginary */
1682 /* FIXME: Make complex numbers optional */
1683 /*if (x->sign < 0) {
1684 mperr(_("The power of negative numbers is only defined for integer exponents"));
1685 mp_set_from_integer(0, z);
1690 if (mp_is_zero(x
) && y
->sign
< 0) {
1691 mperr(_("The power of zero is undefined for a negative exponent"));
1692 mp_set_from_integer(0, z
);
1697 if (mp_is_zero(y
)) {
1698 mp_set_from_integer(1, z
);
1703 mp_multiply(y
, &t
, z
);
1709 mp_reciprocal_real(const MPNumber
*x
, MPNumber
*z
)
1715 if (mp_is_zero(x
)) {
1716 mperr(_("Reciprocal of zero is undefined"));
1717 mp_set_from_integer(0, z
);
1721 /* Start by approximating value using floating point */
1722 mp_set_from_mp(x
, &t1
);
1724 mp_set_from_float(1.0f
/ mp_cast_to_float(&t1
), &t1
);
1725 t1
.exponent
-= x
->exponent
;
1731 /* t1 = t1 - (t1 * ((x * t1) - 1)) (2*t1 - t1^2*x) */
1732 mp_multiply(x
, &t1
, &t2
);
1733 mp_add_integer(&t2
, -1, &t2
);
1734 mp_multiply(&t1
, &t2
, &t2
);
1735 mp_subtract(&t1
, &t2
, &t1
);
1739 /* FOLLOWING LOOP ALMOST DOUBLES T (POSSIBLE
1740 * BECAUSE NEWTONS METHOD HAS 2ND ORDER CONVERGENCE).
1751 /* RETURN IF NEWTON ITERATION WAS CONVERGING */
1752 if (t2
.sign
!= 0 && (t1
.exponent
- t2
.exponent
) << 1 < MP_T
- it0
) {
1753 /* THE FOLLOWING MESSAGE MAY INDICATE THAT B**(T-1) IS TOO SMALL,
1754 * OR THAT THE STARTING APPROXIMATION IS NOT ACCURATE ENOUGH.
1756 mperr("*** ERROR OCCURRED IN MP_RECIPROCAL, NEWTON ITERATION NOT CONVERGING PROPERLY ***");
1759 mp_set_from_mp(&t1
, z
);
1764 mp_reciprocal(const MPNumber
*x
, MPNumber
*z
)
1766 if (mp_is_complex(x
)) {
1768 MPNumber real_x
, im_x
;
1770 mp_real_component(x
, &real_x
);
1771 mp_imaginary_component(x
, &im_x
);
1773 /* 1/(a+bi) = (a-bi)/(a+bi)(a-bi) = (a-bi)/(a²+b²) */
1774 mp_multiply(&real_x
, &real_x
, &t1
);
1775 mp_multiply(&im_x
, &im_x
, &t2
);
1776 mp_add(&t1
, &t2
, &t1
);
1777 mp_reciprocal_real(&t1
, z
);
1778 mp_conjugate(x
, &t1
);
1779 mp_multiply(&t1
, z
, z
);
1782 mp_reciprocal_real(x
, z
);
1787 mp_root_real(const MPNumber
*x
, int64_t n
, MPNumber
*z
)
1789 float approximation
;
1795 mp_set_from_mp(x
, z
);
1799 /* x^(1/0) invalid */
1801 mperr(_("Root must be non-zero"));
1802 mp_set_from_integer(0, z
);
1808 /* LOSS OF ACCURACY IF NP LARGE, SO ONLY ALLOW NP <= MAX (B, 64) */
1809 if (np
> max(MP_BASE
, 64)) {
1810 mperr("*** ABS(N) TOO LARGE IN CALL TO MP_ROOT ***");
1811 mp_set_from_integer(0, z
);
1815 /* 0^(1/n) = 0 for positive n */
1816 if (mp_is_zero(x
)) {
1817 mp_set_from_integer(0, z
);
1819 mperr(_("Negative root of zero is undefined"));
1823 // FIXME: Imaginary root
1824 if (x
->sign
< 0 && np
% 2 == 0) {
1825 mperr(_("nth root of negative number is undefined for even n"));
1826 mp_set_from_integer(0, z
);
1830 /* DIVIDE EXPONENT BY NP */
1831 ex
= x
->exponent
/ np
;
1833 /* Get initial approximation */
1834 mp_set_from_mp(x
, &t1
);
1836 approximation
= exp(((float)(np
* ex
- x
->exponent
) * log((float)MP_BASE
) -
1837 log((fabs(mp_cast_to_float(&t1
))))) / (float)np
);
1838 mp_set_from_float(approximation
, &t1
);
1842 /* MAIN ITERATION LOOP */
1847 /* t1 = t1 - ((t1 * ((x * t1^np) - 1)) / np) */
1848 mp_xpowy_integer(&t1
, np
, &t2
);
1849 mp_multiply(x
, &t2
, &t2
);
1850 mp_add_integer(&t2
, -1, &t2
);
1851 mp_multiply(&t1
, &t2
, &t2
);
1852 mp_divide_integer(&t2
, np
, &t2
);
1853 mp_subtract(&t1
, &t2
, &t1
);
1855 /* FOLLOWING LOOP ALMOST DOUBLES T (POSSIBLE BECAUSE
1856 * NEWTONS METHOD HAS 2ND ORDER CONVERGENCE).
1870 /* NOW R(I2) IS X**(-1/NP)
1871 * CHECK THAT NEWTON ITERATION WAS CONVERGING
1873 if (t2
.sign
!= 0 && (t1
.exponent
- t2
.exponent
) << 1 < MP_T
- it0
) {
1874 /* THE FOLLOWING MESSAGE MAY INDICATE THAT B**(T-1) IS TOO SMALL,
1875 * OR THAT THE INITIAL APPROXIMATION OBTAINED USING ALOG AND EXP
1876 * IS NOT ACCURATE ENOUGH.
1878 mperr("*** ERROR OCCURRED IN MP_ROOT, NEWTON ITERATION NOT CONVERGING PROPERLY ***");
1882 mp_xpowy_integer(&t1
, n
- 1, &t1
);
1883 mp_multiply(x
, &t1
, z
);
1887 mp_set_from_mp(&t1
, z
);
1892 mp_root(const MPNumber
*x
, int64_t n
, MPNumber
*z
)
1894 if (!mp_is_complex(x
) && mp_is_negative(x
) && n
% 2 == 1) {
1896 mp_root_real(z
, n
, z
);
1897 mp_invert_sign(z
, z
);
1899 else if (mp_is_complex(x
) || mp_is_negative(x
)) {
1903 mp_arg(x
, MP_RADIANS
, &theta
);
1905 mp_root_real(&r
, n
, &r
);
1906 mp_divide_integer(&theta
, n
, &theta
);
1907 mp_set_from_polar(&r
, MP_RADIANS
, &theta
, z
);
1910 mp_root_real(x
, n
, z
);
1915 mp_sqrt(const MPNumber
*x
, MPNumber
*z
)
1918 mp_set_from_integer(0, z
);
1919 /* FIXME: Make complex numbers optional */
1920 /*else if (x->sign < 0) {
1921 mperr(_("Square root is undefined for negative values"));
1922 mp_set_from_integer(0, z);
1928 mp_multiply(x
, &t
, z
);
1929 mp_ext(t
.fraction
[0], z
->fraction
[0], z
);
1935 mp_factorial(const MPNumber
*x
, MPNumber
*z
)
1940 if (mp_is_zero(x
)) {
1941 mp_set_from_integer(1, z
);
1944 if (!mp_is_natural(x
)) {
1945 /* Translators: Error displayed when attempted take the factorial of a fractional number */
1946 mperr(_("Factorial is only defined for natural numbers"));
1947 mp_set_from_integer(0, z
);
1951 /* Convert to integer - if couldn't be converted then the factorial would be too big anyway */
1952 value
= mp_cast_to_int(x
);
1953 mp_set_from_mp(x
, z
);
1954 for (i
= 2; i
< value
; i
++)
1955 mp_multiply_integer(z
, i
, z
);
1960 mp_modulus_divide(const MPNumber
*x
, const MPNumber
*y
, MPNumber
*z
)
1964 if (!mp_is_integer(x
) || !mp_is_integer(y
)) {
1965 /* Translators: Error displayed when attemping to do a modulus division on non-integer numbers */
1966 mperr(_("Modulus division is only defined for integers"));
1967 mp_set_from_integer(0, z
);
1970 mp_divide(x
, y
, &t1
);
1972 mp_multiply(&t1
, y
, &t2
);
1973 mp_subtract(x
, &t2
, z
);
1975 mp_set_from_integer(0, &t1
);
1976 if ((mp_is_less_than(y
, &t1
) && mp_is_greater_than(z
, &t1
)) || mp_is_less_than(z
, &t1
))
1982 mp_xpowy(const MPNumber
*x
, const MPNumber
*y
, MPNumber
*z
)
1984 if (mp_is_integer(y
)) {
1985 mp_xpowy_integer(x
, mp_cast_to_int(y
), z
);
1987 MPNumber reciprocal
;
1988 mp_reciprocal(y
, &reciprocal
);
1989 if (mp_is_integer(&reciprocal
))
1990 mp_root(x
, mp_cast_to_int(&reciprocal
), z
);
1998 mp_xpowy_integer(const MPNumber
*x
, int64_t n
, MPNumber
*z
)
2004 if (mp_is_zero(x
) && n
< 0) {
2005 /* Translators: Error displayed when attempted to raise 0 to a negative exponent */
2006 mperr(_("The power of zero is undefined for a negative exponent"));
2007 mp_set_from_integer(0, z
);
2013 mp_set_from_integer(1, z
);
2018 if (mp_is_zero(x
)) {
2019 mp_set_from_integer(0, z
);
2025 mp_set_from_mp(x
, z
);
2030 mp_reciprocal(x
, &t
);
2034 mp_set_from_mp(x
, &t
);
2036 /* Multply x n times */
2037 // FIXME: Can do mp_multiply(z, z, z) until close to answer (each call doubles number of multiples) */
2038 mp_set_from_integer(1, z
);
2039 for (i
= 0; i
< n
; i
++)
2040 mp_multiply(z
, &t
, z
);
2045 mp_factorize(const MPNumber
*x
)
2048 MPNumber
*factor
= g_slice_alloc0(sizeof(MPNumber
));
2049 MPNumber value
, tmp
, divisor
, root
;
2053 if (mp_is_zero(&value
)) {
2054 mp_set_from_mp(&value
, factor
);
2055 list
= g_list_append(list
, factor
);
2059 mp_set_from_integer(1, &tmp
);
2060 if (mp_is_equal(&value
, &tmp
)) {
2061 mp_set_from_mp(x
, factor
);
2062 list
= g_list_append(list
, factor
);
2066 mp_set_from_integer(2, &divisor
);
2068 mp_divide(&value
, &divisor
, &tmp
);
2069 if (mp_is_integer(&tmp
)) {
2071 mp_set_from_mp(&divisor
, factor
);
2072 list
= g_list_append(list
, factor
);
2073 factor
= g_slice_alloc0(sizeof(MPNumber
));
2079 mp_set_from_integer(3, &divisor
);
2080 mp_sqrt(&value
, &root
);
2081 while (mp_is_less_equal(&divisor
, &root
)) {
2082 mp_divide(&value
, &divisor
, &tmp
);
2083 if (mp_is_integer(&tmp
)) {
2085 mp_sqrt(&value
, &root
);
2086 mp_set_from_mp(&divisor
, factor
);
2087 list
= g_list_append(list
, factor
);
2088 factor
= g_slice_alloc0(sizeof(MPNumber
));
2090 mp_add_integer(&divisor
, 2, &tmp
);
2095 mp_set_from_integer(1, &tmp
);
2096 if (mp_is_greater_than(&value
, &tmp
)) {
2097 mp_set_from_mp(&value
, factor
);
2098 list
= g_list_append(list
, factor
);
2100 g_slice_free(MPNumber
, factor
);
2103 if (mp_is_negative(x
)) {
2104 mp_invert_sign(list
->data
, list
->data
);