[gcalctool] Rename MP functions and document mp.h
- From: Robert Ancell <rancell src gnome org>
- To: svn-commits-list gnome org
- Subject: [gcalctool] Rename MP functions and document mp.h
- Date: Thu, 7 May 2009 21:59:01 -0400 (EDT)
commit c35c036c1b3c686a3bc55f41c0968fbe03bd0896
Author: Robert Ancell <robert ancell gmail com>
Date: Fri May 8 11:58:26 2009 +1000
Rename MP functions and document mp.h
Make binary operations with variable word size thread safe
---
gcalctool/calctool.c | 18 ++--
gcalctool/ce_parser.y | 28 ++--
gcalctool/display.c | 22 ++--
gcalctool/financial.c | 66 +++++-----
gcalctool/functions.c | 3 +-
gcalctool/mp-binary.c | 78 +++++------
gcalctool/mp-convert.c | 58 ++++----
gcalctool/mp-internal.h | 4 +-
gcalctool/mp-trigonometric.c | 102 +++++++-------
gcalctool/mp.c | 275 ++++++++++++++++++-------------------
gcalctool/mp.h | 312 +++++++++++++++++++++++++++++-------------
gcalctool/register.c | 2 +-
gcalctool/unittest.c | 60 ++++++--
13 files changed, 581 insertions(+), 447 deletions(-)
diff --git a/gcalctool/calctool.c b/gcalctool/calctool.c
index d70dc4f..7a712d4 100644
--- a/gcalctool/calctool.c
+++ b/gcalctool/calctool.c
@@ -53,14 +53,14 @@ to_rad(const MPNumber *s1, MPNumber *t1)
if (v->ttype == DEG) {
mp_get_pi(&MP1);
- mpmul(s1, &MP1, &MP2);
+ mp_multiply(s1, &MP1, &MP2);
mp_set_from_integer(180, &MP1);
- mpdiv(&MP2, &MP1, t1);
+ mp_divide(&MP2, &MP1, t1);
} else if (v->ttype == GRAD) {
mp_get_pi(&MP1);
- mpmul(s1, &MP1, &MP2);
+ mp_multiply(s1, &MP1, &MP2);
mp_set_from_integer(200, &MP1);
- mpdiv(&MP2, &MP1, t1);
+ mp_divide(&MP2, &MP1, t1);
} else {
mp_set_from_mp(s1, t1);
}
@@ -75,9 +75,9 @@ do_trig_typeconv(TrigType ttype, const MPNumber *s1, MPNumber *t1)
case DEG:
mp_set_from_integer(180, &MP1);
- mpmul(s1, &MP1, &MP2);
+ mp_multiply(s1, &MP1, &MP2);
mp_get_pi(&MP1);
- mpdiv(&MP2, &MP1, t1);
+ mp_divide(&MP2, &MP1, t1);
break;
case RAD:
@@ -86,9 +86,9 @@ do_trig_typeconv(TrigType ttype, const MPNumber *s1, MPNumber *t1)
case GRAD:
mp_set_from_integer(200, &MP1);
- mpmul(s1, &MP1, &MP2);
+ mp_multiply(s1, &MP1, &MP2);
mp_get_pi(&MP1);
- mpdiv(&MP2, &MP1, t1);
+ mp_divide(&MP2, &MP1, t1);
break;
default:
@@ -147,7 +147,7 @@ solve(const char *equation)
exit(1);
}
else {
- mp_cast_to_string(result_str, MAXLINE, &result, basevals[v->base], 9);
+ mp_cast_to_string(&result, basevals[v->base], 9, result_str, MAXLINE);
printf("%s\n", result_str);
exit(0);
}
diff --git a/gcalctool/ce_parser.y b/gcalctool/ce_parser.y
index 1e41c18..c01d63e 100644
--- a/gcalctool/ce_parser.y
+++ b/gcalctool/ce_parser.y
@@ -167,7 +167,7 @@ exp:
if (!mp_is_natural(&$1) || !mp_is_natural(&$3)) {
parser_state.error = -PARSER_ERR_BITWISEOP;
}
- mp_xnor(&$1, &$3, &$$);
+ mp_xnor(&$1, &$3, v->wordlen, &$$);
}
| exp tXOR exp {
if (!mp_is_natural(&$1) || !mp_is_natural(&$3)) {
@@ -181,18 +181,18 @@ exp:
term:
number {cp(&$1, &$$);}
| rcl {cp(&$1, &$$);}
-| term '/' term {mpdiv(&$1, &$3, &$$);}
-| term '*' term {mpmul(&$1, &$3, &$$);}
+| term '/' term {mp_divide(&$1, &$3, &$$);}
+| term '*' term {mp_multiply(&$1, &$3, &$$);}
| 'e' '^' term {mp_epowy(&$3, &$$);}
| term '!' {mp_factorial(&$1, &$$);}
| term '%' {mp_percent(&$1, &$$);}
| '~' term %prec LNEG {
if (!mp_is_natural(&$2)) {
parser_state.error = -PARSER_ERR_BITWISEOP;
- } else if (!mp_is_overflow(&$2)) {
+ } else if (!mp_is_overflow(&$2, v->wordlen)) {
parser_state.error = -PARSER_ERR_OVERFLOW;
}
- mp_not(&$2, &$$);
+ mp_not(&$2, v->wordlen, &$$);
}
| '-' term %prec NEG {mp_invert_sign(&$2, &$$);}
| '+' term %prec POS {cp(&$2, &$$);}
@@ -216,11 +216,11 @@ func:
tLOG10 term %prec HIGH {mp_logarithm(10, &$2, &$$);}
| tLOG2 term %prec HIGH {mp_logarithm(2, &$2, &$$);}
| tSQRT term %prec HIGH {mp_sqrt(&$2, &$$);}
-| tLN term %prec HIGH {mpln(&$2, &$$);}
+| tLN term %prec HIGH {mp_ln(&$2, &$$);}
| tRAND %prec HIGH {mp_set_from_random(&$$);}
| tABS term %prec HIGH {mp_abs(&$2, &$$);}
-| tFRAC term %prec HIGH {mpcmf(&$2, &$$);}
-| tINT term %prec HIGH {mpcmim(&$2, &$$);}
+| tFRAC term %prec HIGH {mp_fractional_component(&$2, &$$);}
+| tINT term %prec HIGH {mp_integer_component(&$2, &$$);}
| tCHS term %prec HIGH {mp_invert_sign(&$2, &$$);}
| tSIN term %prec HIGH {to_rad(&$2, &$2); mp_sin(&$2, &$$);}
@@ -236,22 +236,22 @@ func:
| tACOSH term %prec HIGH {mp_acosh(&$2, &$$);}
| tATANH term %prec HIGH {mp_atanh(&$2, &$$);}
-| tTRUNC term %prec HIGH {mp_mask(&$2, &$$);}
+| tTRUNC term %prec HIGH {mp_mask(&$2, v->wordlen, &$$);}
| t1S term %prec HIGH {
if (!mp_is_natural(&$2)) {
parser_state.error = -PARSER_ERR_BITWISEOP;
- } else if (!mp_is_overflow(&$2)) {
+ } else if (!mp_is_overflow(&$2, v->wordlen)) {
parser_state.error = -PARSER_ERR_OVERFLOW;
}
- mp_1s_complement(&$2, &$$);
+ mp_1s_complement(&$2, v->wordlen, &$$);
}
| t2S term %prec HIGH {
if (!mp_is_natural(&$2)) {
parser_state.error = -PARSER_ERR_BITWISEOP;
- } else if (!mp_is_overflow(&$2)) {
+ } else if (!mp_is_overflow(&$2, v->wordlen)) {
parser_state.error = -PARSER_ERR_OVERFLOW;
}
- mp_2s_complement(&$2, &$$);
+ mp_2s_complement(&$2, v->wordlen, &$$);
}
;
@@ -280,6 +280,6 @@ int ceerror(char *s)
| '(' lexp ')' {cp(&$2, &$$);}
-| term term {mpmul(&$1, &$2, &$$);}
+| term term {mp_multiply(&$1, &$2, &$$);}
#endif
diff --git a/gcalctool/display.c b/gcalctool/display.c
index 951702a..108ea96 100644
--- a/gcalctool/display.c
+++ b/gcalctool/display.c
@@ -660,39 +660,39 @@ make_eng_sci(GCDisplay *display, char *target, int target_len, const MPNumber *M
mp_set_from_mp(&MPval, &MPmant);
mp_set_from_integer(basevals[base], &MP1base);
- mppwr(&MP1base, 3, &MP3base);
+ mp_pwr_integer(&MP1base, 3, &MP3base);
- mppwr(&MP1base, 10, &MP10base);
+ mp_pwr_integer(&MP1base, 10, &MP10base);
mp_set_from_integer(1, &MP1);
- mpdiv(&MP1, &MP10base, &MPatmp);
+ mp_divide(&MP1, &MP10base, &MPatmp);
mp_set_from_integer(0, &MP1);
if (!mp_is_equal(&MPmant, &MP1)) {
while (!eng && mp_is_greater_equal(&MPmant, &MP10base)) {
exp += 10;
- mpmul(&MPmant, &MPatmp, &MPmant);
+ mp_multiply(&MPmant, &MPatmp, &MPmant);
}
while ((!eng && mp_is_greater_equal(&MPmant, &MP1base)) ||
(eng && (mp_is_greater_equal(&MPmant, &MP3base) || exp % 3 != 0))) {
exp += 1;
- mpdiv(&MPmant, &MP1base, &MPmant);
+ mp_divide(&MPmant, &MP1base, &MPmant);
}
while (!eng && mp_is_less_than(&MPmant, &MPatmp)) {
exp -= 10;
- mpmul(&MPmant, &MP10base, &MPmant);
+ mp_multiply(&MPmant, &MP10base, &MPmant);
}
mp_set_from_integer(1, &MP1);
while (mp_is_less_than(&MPmant, &MP1) || (eng && exp % 3 != 0)) {
exp -= 1;
- mpmul(&MPmant, &MP1base, &MPmant);
+ mp_multiply(&MPmant, &MP1base, &MPmant);
}
}
- mp_cast_to_string(fixed, MAX_DIGITS, &MPmant, basevals[base], v->accuracy);
+ mp_cast_to_string(&MPmant, basevals[base], v->accuracy, fixed, MAX_DIGITS);
len = strlen(fixed);
for (i = 0; i < len; i++) {
*optr++ = fixed[i];
@@ -711,7 +711,7 @@ make_eng_sci(GCDisplay *display, char *target, int target_len, const MPNumber *M
mp_add_integer(&MP1, exp, &MPval);
mp_set_from_integer(1, &MP1);
for (ddig = 0; mp_is_greater_equal(&MPval, &MP1); ddig++) {
- mpdiv(&MPval, &MP1base, &MPval);
+ mp_divide(&MPval, &MP1base, &MPval);
}
if (ddig == 0) {
@@ -719,7 +719,7 @@ make_eng_sci(GCDisplay *display, char *target, int target_len, const MPNumber *M
}
while (ddig-- > 0) {
- mpmul(&MPval, &MP1base, &MPval);
+ mp_multiply(&MPval, &MP1base, &MPval);
dval = mp_cast_to_int(&MPval);
*optr++ = digits[dval];
dval = -dval;
@@ -763,6 +763,6 @@ display_make_number(GCDisplay *display, char *target, int target_len, const MPNu
(display->format == FIX && val != 0.0 && (val > max_fix[base]))) {
make_eng_sci(display, target, target_len, MPnumber, base);
} else {
- mp_cast_to_string(target, target_len, MPnumber, basevals[base], v->accuracy);
+ mp_cast_to_string(MPnumber, basevals[base], v->accuracy, target, target_len);
}
}
diff --git a/gcalctool/financial.c b/gcalctool/financial.c
index 2f2d493..17c6689 100644
--- a/gcalctool/financial.c
+++ b/gcalctool/financial.c
@@ -36,11 +36,11 @@ calc_ctrm(MPNumber *t, MPNumber *pint, MPNumber *fv, MPNumber *pv)
*/
MPNumber MP1, MP2, MP3, MP4;
- mpdiv(fv, pv, &MP1);
- mpln(&MP1, &MP2);
+ mp_divide(fv, pv, &MP1);
+ mp_ln(&MP1, &MP2);
mp_add_integer(pint, 1, &MP3);
- mpln(&MP3, &MP4);
- mpdiv(&MP2, &MP4, t);
+ mp_ln(&MP3, &MP4);
+ mp_divide(&MP2, &MP4, t);
}
@@ -70,8 +70,8 @@ calc_ddb(MPNumber *t, MPNumber *cost, MPNumber *life, MPNumber *period)
len = mp_cast_to_int(period);
for (i = 0; i < len; i++) {
mp_subtract(cost, &MPbv, &MP1);
- mpmuli(&MP1, 2, &MP2);
- mpdiv(&MP2, life, t);
+ mp_multiply_integer(&MP1, 2, &MP2);
+ mp_divide(&MP2, life, t);
mp_set_from_mp(&MPbv, &MP1);
mp_add(&MP1, t, &MPbv); /* TODO: why result is MPbv, for next loop? */
}
@@ -98,10 +98,10 @@ calc_fv(MPNumber *t, MPNumber *pmt, MPNumber *pint, MPNumber *n)
MPNumber MP1, MP2, MP3, MP4;
mp_add_integer(pint, 1, &MP1);
- mppwr2(&MP1, n, &MP2);
+ mp_pwr(&MP1, n, &MP2);
mp_add_integer(&MP2, -1, &MP3);
- mpmul(pmt, &MP3, &MP4);
- mpdiv(&MP4, pint, t);
+ mp_multiply(pmt, &MP3, &MP4);
+ mp_divide(&MP4, pint, t);
}
@@ -119,7 +119,7 @@ calc_gpm(MPNumber *t, MPNumber *cost, MPNumber *margin)
mp_set_from_integer(1, &MP1);
mp_subtract(&MP1, margin, &MP2);
- mpdiv(cost, &MP2, t);
+ mp_divide(cost, &MP2, t);
}
@@ -137,12 +137,12 @@ calc_pmt(MPNumber *t, MPNumber *prin, MPNumber *pint, MPNumber *n)
MPNumber MP1, MP2, MP3, MP4;
mp_add_integer(pint, 1, &MP1);
- mpmuli(n, -1, &MP2);
- mppwr2(&MP1, &MP2, &MP3);
- mpmuli(&MP3, -1, &MP4);
+ mp_multiply_integer(n, -1, &MP2);
+ mp_pwr(&MP1, &MP2, &MP3);
+ mp_multiply_integer(&MP3, -1, &MP4);
mp_add_integer(&MP4, 1, &MP1);
- mpdiv(pint, &MP1, &MP2);
- mpmul(prin, &MP2, t);
+ mp_divide(pint, &MP1, &MP2);
+ mp_multiply(prin, &MP2, t);
}
@@ -160,12 +160,12 @@ calc_pv(MPNumber *t, MPNumber *pmt, MPNumber *pint, MPNumber *n)
MPNumber MP1, MP2, MP3, MP4;
mp_add_integer(pint, 1, &MP1);
- mpmuli(n, -1, &MP2);
- mppwr2(&MP1, &MP2, &MP3);
- mpmuli(&MP3, -1, &MP4);
+ mp_multiply_integer(n, -1, &MP2);
+ mp_pwr(&MP1, &MP2, &MP3);
+ mp_multiply_integer(&MP3, -1, &MP4);
mp_add_integer(&MP4, 1, &MP1);
- mpdiv(&MP1, pint, &MP2);
- mpmul(pmt, &MP2, t);
+ mp_divide(&MP1, pint, &MP2);
+ mp_multiply(pmt, &MP2, t);
}
@@ -182,10 +182,10 @@ calc_rate(MPNumber *t, MPNumber *fv, MPNumber *pv, MPNumber *n)
MPNumber MP1, MP2, MP3, MP4;
- mpdiv(fv, pv, &MP1);
+ mp_divide(fv, pv, &MP1);
mp_set_from_integer(1, &MP2);
- mpdiv(&MP2, n, &MP3);
- mppwr2(&MP1, &MP3, &MP4);
+ mp_divide(&MP2, n, &MP3);
+ mp_pwr(&MP1, &MP3, &MP4);
mp_add_integer(&MP4, -1, t);
}
@@ -203,7 +203,7 @@ calc_sln(MPNumber *t, MPNumber *cost, MPNumber *salvage, MPNumber *life)
MPNumber MP1;
mp_subtract(cost, salvage, &MP1);
- mpdiv(&MP1, life, t);
+ mp_divide(&MP1, life, t);
}
@@ -225,12 +225,12 @@ calc_syd(MPNumber *t, MPNumber *cost, MPNumber *salvage, MPNumber *life, MPNumbe
mp_subtract(life, period, &MP2);
mp_add_integer(&MP2, 1, &MP3);
mp_add_integer(life, 1, &MP2);
- mpmul(life, &MP2, &MP4);
+ mp_multiply(life, &MP2, &MP4);
mp_set_from_integer(2, &MP2);
- mpdiv(&MP4, &MP2, &MP1);
- mpdiv(&MP3, &MP1, &MP2);
+ mp_divide(&MP4, &MP2, &MP1);
+ mp_divide(&MP3, &MP1, &MP2);
mp_subtract(cost, salvage, &MP1);
- mpmul(&MP1, &MP2, t);
+ mp_multiply(&MP1, &MP2, t);
}
@@ -248,12 +248,12 @@ calc_term(MPNumber *t, MPNumber *pmt, MPNumber *fv, MPNumber *pint)
MPNumber MP1, MP2, MP3, MP4;
mp_add_integer(pint, 1, &MP1);
- mpln(&MP1, &MP2);
- mpmul(fv, pint, &MP1);
- mpdiv(&MP1, pmt, &MP3);
+ mp_ln(&MP1, &MP2);
+ mp_multiply(fv, pint, &MP1);
+ mp_divide(&MP1, pmt, &MP3);
mp_add_integer(&MP3, 1, &MP4);
- mpln(&MP4, &MP1);
- mpdiv(&MP1, &MP2, t);
+ mp_ln(&MP4, &MP1);
+ mp_divide(&MP1, &MP2, t);
}
void
diff --git a/gcalctool/functions.c b/gcalctool/functions.c
index 01e39cd..e889aa4 100644
--- a/gcalctool/functions.c
+++ b/gcalctool/functions.c
@@ -274,7 +274,7 @@ do_shift(int count)
"gtk-dialog-error");
}
else {
- mp_shift(&MPval, display_get_answer(&v->display), count);
+ mp_shift(&MPval, count, display_get_answer(&v->display));
display_set_answer(&v->display);
}
}
@@ -330,7 +330,6 @@ do_exchange(int index)
static void
do_wordlen(int len)
{
- mp_set_wordlen(len);
v->wordlen = len;
set_int_resource(R_WORDLEN, len);
}
diff --git a/gcalctool/mp-binary.c b/gcalctool/mp-binary.c
index 6aacb24..6e00f4e 100644
--- a/gcalctool/mp-binary.c
+++ b/gcalctool/mp-binary.c
@@ -2,7 +2,6 @@
#include "calctool.h" // FIXME
static char digits[] = "0123456789ABCDEF";
-static int current_wordlen = 64;
static int hex_to_int(char digit)
{
@@ -17,13 +16,13 @@ static int hex_to_int(char digit)
static void
-mp_bitwise(const MPNumber *s1, const MPNumber *s2, int (*bitwise_operator)(int, int), MPNumber *t, int wordlen)
+mp_bitwise(const MPNumber *x, const MPNumber *y, int (*bitwise_operator)(int, int), MPNumber *z, int wordlen)
{
char text1[MAX_DIGITS], text2[MAX_DIGITS], text_out[MAX_DIGITS];
int offset1, offset2, offset_out;
- mp_cast_to_string(text1, MAX_DIGITS, s1, 16, 0);
- mp_cast_to_string(text2, MAX_DIGITS, s2, 16, 0);
+ mp_cast_to_string(x, 16, 0, text1, MAX_DIGITS);
+ mp_cast_to_string(y, 16, 0, text2, MAX_DIGITS);
offset1 = strlen(text1) - 1;
offset2 = strlen(text2) - 1;
offset_out = wordlen / 4 - 1;
@@ -50,7 +49,7 @@ mp_bitwise(const MPNumber *s1, const MPNumber *s2, int (*bitwise_operator)(int,
text_out[offset_out] = digits[bitwise_operator(v1, v2)];
}
- mp_set_from_string(text_out, 16, t);
+ mp_set_from_string(text_out, 16, z);
}
@@ -62,106 +61,99 @@ static int mp_bitwise_not(int v1, int dummy) { return v1 ^ 0xF; }
int
-mp_is_overflow (const MPNumber *s1)
+mp_is_overflow (const MPNumber *x, int wordlen)
{
MPNumber tmp1, tmp2;
mp_set_from_integer(2, &tmp1);
- mppwr(&tmp1, current_wordlen, &tmp2);
- return mp_is_greater_than (&tmp2, s1);
+ mp_pwr_integer(&tmp1, wordlen, &tmp2);
+ return mp_is_greater_than (&tmp2, x);
}
void
-mp_set_wordlen (int len)
+mp_and(const MPNumber *x, const MPNumber *y, MPNumber *z)
{
- current_wordlen = len;
+ mp_bitwise(x, y, mp_bitwise_and, z, 0);
}
void
-mp_and(const MPNumber *s1, const MPNumber *s2, MPNumber *t)
+mp_or(const MPNumber *x, const MPNumber *y, MPNumber *z)
{
- mp_bitwise(s1, s2, mp_bitwise_and, t, 0);
+ mp_bitwise(x, y, mp_bitwise_or, z, 0);
}
void
-mp_or(const MPNumber *s1, const MPNumber *s2, MPNumber *t)
+mp_xor(const MPNumber *x, const MPNumber *y, MPNumber *z)
{
- mp_bitwise(s1, s2, mp_bitwise_or, t, 0);
+ mp_bitwise(x, y, mp_bitwise_xor, z, 0);
}
void
-mp_xor(const MPNumber *s1, const MPNumber *s2, MPNumber *t)
+mp_xnor(const MPNumber *x, const MPNumber *y, int wordlen, MPNumber *z)
{
- mp_bitwise(s1, s2, mp_bitwise_xor, t, 0);
+ mp_bitwise(x, y, mp_bitwise_xnor, z, wordlen);
}
void
-mp_xnor(const MPNumber *s1, const MPNumber *s2, MPNumber *t)
-{
- mp_bitwise(s1, s2, mp_bitwise_xnor, t, 0);
-}
-
-
-void
-mp_not(const MPNumber *s1, MPNumber *t)
+mp_not(const MPNumber *x, int wordlen, MPNumber *z)
{
MPNumber temp;
mp_set_from_integer(0, &temp);
- mp_bitwise(s1, &temp, mp_bitwise_not, t, current_wordlen);
+ mp_bitwise(x, &temp, mp_bitwise_not, z, wordlen);
}
void
-mp_mask(const MPNumber *s1, MPNumber *t1)
+mp_mask(const MPNumber *x, int wordlen, MPNumber *z)
{
char text[MAX_DIGITS];
size_t len, offset;
/* Convert to a hexadecimal string and use last characters */
- mp_cast_to_string(text, MAX_DIGITS, s1, 16, 0);
+ mp_cast_to_string(x, 16, 0, text, MAX_DIGITS);
len = strlen(text);
- offset = current_wordlen / 4;
+ offset = wordlen / 4;
offset = len > offset ? len - offset: 0;
- mp_set_from_string(text + offset, 16, t1);
+ mp_set_from_string(text + offset, 16, z);
}
void
-mp_shift(MPNumber *s, MPNumber *t, int times)
+mp_shift(const MPNumber *x, int count, MPNumber *z)
{
int i, multiplier = 1;
- if (times >= 0) {
- for (i = 0; i < times; i++)
+ if (count >= 0) {
+ for (i = 0; i < count; i++)
multiplier *= 2;
- mpmuli(s, multiplier, t);
+ mp_multiply_integer(x, multiplier, z);
}
else {
MPNumber temp;
- for (i = 0; i < -times; i++)
+ for (i = 0; i < -count; i++)
multiplier *= 2;
- mpdivi(s, multiplier, &temp);
- mpcmim(&temp, t);
+ mp_divide_integer(x, multiplier, &temp);
+ mp_integer_component(&temp, z);
}
}
void
-mp_1s_complement(const MPNumber *s, MPNumber *t)
+mp_1s_complement(const MPNumber *x, int wordlen, MPNumber *z)
{
- MPNumber temp;
- mp_set_from_integer(0, &temp);
- mp_bitwise(s, &temp, mp_bitwise_xnor, t, current_wordlen);
+ MPNumber t;
+ mp_set_from_integer(0, &t);
+ mp_bitwise(x, &t, mp_bitwise_xnor, z, wordlen);
}
void
-mp_2s_complement(const MPNumber *s, MPNumber *t)
+mp_2s_complement(const MPNumber *x, int wordlen, MPNumber *z)
{
- mp_1s_complement (s, t);
- mp_add_integer (t, 1, t);
+ mp_1s_complement (x, wordlen, z);
+ mp_add_integer (z, 1, z);
}
diff --git a/gcalctool/mp-convert.c b/gcalctool/mp-convert.c
index fac4c09..c9d573f 100644
--- a/gcalctool/mp-convert.c
+++ b/gcalctool/mp-convert.c
@@ -117,7 +117,7 @@ mp_set_from_float(float rx, MPNumber *z)
tp <<= 4;
if (tp <= ib && tp != MP.b && i < k)
continue;
- mpdivi(z, tp, z);
+ mp_divide_integer(z, tp, z);
tp = 1;
}
} else if (ie > 0) {
@@ -125,7 +125,7 @@ mp_set_from_float(float rx, MPNumber *z)
tp <<= 4;
if (tp <= ib && tp != MP.b && i < ie)
continue;
- mpmuli(z, tp, z);
+ mp_multiply_integer(z, tp, z);
tp = 1;
}
}
@@ -134,9 +134,9 @@ mp_set_from_float(float rx, MPNumber *z)
}
void
-mp_set_from_random(MPNumber *t)
+mp_set_from_random(MPNumber *z)
{
- mp_set_from_double(drand48(), t);
+ mp_set_from_double(drand48(), z);
}
/* CONVERTS DOUBLE-PRECISION NUMBER DX TO MULTIPLE-PRECISION Z.
@@ -202,7 +202,7 @@ mp_set_from_double(double dx, MPNumber *z)
tp <<= 4;
if (tp <= ib && tp != MP.b && i < k)
continue;
- mpdivi(z, tp, z);
+ mp_divide_integer(z, tp, z);
tp = 1;
}
} else if (ie > 0) {
@@ -210,7 +210,7 @@ mp_set_from_double(double dx, MPNumber *z)
tp <<= 4;
if (tp <= ib && tp != MP.b && i < ie)
continue;
- mpmuli(z, tp, z);
+ mp_multiply_integer(z, tp, z);
tp = 1;
}
}
@@ -270,12 +270,12 @@ mp_set_from_fraction(int i, int j, MPNumber *q)
}
mp_set_from_integer(i, q);
- if (j != 1) mpdivi(q, j, q);
+ if (j != 1) mp_divide_integer(q, j, q);
}
/* CONVERTS MULTIPLE-PRECISION X TO INTEGER, AND
* RETURNS RESULT.
- * ASSUMING THAT X NOT TOO LARGE (ELSE USE MPCMIM).
+ * ASSUMING THAT X NOT TOO LARGE (ELSE USE MP_INTEGER_COMPONENT)
* X IS TRUNCATED TOWARDS ZERO.
* IF INT(X)IS TOO LARGE TO BE REPRESENTED AS A SINGLE-
* PRECISION INTEGER, IZ IS RETURNED AS ZERO. THE USER
@@ -485,14 +485,14 @@ mp_cast_to_double(const MPNumber *x)
* maximum number of digits specified.
*/
void
-mp_cast_to_string(char *target, int target_len, const MPNumber *MPnumber, int base, int accuracy)
+mp_cast_to_string(const MPNumber *MPnumber, int base, int accuracy, char *buffer, int buffer_length)
{
static char digits[] = "0123456789ABCDEF";
char *optr, *start, *end, *stopper, *last_non_zero;
MPNumber number, integer_component, fractional_component, MPbase, temp;
- optr = target;
- stopper = target + target_len - 1;
+ optr = buffer;
+ stopper = buffer + buffer_length - 1;
/* Insert sign */
if (mp_is_negative(MPnumber)) {
@@ -504,14 +504,14 @@ mp_cast_to_string(char *target, int target_len, const MPNumber *MPnumber, int ba
/* Add rounding factor */
mp_set_from_integer(base, &MPbase);
- mppwr(&MPbase, -(accuracy+1), &temp);
- mpmuli(&temp, base, &temp);
- mpdivi(&temp, 2, &temp);
+ mp_pwr_integer(&MPbase, -(accuracy+1), &temp);
+ mp_multiply_integer(&temp, base, &temp);
+ mp_divide_integer(&temp, 2, &temp);
mp_add(&number, &temp, &number);
/* Split into integer and fractional component */
- mpcmim(&number, &integer_component);
- mpcmf(&number, &fractional_component);
+ mp_integer_component(&number, &integer_component);
+ mp_fractional_component(&number, &fractional_component);
/* Write out the integer component least significant digit to most */
start = optr;
@@ -519,12 +519,12 @@ mp_cast_to_string(char *target, int target_len, const MPNumber *MPnumber, int ba
do {
MPNumber t, t2, t3;
- mpdivi(&temp, base, &t);
- mpcmim(&t, &t);
- mpmuli(&t, base, &t2);
+ mp_divide_integer(&temp, base, &t);
+ mp_integer_component(&t, &t);
+ mp_multiply_integer(&t, base, &t2);
mp_subtract(&temp, &t2, &t3);
- mpcmim(&t3, &t3);
+ mp_integer_component(&t3, &t3);
if (optr == stopper) {
mperr(_("Number too big to represent"));
@@ -562,8 +562,8 @@ mp_cast_to_string(char *target, int target_len, const MPNumber *MPnumber, int ba
int d;
MPNumber digit;
- mpmuli(&temp, base, &temp);
- mpcmim(&temp, &digit);
+ mp_multiply_integer(&temp, base, &temp);
+ mp_integer_component(&temp, &digit);
d = mp_cast_to_int(&digit);
if (optr == stopper) {
@@ -631,7 +631,7 @@ mp_set_from_string(const char *str, int base, MPNumber *MPval)
/* Convert integer part */
mp_set_from_integer(0, MPval);
while ((inum = char_val(*optr, base)) >= 0) {
- mpmuli(MPval, base, MPval);
+ mp_multiply_integer(MPval, base, MPval);
mp_add_integer(MPval, inum, MPval);
optr++;
}
@@ -645,12 +645,12 @@ mp_set_from_string(const char *str, int base, MPNumber *MPval)
mp_set_from_integer(0, &numerator);
mp_set_from_integer(1, &denominator);
while ((inum = char_val(*optr, base)) >= 0) {
- mpmuli(&denominator, base, &denominator);
- mpmuli(&numerator, base, &numerator);
+ mp_multiply_integer(&denominator, base, &denominator);
+ mp_multiply_integer(&numerator, base, &numerator);
mp_add_integer(&numerator, inum, &numerator);
optr++;
}
- mpdiv(&numerator, &denominator, &numerator);
+ mp_divide(&numerator, &denominator, &numerator);
mp_add(MPval, &numerator, MPval);
}
@@ -672,7 +672,7 @@ mp_set_from_string(const char *str, int base, MPNumber *MPval)
/* Get magnitude */
mp_set_from_integer(0, &MPexponent);
while ((inum = char_val(*optr, base)) >= 0) {
- mpmuli(&MPexponent, base, &MPexponent);
+ mp_multiply_integer(&MPexponent, base, &MPexponent);
mp_add_integer(&MPexponent, inum, &MPexponent);
optr++;
}
@@ -681,8 +681,8 @@ mp_set_from_string(const char *str, int base, MPNumber *MPval)
}
mp_set_from_integer(base, &MPbase);
- mppwr2(&MPbase, &MPexponent, &temp);
- mpmul(MPval, &temp, MPval);
+ mp_pwr(&MPbase, &MPexponent, &temp);
+ mp_multiply(MPval, &temp, MPval);
}
/* Strip trailing whitespace */
diff --git a/gcalctool/mp-internal.h b/gcalctool/mp-internal.h
index a20102a..4601444 100644
--- a/gcalctool/mp-internal.h
+++ b/gcalctool/mp-internal.h
@@ -42,10 +42,10 @@ struct {
void mpchk();
void mpgcd(int *, int *);
-void mpmul2(MPNumber *, int, MPNumber *, int);
+void mpmul2(const MPNumber *, int, MPNumber *, int);
void mp_normalize(MPNumber *, int trunc);
void mpexp1(const MPNumber *, MPNumber *);
-void mpmulq(MPNumber *, int, int, MPNumber *);
+void mpmulq(const MPNumber *, int, int, MPNumber *);
void mp_reciprocal(const MPNumber *, MPNumber *);
void mp_atan1N(int n, MPNumber *z);
diff --git a/gcalctool/mp-trigonometric.c b/gcalctool/mp-trigonometric.c
index eb41036..4393a83 100644
--- a/gcalctool/mp-trigonometric.c
+++ b/gcalctool/mp-trigonometric.c
@@ -75,7 +75,7 @@ mpsin1(const MPNumber *x, MPNumber *z, int do_sin)
}
b2 = max(MP.b,64) << 1;
- mpmul(x, x, &t2);
+ mp_multiply(x, x, &t2);
if (mp_compare_mp_to_int(&t2, 1) > 0) {
mperr("*** ABS(X) > 1 IN CALL TO MPSIN1 ***");
}
@@ -102,16 +102,16 @@ mpsin1(const MPNumber *x, MPNumber *z, int do_sin)
MP.t = min(MP.t,ts);
/* PUT R(I3) FIRST IN CASE ITS DIGITS ARE MAINLY ZERO */
- mpmul(&t2, &t1, &t1);
+ mp_multiply(&t2, &t1, &t1);
/* IF I*(I+1) IS NOT REPRESENTABLE AS AN INTEGER, THE FOLLOWING
* DIVISION BY I*(I+1) HAS TO BE SPLIT UP.
*/
if (i > b2) {
- mpdivi(&t1, -i, &t1);
- mpdivi(&t1, i + 1, &t1);
+ mp_divide_integer(&t1, -i, &t1);
+ mp_divide_integer(&t1, i + 1, &t1);
} else {
- mpdivi(&t1, -i * (i + 1), &t1);
+ mp_divide_integer(&t1, -i * (i + 1), &t1);
}
i += 2;
@@ -153,16 +153,16 @@ mp_acos(const MPNumber *x, MPNumber *z)
mperr("Error");
z->sign = 0;
} else if (x->sign == 0) {
- mpdivi(&MPpi, 2, z);
+ mp_divide_integer(&MPpi, 2, z);
} else if (mp_is_equal(x, &MP1)) {
z->sign = 0;
} else if (mp_is_equal(x, &MPn1)) {
mp_set_from_mp(&MPpi, z);
} else {
- mpmul(x, x, &MP2);
+ mp_multiply(x, x, &MP2);
mp_subtract(&MP1, &MP2, &MP2);
mp_sqrt(&MP2, &MP2);
- mpdiv(&MP2, x, &MP2);
+ mp_divide(&MP2, x, &MP2);
mp_atan(&MP2, &MPy);
if (x->sign > 0) {
mp_set_from_mp(&MPy, z);
@@ -189,11 +189,11 @@ mp_acosh(const MPNumber *x, MPNumber *z)
mperr("Error");
mp_set_from_integer(0, z);
} else {
- mpmul(x, x, &MP1);
+ mp_multiply(x, x, &MP1);
mp_add_integer(&MP1, -1, &MP1);
mp_sqrt(&MP1, &MP1);
mp_add(x, &MP1, &MP1);
- mpln(&MP1, z);
+ mp_ln(&MP1, z);
}
}
@@ -222,9 +222,9 @@ mp_asin(const MPNumber *x, MPNumber *z)
mp_set_from_mp(&t1, &t2);
mp_subtract(&t1, x, &t1);
mp_add(&t2, x, &t2);
- mpmul(&t1, &t2, &t2);
+ mp_multiply(&t1, &t2, &t2);
mp_root(&t2, -2, &t2);
- mpmul(x, &t2, z);
+ mp_multiply(x, &t2, z);
mp_atan(z, z);
return;
}
@@ -237,7 +237,7 @@ mp_asin(const MPNumber *x, MPNumber *z)
/* X == +-1 SO RETURN +-PI/2 */
mp_get_pi(z);
- mpdivi(z, t2.sign << 1, z);
+ mp_divide_integer(z, t2.sign << 1, z);
}
@@ -250,11 +250,11 @@ mp_asinh(const MPNumber *x, MPNumber *z)
{
MPNumber MP1;
- mpmul(x, x, &MP1);
+ mp_multiply(x, x, &MP1);
mp_add_integer(&MP1, 1, &MP1);
mp_sqrt(&MP1, &MP1);
mp_add(x, &MP1, &MP1);
- mpln(&MP1, z);
+ mp_ln(&MP1, z);
}
@@ -294,23 +294,23 @@ mp_atan(const MPNumber *x, MPNumber *z)
break;
q <<= 1;
- mpmul(&t2, &t2, z);
+ mp_multiply(&t2, &t2, z);
mp_add_integer(z, 1, z);
mp_sqrt(z, z);
mp_add_integer(z, 1, z);
- mpdiv(&t2, z, &t2);
+ mp_divide(&t2, z, &t2);
}
/* USE POWER SERIES NOW ARGUMENT IN (-0.5, 0.5) */
mp_set_from_mp(&t2, z);
- mpmul(&t2, &t2, &t1);
+ mp_multiply(&t2, &t2, &t1);
i = 1;
ts = MP.t;
/* SERIES LOOP. REDUCE T IF POSSIBLE. */
while ((MP.t = ts + 2 + t2.exponent) > 1) {
MP.t = min(MP.t,ts);
- mpmul(&t2, &t1, &t2);
+ mp_multiply(&t2, &t1, &t2);
mpmulq(&t2, -i, i + 2, &t2);
i += 2;
MP.t = ts;
@@ -320,7 +320,7 @@ mp_atan(const MPNumber *x, MPNumber *z)
/* RESTORE T, CORRECT FOR ARGUMENT REDUCTION, AND EXIT */
MP.t = ts;
- mpmuli(z, q, z);
+ mp_multiply_integer(z, q, z);
/* CHECK THAT RELATIVE ERROR LESS THAN 0.01 UNLESS EXPONENT
* OF X IS LARGE (WHEN ATAN MIGHT NOT WORK)
@@ -358,10 +358,10 @@ mp_atanh(const MPNumber *x, MPNumber *z)
} else {
mp_add(&MP1, x, &MP2);
mp_subtract(&MP1, x, &MP3);
- mpdiv(&MP2, &MP3, &MP3);
- mpln(&MP3, &MP3);
+ mp_divide(&MP2, &MP3, &MP3);
+ mp_ln(&MP3, &MP3);
mp_set_from_string("0.5", 10, &MP1);
- mpmul(&MP1, &MP3, z);
+ mp_multiply(&MP1, &MP3, z);
}
}
@@ -393,7 +393,7 @@ mp_cos(const MPNumber *x, MPNumber *z)
* COMPUTING PI/2 WITH ONE GUARD DIGIT.
*/
mp_get_pi(&t);
- mpdivi(&t, 2, &t);
+ mp_divide_integer(&t, 2, &t);
mp_subtract(&t, z, z);
mp_sin(z, z);
}
@@ -401,7 +401,7 @@ mp_cos(const MPNumber *x, MPNumber *z)
/* RETURNS Z = COSH(X) FOR MP NUMBERS X AND Z, X NOT TOO LARGE.
- * USES MPEXP, DIMENSION OF R IN COMMON AT LEAST 5T+12
+ * USES MP_EPOWY, DIMENSION OF R IN COMMON AT LEAST 5T+12
*/
void
mp_cosh(const MPNumber *x, MPNumber *z)
@@ -418,19 +418,19 @@ mp_cosh(const MPNumber *x, MPNumber *z)
mpchk();
mp_abs(x, &t);
- /* IF ABS(X) TOO LARGE MPEXP WILL PRINT ERROR MESSAGE
+ /* IF ABS(X) TOO LARGE MP_EPOWY WILL PRINT ERROR MESSAGE
* INCREASE M TO AVOID OVERFLOW WHEN COSH(X) REPRESENTABLE
*/
MP.m += 2;
- mpexp(&t, &t);
+ mp_epowy(&t, &t);
mp_reciprocal(&t, z);
mp_add(&t, z, z);
- /* RESTORE M. IF RESULT OVERFLOWS OR UNDERFLOWS, MPDIVI WILL
+ /* RESTORE M. IF RESULT OVERFLOWS OR UNDERFLOWS, mp_divide_integer WILL
* ACT ACCORDINGLY.
*/
MP.m += -2;
- mpdivi(z, 2, z);
+ mp_divide_integer(z, 2, z);
}
@@ -472,12 +472,12 @@ mp_sin(const MPNumber *x, MPNumber *z)
*/
else {
mp_atan1N(5, &t2);
- mpmuli(&t2, 4, &t2);
+ mp_multiply_integer(&t2, 4, &t2);
mp_atan1N(239, z);
mp_subtract(&t2, z, z);
- mpdiv(&t1, z, &t1);
- mpdivi(&t1, 8, &t1);
- mpcmf(&t1, &t1);
+ mp_divide(&t1, z, &t1);
+ mp_divide_integer(&t1, 8, &t1);
+ mp_fractional_component(&t1, &t1);
/* SUBTRACT 1/2, SAVE SIGN AND TAKE ABS */
mp_add_fraction(&t1, -1, 2, &t1);
@@ -488,7 +488,7 @@ mp_sin(const MPNumber *x, MPNumber *z)
}
t1.sign = 1;
- mpmuli(&t1, 4, &t1);
+ mp_multiply_integer(&t1, 4, &t1);
/* IF NOT LESS THAN 1, SUBTRACT FROM 2 */
if (t1.exponent > 0)
@@ -500,17 +500,17 @@ mp_sin(const MPNumber *x, MPNumber *z)
}
t1.sign = 1;
- mpmuli(&t1, 2, &t1);
+ mp_multiply_integer(&t1, 2, &t1);
/* NOW REDUCED TO FIRST QUADRANT, IF LESS THAN PI/4 USE
* POWER SERIES, ELSE COMPUTE COS OF COMPLEMENT
*/
if (t1.exponent > 0) {
mp_add_integer(&t1, -2, &t1);
- mpmul(&t1, z, &t1);
+ mp_multiply(&t1, z, &t1);
mpsin1(&t1, z, 0);
} else {
- mpmul(&t1, z, &t1);
+ mp_multiply(&t1, z, &t1);
mpsin1(&t1, z, 1);
}
}
@@ -537,7 +537,7 @@ mp_sin(const MPNumber *x, MPNumber *z)
/* RETURNS Z = SINH(X) FOR MP NUMBERS X AND Z, X NOT TOO LARGE.
- * METHOD IS TO USE MPEXP OR MPEXP1, SPACE = 5T+12
+ * METHOD IS TO USE MP_EPOWY OR MPEXP1, SPACE = 5T+12
* SAVE SIGN OF X AND CHECK FOR ZERO, SINH(0) = 0
*/
void
@@ -562,27 +562,27 @@ mp_sinh(const MPNumber *x, MPNumber *z)
if (t2.exponent <= 0) {
mpexp1(&t2, &t1);
mp_add_integer(&t1, 2, &t2);
- mpmul(&t2, &t1, z);
+ mp_multiply(&t2, &t1, z);
mp_add_integer(&t1, 1, &t2);
- mpdiv(z, &t2, z);
+ mp_divide(z, &t2, z);
}
- /* HERE ABS(X) >= 1, IF TOO LARGE MPEXP GIVES ERROR MESSAGE
+ /* HERE ABS(X) >= 1, IF TOO LARGE MP_EPOWY GIVES ERROR MESSAGE
* INCREASE M TO AVOID OVERFLOW IF SINH(X) REPRESENTABLE
*/
else {
MP.m += 2;
- mpexp(&t2, &t2);
+ mp_epowy(&t2, &t2);
mp_reciprocal(&t2, z);
mp_subtract(&t2, z, z);
- /* RESTORE M. IF RESULT OVERFLOWS OR UNDERFLOWS, MPDIVI AT
+ /* RESTORE M. IF RESULT OVERFLOWS OR UNDERFLOWS, mp_divide_integer AT
* STATEMENT 30 WILL ACT ACCORDINGLY.
*/
MP.m += -2;
}
/* DIVIDE BY TWO AND RESTORE SIGN */
- mpdivi(z, xs << 1, z);
+ mp_divide_integer(z, xs << 1, z);
}
@@ -599,12 +599,12 @@ mp_tan(const MPNumber *x, MPNumber *z)
mperr(_("Tangent is infinite"));
return;
}
- mpdiv(&MPsin, &MPcos, z);
+ mp_divide(&MPsin, &MPcos, z);
}
/* RETURNS Z = TANH(X) FOR MP NUMBERS X AND Z,
- * USING MPEXP OR MPEXP1, SPACE = 5T+12
+ * USING MP_EPOWY OR MPEXP1, SPACE = 5T+12
*/
void
mp_tanh(const MPNumber *x, MPNumber *z)
@@ -636,18 +636,18 @@ mp_tanh(const MPNumber *x, MPNumber *z)
}
/* HERE ABS(X) NOT SO LARGE */
- mpmuli(&t, 2, &t);
+ mp_multiply_integer(&t, 2, &t);
if (t.exponent > 0) {
- /* HERE ABS(X) >= 1/2 SO USE MPEXP */
- mpexp(&t, &t);
+ /* HERE ABS(X) >= 1/2 SO USE MP_EPOWY */
+ mp_epowy(&t, &t);
mp_add_integer(&t, -1, z);
mp_add_integer(&t, 1, &t);
- mpdiv(z, &t, z);
+ mp_divide(z, &t, z);
} else {
/* HERE ABS(X) < 1/2, SO USE MPEXP1 TO AVOID CANCELLATION */
mpexp1(&t, &t);
mp_add_integer(&t, 2, z);
- mpdiv(&t, z, z);
+ mp_divide(&t, z, z);
}
/* RESTORE SIGN */
diff --git a/gcalctool/mp.c b/gcalctool/mp.c
index ff0718c..821e8bc 100644
--- a/gcalctool/mp.c
+++ b/gcalctool/mp.c
@@ -336,8 +336,8 @@ mp_atan1N(int n, MPNumber *z)
*/
if (i >= id) {
mpmulq(&t, -i, i + 2, &t);
- mpdivi(&t, n, &t);
- mpdivi(&t, n, &t);
+ mp_divide_integer(&t, n, &t);
+ mp_divide_integer(&t, n, &t);
}
else {
mpmulq(&t, -i, (i + 2)*n*n, &t);
@@ -376,7 +376,7 @@ mpchk()
* I.E., Y = X - INTEGER PART OF X (TRUNCATED TOWARDS 0).
*/
void
-mpcmf(const MPNumber *x, MPNumber *y)
+mp_fractional_component(const MPNumber *x, MPNumber *y)
{
/* RETURN 0 IF X = 0
OR IF EXPONENT SO LARGE THAT NO FRACTIONAL PART */
@@ -410,7 +410,7 @@ mpcmf(const MPNumber *x, MPNumber *y)
* CHECK LEGALITY OF B, T, M AND MXR
*/
void
-mpcmim(const MPNumber *x, MPNumber *y)
+mp_integer_component(const MPNumber *x, MPNumber *y)
{
int i;
@@ -505,7 +505,7 @@ mp_compare_mp_to_mp(const MPNumber *x, const MPNumber *y)
* CHECK LEGALITY OF B, T, M AND MXR
*/
void
-mpdiv(const MPNumber *x, const MPNumber *y, MPNumber *z)
+mp_divide(const MPNumber *x, const MPNumber *y, MPNumber *z)
{
int i, ie, iz3;
MPNumber t;
@@ -514,7 +514,7 @@ mpdiv(const MPNumber *x, const MPNumber *y, MPNumber *z)
/* CHECK FOR DIVISION BY ZERO */
if (y->sign == 0) {
- mperr("*** ATTEMPTED DIVISION BY ZERO IN CALL TO MPDIV ***");
+ mperr("*** ATTEMPTED DIVISION BY ZERO IN CALL TO MP_DIVIDE ***");
z->sign = 0;
return;
}
@@ -531,13 +531,13 @@ mpdiv(const MPNumber *x, const MPNumber *y, MPNumber *z)
/* FORM RECIPROCAL OF Y */
mp_reciprocal(y, &t);
- /* SET EXPONENT OF R(I2) TO ZERO TO AVOID OVERFLOW IN MPMUL */
+ /* SET EXPONENT OF R(I2) TO ZERO TO AVOID OVERFLOW IN MP_MULTIPLY */
ie = t.exponent;
t.exponent = 0;
i = t.fraction[0];
/* MULTIPLY BY X */
- mpmul(x, &t, z);
+ mp_multiply(x, &t, z);
iz3 = z->fraction[0];
mpext(i, iz3, z);
@@ -549,7 +549,7 @@ mpdiv(const MPNumber *x, const MPNumber *y, MPNumber *z)
if (z->exponent < -MP.m)
mpunfl(z);
else if (z->exponent > MP.m)
- mpovfl(z, "*** OVERFLOW OCCURRED IN MPDIV ***");
+ mpovfl(z, "*** OVERFLOW OCCURRED IN MP_DIVIDE ***");
}
@@ -557,14 +557,14 @@ mpdiv(const MPNumber *x, const MPNumber *y, MPNumber *z)
* THIS IS MUCH FASTER THAN DIVISION BY AN MP NUMBER.
*/
void
-mpdivi(const MPNumber *x, int iy, MPNumber *z)
+mp_divide_integer(const MPNumber *x, int iy, MPNumber *z)
{
int i__1;
int c, i, k, b2, c2, i2, j1, j2, r1;
int j11, kh, iq, ir, iqj;
if (iy == 0) {
- mperr("*** ATTEMPTED DIVISION BY ZERO IN CALL TO MPDIVI ***");
+ mperr("*** ATTEMPTED DIVISION BY ZERO IN CALL TO MP_DIVIDE_INTEGER ***");
z->sign = 0;
return;
}
@@ -711,7 +711,7 @@ mpdivi(const MPNumber *x, int iy, MPNumber *z)
L210:
/* CARRY NEGATIVE SO OVERFLOW MUST HAVE OCCURRED */
mpchk();
- mperr("*** INTEGER OVERFLOW IN MPDIVI, B TOO LARGE ***");
+ mperr("*** INTEGER OVERFLOW IN MP_DIVIDE_INTEGER, B TOO LARGE ***");
z->sign = 0;
}
@@ -723,12 +723,12 @@ mp_is_integer(const MPNumber *x)
/* Multiplication and division by 10000 is used to get around a
* limitation to the "fix" for Sun bugtraq bug #4006391 in the
- * mpcmim() routine in mp.c, when the exponent is less than 1.
+ * mp_integer_component() routine in mp.c, when the exponent is less than 1.
*/
mp_set_from_integer(10000, &MPtt);
- mpmul(x, &MPtt, &MP0);
- mpdiv(&MP0, &MPtt, &MP0);
- mpcmim(&MP0, &MP1);
+ mp_multiply(x, &MPtt, &MP0);
+ mp_divide(&MP0, &MPtt, &MP0);
+ mp_integer_component(&MP0, &MP1);
return mp_is_equal(&MP0, &MP1);
}
@@ -765,7 +765,7 @@ mperr(const char *format, ...)
}
-/* RETURNS Y = EXP(X) FOR MP X AND Y.
+/* RETURNS Z = EXP(X) FOR MP X AND Z.
* EXP OF INTEGER AND FRACTIONAL PARTS OF X ARE COMPUTED
* SEPARATELY. SEE ALSO COMMENTS IN MPEXP1.
* TIME IS O(SQRT(T)M(T)).
@@ -773,26 +773,26 @@ mperr(const char *format, ...)
* CHECK LEGALITY OF B, T, M AND MXR
*/
void
-mpexp(const MPNumber *x, MPNumber *y)
+mp_epowy(const MPNumber *x, MPNumber *z)
{
float r__1;
int i, ie, ix, ts, xs, tss;
- float rx, ry, rlb;
+ float rx, rz, rlb;
MPNumber t1, t2;
mpchk();
/* CHECK FOR X == 0 */
if (x->sign == 0) {
- mp_set_from_integer(1, y);
+ mp_set_from_integer(1, z);
return;
}
/* CHECK IF ABS(X) < 1 */
if (x->exponent <= 0) {
/* USE MPEXP1 HERE */
- mpexp1(x, y);
- mp_add_integer(y, 1, y);
+ mpexp1(x, z);
+ mp_add_integer(z, 1, z);
return;
}
@@ -802,13 +802,13 @@ mpexp(const MPNumber *x, MPNumber *y)
rlb = log((float) MP.b) * (float)1.01;
if (mp_compare_mp_to_float(x, -(double)((float) (MP.m + 1)) * rlb) < 0) {
/* UNDERFLOW SO CALL MPUNFL AND RETURN */
- mpunfl(y);
+ mpunfl(z);
return;
}
if (mp_compare_mp_to_float(x, (float) MP.m * rlb) > 0) {
/* OVERFLOW HERE */
- mpovfl(y, "*** OVERFLOW IN SUBROUTINE MPEXP ***");
+ mpovfl(z, "*** OVERFLOW IN SUBROUTINE MP_EPOWY ***");
return;
}
@@ -823,17 +823,17 @@ mpexp(const MPNumber *x, MPNumber *y)
* SO DIVIDE BY 32.
*/
if (fabs(rx) > (float) MP.m) {
- mpdivi(&t2, 32, &t2);
+ mp_divide_integer(&t2, 32, &t2);
}
/* GET FRACTIONAL AND INTEGER PARTS OF ABS(X) */
ix = mp_cast_to_int(&t2);
- mpcmf(&t2, &t2);
+ mp_fractional_component(&t2, &t2);
/* ATTACH SIGN TO FRACTIONAL PART AND COMPUTE EXP OF IT */
t2.sign *= xs;
- mpexp1(&t2, y);
- mp_add_integer(y, 1, y);
+ mpexp1(&t2, z);
+ mp_add_integer(z, 1, z);
/* COMPUTE E-2 OR 1/E USING TWO EXTRA DIGITS IN CASE ABS(X) LARGE
* (BUT ONLY ONE EXTRA DIGIT IF T < 4)
@@ -854,7 +854,7 @@ mpexp(const MPNumber *x, MPNumber *y)
if (MP.t <= 2)
break;
++i;
- mpdivi(&t1, i * xs, &t1);
+ mp_divide_integer(&t1, i * xs, &t1);
MP.t = ts;
mp_add(&t2, &t1, &t2);
} while (t1.sign != 0);
@@ -864,30 +864,30 @@ mpexp(const MPNumber *x, MPNumber *y)
if (xs > 0) {
mp_add_integer(&t2, 2, &t2);
}
- mppwr(&t2, ix, &t2);
+ mp_pwr_integer(&t2, ix, &t2);
/* RESTORE T NOW */
MP.t = tss;
/* MULTIPLY EXPS OF INTEGER AND FRACTIONAL PARTS */
- mpmul(y, &t2, y);
+ mp_multiply(z, &t2, z);
/* MUST CORRECT RESULT IF DIVIDED BY 32 ABOVE. */
- if (fabs(rx) > (float) MP.m && y->sign != 0) {
+ if (fabs(rx) > (float) MP.m && z->sign != 0) {
for (i = 1; i <= 5; ++i) {
- /* SAVE EXPONENT TO AVOID OVERFLOW IN MPMUL */
- ie = y->exponent;
- y->exponent = 0;
- mpmul(y, y, y);
- y->exponent += ie << 1;
+ /* SAVE EXPONENT TO AVOID OVERFLOW IN MP_MULTIPLY */
+ ie = z->exponent;
+ z->exponent = 0;
+ mp_multiply(z, z, z);
+ z->exponent += ie << 1;
/* CHECK FOR UNDERFLOW AND OVERFLOW */
- if (y->exponent < -MP.m) {
- mpunfl(y);
+ if (z->exponent < -MP.m) {
+ mpunfl(z);
return;
}
- if (y->exponent > MP.m) {
- mpovfl(y, "*** OVERFLOW IN SUBROUTINE MPEXP ***");
+ if (z->exponent > MP.m) {
+ mpovfl(z, "*** OVERFLOW IN SUBROUTINE MP_EPOWY ***");
return;
}
}
@@ -899,15 +899,15 @@ mpexp(const MPNumber *x, MPNumber *y)
if (fabs(rx) > (float)10.0)
return;
- ry = mp_cast_to_float(y);
- if ((r__1 = ry - exp(rx), fabs(r__1)) < ry * (float)0.01)
+ rz = mp_cast_to_float(z);
+ if ((r__1 = rz - exp(rx), fabs(r__1)) < rz * (float)0.01)
return;
/* THE FOLLOWING MESSAGE MAY INDICATE THAT
* B**(T-1) IS TOO SMALL, OR THAT M IS TOO SMALL SO THE
* RESULT UNDERFLOWED.
*/
- mperr("*** ERROR OCCURRED IN MPEXP, RESULT INCORRECT ***");
+ mperr("*** ERROR OCCURRED IN MP_EPOWY, RESULT INCORRECT ***");
}
@@ -958,7 +958,7 @@ mpexp1(const MPNumber *x, MPNumber *y)
ic <<= 1;
if (ic < ib && ic != MP.b && i < q)
continue;
- mpdivi(&t1, ic, &t1);
+ mp_divide_integer(&t1, ic, &t1);
ic = 1;
}
}
@@ -979,9 +979,9 @@ mpexp1(const MPNumber *x, MPNumber *y)
break;
MP.t = min(MP.t,ts);
- mpmul(&t1, &t2, &t2);
+ mp_multiply(&t1, &t2, &t2);
++i;
- mpdivi(&t2, i, &t2);
+ mp_divide_integer(&t2, i, &t2);
MP.t = ts;
mp_add(&t2, y, y);
} while (t2.sign != 0);
@@ -993,12 +993,12 @@ mpexp1(const MPNumber *x, MPNumber *y)
/* APPLY (X+1)**2 - 1 = X(2 + X) FOR Q ITERATIONS */
for (i = 1; i <= q; ++i) {
mp_add_integer(y, 2, &t1);
- mpmul(&t1, y, y);
+ mp_multiply(&t1, y, y);
}
}
-/* ROUTINE CALLED BY MPDIV AND MP_SQRT TO ENSURE THAT
+/* ROUTINE CALLED BY MP_DIVIDE AND MP_SQRT TO ENSURE THAT
* RESULTS ARE REPRESENTED EXACTLY IN T-2 DIGITS IF THEY
* CAN BE. X IS AN MP NUMBER, I AND J ARE INTEGERS.
*/
@@ -1028,8 +1028,8 @@ mpext(int i, int j, MPNumber *x)
x->fraction[MP.t - 2] = MP.b - 1;
x->fraction[MP.t - 1] = MP.b;
- /* NORMALIZE X (LAST DIGIT B IS OK IN MPMULI) */
- mpmuli(x, 1, x);
+ /* NORMALIZE X (LAST DIGIT B IS OK IN MP_MULTIPLY_INTEGER) */
+ mp_multiply_integer(x, 1, x);
}
@@ -1121,7 +1121,7 @@ mp_is_less_equal(const MPNumber *x, const MPNumber *y)
* CHECK LEGALITY OF B, T, M AND MXR
*/
void
-mpln(MPNumber *x, MPNumber *y)
+mp_ln(const MPNumber *x, MPNumber *z)
{
int e, k;
float rx, rlx;
@@ -1131,14 +1131,14 @@ mpln(MPNumber *x, MPNumber *y)
/* CHECK THAT X IS POSITIVE */
if (x->sign <= 0) {
- mperr("*** X NONPOSITIVE IN CALL TO MPLN ***");
- y->sign = 0;
+ mperr("*** X NONPOSITIVE IN CALL TO MP_LN ***");
+ z->sign = 0;
return;
}
/* MOVE X TO LOCAL STORAGE */
mp_set_from_mp(x, &t1);
- y->sign = 0;
+ z->sign = 0;
k = 0;
/* LOOP TO GET APPROXIMATE LN(X) USING SINGLE-PRECISION */
@@ -1150,7 +1150,7 @@ mpln(MPNumber *x, MPNumber *y)
if (t2.sign == 0 || t2.exponent + 1 <= 0) {
/* COMPUTE FINAL CORRECTION ACCURATELY USING MPLNS */
mplns(&t2, &t2);
- mp_add(y, &t2, y);
+ mp_add(z, &t2, z);
return;
}
@@ -1164,17 +1164,17 @@ mpln(MPNumber *x, MPNumber *y)
rlx = log(rx) + (float) e * log((float) MP.b);
mp_set_from_float(-(double)rlx, &t2);
- /* UPDATE Y AND COMPUTE ACCURATE EXP OF APPROXIMATE LOG */
- mp_subtract(y, &t2, y);
- mpexp(&t2, &t2);
+ /* UPDATE Z AND COMPUTE ACCURATE EXP OF APPROXIMATE LOG */
+ mp_subtract(z, &t2, z);
+ mp_epowy(&t2, &t2);
/* COMPUTE RESIDUAL WHOSE LOG IS STILL TO BE FOUND */
- mpmul(&t1, &t2, &t1);
+ mp_multiply(&t1, &t2, &t1);
/* MAKE SURE NOT LOOPING INDEFINITELY */
++k;
if (k >= 10) {
- mperr("*** ERROR IN MPLN, ITERATION NOT CONVERGING ***");
+ mperr("*** ERROR IN MP_LN, ITERATION NOT CONVERGING ***");
return;
}
}
@@ -1186,14 +1186,14 @@ mpln(MPNumber *x, MPNumber *y)
* 1. log10(x) = log10(e) * log(x)
*/
void
-mp_logarithm(int n, MPNumber *MPx, MPNumber *MPretval)
+mp_logarithm(int n, MPNumber *x, MPNumber *z)
{
- MPNumber MP1, MP2;
+ MPNumber t1, t2;
- mp_set_from_integer(n, &MP1);
- mpln(&MP1, &MP1);
- mpln(MPx, &MP2);
- mpdiv(&MP2, &MP1, MPretval);
+ mp_set_from_integer(n, &t1);
+ mp_ln(&t1, &t1);
+ mp_ln(x, &t2);
+ mp_divide(&t2, &t1, z);
}
@@ -1229,13 +1229,13 @@ mplns(const MPNumber *x, MPNumber *y)
/* SAVE T AND GET STARTING APPROXIMATION TO -LN(1+X) */
ts = MP.t;
mp_set_from_mp(x, &t2);
- mpdivi(x, 4, &t1);
+ mp_divide_integer(x, 4, &t1);
mp_add_fraction(&t1, -1, 3, &t1);
- mpmul(x, &t1, &t1);
+ mp_multiply(x, &t1, &t1);
mp_add_fraction(&t1, 1, 2, &t1);
- mpmul(x, &t1, &t1);
+ mp_multiply(x, &t1, &t1);
mp_add_integer(&t1, -1, &t1);
- mpmul(x, &t1, y);
+ mp_multiply(x, &t1, y);
/* START NEWTON ITERATION USING SMALL T, LATER INCREASE */
@@ -1248,7 +1248,7 @@ mplns(const MPNumber *x, MPNumber *y)
while(1)
{
mpexp1(y, &t3);
- mpmul(&t2, &t3, &t1);
+ mp_multiply(&t2, &t3, &t1);
mp_add(&t3, &t1, &t3);
mp_add(&t2, &t3, &t3);
mp_subtract(y, &t3, y);
@@ -1320,12 +1320,12 @@ mpmaxr(MPNumber *x)
* EFFICIENT AND MACHINE-INDEPENDENT MANNER.
* IN COMMENTS TO OTHER MP ROUTINES, M(T) IS THE TIME
* TO PERFORM T-DIGIT MP MULTIPLICATION. THUS
- * M(T) = O(T**2) WITH THE PRESENT VERSION OF MPMUL,
+ * M(T) = O(T**2) WITH THE PRESENT VERSION OF MP_MULTIPLY,
* BUT M(T) = O(T.LOG(T).LOG(LOG(T))) IS THEORETICALLY POSSIBLE.
* CHECK LEGALITY OF B, T, M AND MXR
*/
void
-mpmul(const MPNumber *x, const MPNumber *y, MPNumber *z)
+mp_multiply(const MPNumber *x, const MPNumber *y, MPNumber *z)
{
int c, i, j, i2, xi;
MPNumber r;
@@ -1363,7 +1363,7 @@ mpmul(const MPNumber *x, const MPNumber *y, MPNumber *z)
/* CHECK FOR LEGAL BASE B DIGIT */
if (xi < 0 || xi >= MP.b) {
- mperr("*** ILLEGAL BASE B DIGIT IN CALL TO MPMUL, POSSIBLE OVERWRITING PROBLEM ***");
+ mperr("*** ILLEGAL BASE B DIGIT IN CALL TO MP_MULTIPLY, POSSIBLE OVERWRITING PROBLEM ***");
z->sign = 0;
return;
}
@@ -1374,7 +1374,7 @@ mpmul(const MPNumber *x, const MPNumber *y, MPNumber *z)
for (j = i2 - 1; j >= 0; j--) {
int ri = r.fraction[j] + c;
if (ri < 0) {
- mperr("*** INTEGER OVERFLOW IN MPMUL, B TOO LARGE ***");
+ mperr("*** INTEGER OVERFLOW IN MP_MULTIPLY, B TOO LARGE ***");
z->sign = 0;
return;
}
@@ -1382,7 +1382,7 @@ mpmul(const MPNumber *x, const MPNumber *y, MPNumber *z)
r.fraction[j] = ri - MP.b * c;
}
if (c != 0) {
- mperr("*** ILLEGAL BASE B DIGIT IN CALL TO MPMUL, POSSIBLE OVERWRITING PROBLEM ***");
+ mperr("*** ILLEGAL BASE B DIGIT IN CALL TO MP_MULTIPLY, POSSIBLE OVERWRITING PROBLEM ***");
z->sign = 0;
return;
}
@@ -1391,7 +1391,7 @@ mpmul(const MPNumber *x, const MPNumber *y, MPNumber *z)
if (c != 8) {
if (xi < 0 || xi >= MP.b) {
- mperr("*** ILLEGAL BASE B DIGIT IN CALL TO MPMUL, POSSIBLE OVERWRITING PROBLEM ***");
+ mperr("*** ILLEGAL BASE B DIGIT IN CALL TO MP_MULTIPLY, POSSIBLE OVERWRITING PROBLEM ***");
z->sign = 0;
return;
}
@@ -1400,7 +1400,7 @@ mpmul(const MPNumber *x, const MPNumber *y, MPNumber *z)
for (j = i2 - 1; j >= 0; j--) {
int ri = r.fraction[j] + c;
if (ri < 0) {
- mperr("*** INTEGER OVERFLOW IN MPMUL, B TOO LARGE ***");
+ mperr("*** INTEGER OVERFLOW IN MP_MULTIPLY, B TOO LARGE ***");
z->sign = 0;
return;
}
@@ -1409,7 +1409,7 @@ mpmul(const MPNumber *x, const MPNumber *y, MPNumber *z)
}
if (c != 0) {
- mperr("*** ILLEGAL BASE B DIGIT IN CALL TO MPMUL, POSSIBLE OVERWRITING PROBLEM ***");
+ mperr("*** ILLEGAL BASE B DIGIT IN CALL TO MP_MULTIPLY, POSSIBLE OVERWRITING PROBLEM ***");
z->sign = 0;
return;
}
@@ -1429,7 +1429,7 @@ mpmul(const MPNumber *x, const MPNumber *y, MPNumber *z)
* RESULT IS ROUNDED IF TRUNC == 0, OTHERWISE TRUNCATED.
*/
void
-mpmul2(MPNumber *x, int iy, MPNumber *z, int trunc)
+mpmul2(const MPNumber *x, int iy, MPNumber *z, int trunc)
{
int c, i, c1, c2, j1, j2;
int t1, t3, t4, ij, ri = 0, is, ix;
@@ -1547,12 +1547,12 @@ mpmul2(MPNumber *x, int iy, MPNumber *z, int trunc)
/* MULTIPLIES MP X BY SINGLE-PRECISION INTEGER IY GIVING MP Z.
- * THIS IS FASTER THAN USING MPMUL. RESULT IS ROUNDED.
+ * THIS IS FASTER THAN USING MP_MULTIPLY. RESULT IS ROUNDED.
* MULTIPLICATION BY 1 MAY BE USED TO NORMALIZE A NUMBER
* EVEN IF THE LAST DIGIT IS B.
*/
void
-mpmuli(MPNumber *x, int iy, MPNumber *z)
+mp_multiply_integer(const MPNumber *x, int iy, MPNumber *z)
{
mpmul2(x, iy, z, 0);
}
@@ -1560,7 +1560,7 @@ mpmuli(MPNumber *x, int iy, MPNumber *z)
/* MULTIPLIES MP X BY I/J, GIVING MP Y */
void
-mpmulq(MPNumber *x, int i, int j, MPNumber *y)
+mpmulq(const MPNumber *x, int i, int j, MPNumber *y)
{
int is, js;
@@ -1582,9 +1582,9 @@ mpmulq(MPNumber *x, int i, int j, MPNumber *y)
mpgcd(&is, &js);
if (abs(is) == 1) {
/* HERE IS = +-1 */
- mpdivi(x, is * js, y);
+ mp_divide_integer(x, is * js, y);
} else {
- mpdivi(x, js, y);
+ mp_divide_integer(x, js, y);
mpmul2(y, is, y, 0);
}
}
@@ -1745,10 +1745,10 @@ mp_get_pi(MPNumber *z)
mpchk();
mp_atan1N(5, &t);
- mpmuli(&t, 4, &t);
+ mp_multiply_integer(&t, 4, &t);
mp_atan1N(239, z);
mp_subtract(&t, z, z);
- mpmuli(z, 4, z);
+ mp_multiply_integer(z, 4, z);
/* RETURN IF ERROR IS LESS THAN 0.01 */
prec = fabs(mp_cast_to_float(z) - 3.1416);
@@ -1764,7 +1764,7 @@ mp_get_pi(MPNumber *z)
* (2T+6 IS ENOUGH IF N NONNEGATIVE).
*/
void
-mppwr(const MPNumber *x, int n, MPNumber *y)
+mp_pwr_integer(const MPNumber *x, int n, MPNumber *y)
{
int n2, ns;
MPNumber t;
@@ -1775,7 +1775,7 @@ mppwr(const MPNumber *x, int n, MPNumber *y)
mpchk();
n2 = -n2;
if (x->sign == 0) {
- mperr("*** ATTEMPT TO RAISE ZERO TO NEGATIVE POWER IN CALL TO SUBROUTINE MPPWR ***");
+ mperr("*** ATTEMPT TO RAISE ZERO TO NEGATIVE POWER IN CALL TO SUBROUTINE MP_PWR_INTEGER ***");
y->sign = 0;
return;
}
@@ -1808,23 +1808,23 @@ mppwr(const MPNumber *x, int n, MPNumber *y)
ns = n2;
n2 /= 2;
if (n2 << 1 != ns)
- mpmul(y, &t, y);
+ mp_multiply(y, &t, y);
if (n2 <= 0)
return;
- mpmul(&t, &t, &t);
+ mp_multiply(&t, &t, &t);
}
}
/* RETURNS Z = X**Y FOR MP NUMBERS X, Y AND Z, WHERE X IS
* POSITIVE (X == 0 ALLOWED IF Y > 0). SLOWER THAN
- * MPPWR AND MPQPWR, SO USE THEM IF POSSIBLE.
+ * MP_PWR_INTEGER AND MPQPWR, SO USE THEM IF POSSIBLE.
* DIMENSION OF R IN COMMON AT LEAST 7T+16
* CHECK LEGALITY OF B, T, M AND MXR
*/
void
-mppwr2(MPNumber *x, MPNumber *y, MPNumber *z)
+mp_pwr(const MPNumber *x, const MPNumber *y, MPNumber *z)
{
MPNumber t;
@@ -1838,19 +1838,19 @@ mppwr2(MPNumber *x, MPNumber *y, MPNumber *z)
{
/* HERE X IS ZERO, RETURN ZERO IF Y POSITIVE, OTHERWISE ERROR */
if (y->sign <= 0) {
- mperr("*** X ZERO AND Y NONPOSITIVE IN CALL TO MPPWR2 ***");
+ mperr("*** X ZERO AND Y NONPOSITIVE IN CALL TO MP_PWR ***");
}
z->sign = 0;
}
else {
/* USUAL CASE HERE, X POSITIVE
- * USE MPLN AND MPEXP TO COMPUTE POWER
+ * USE MPLN AND MP_EPOWY TO COMPUTE POWER
*/
- mpln(x, &t);
- mpmul(y, &t, z);
+ mp_ln(x, &t);
+ mp_multiply(y, &t, z);
- /* IF X**Y IS TOO LARGE, MPEXP WILL PRINT ERROR MESSAGE */
- mpexp(z, z);
+ /* IF X**Y IS TOO LARGE, MP_EPOWY WILL PRINT ERROR MESSAGE */
+ mp_epowy(z, z);
}
}
@@ -1912,13 +1912,13 @@ mp_reciprocal(const MPNumber *x, MPNumber *z)
if (MP.t <= ts) {
/* MAIN ITERATION LOOP */
while(1) {
- mpmul(x, &t1, &t2);
+ mp_multiply(x, &t1, &t2);
mp_add_integer(&t2, -1, &t2);
/* TEMPORARILY REDUCE T */
ts3 = MP.t;
MP.t = (MP.t + it0) / 2;
- mpmul(&t1, &t2, &t2);
+ mp_multiply(&t1, &t2, &t2);
/* RESTORE T */
MP.t = ts3;
@@ -2057,15 +2057,15 @@ mp_root(const MPNumber *x, int n, MPNumber *z)
/* MAIN ITERATION LOOP */
while(1) {
- mppwr(&t1, np, &t2);
- mpmul(x, &t2, &t2);
+ mp_pwr_integer(&t1, np, &t2);
+ mp_multiply(x, &t2, &t2);
mp_add_integer(&t2, -1, &t2);
/* TEMPORARILY REDUCE T */
ts3 = MP.t;
MP.t = (MP.t + it0) / 2;
- mpmul(&t1, &t2, &t2);
- mpdivi(&t2, np, &t2);
+ mp_multiply(&t1, &t2, &t2);
+ mp_divide_integer(&t2, np, &t2);
/* RESTORE T */
MP.t = ts3;
@@ -2101,8 +2101,8 @@ mp_root(const MPNumber *x, int n, MPNumber *z)
/* RESTORE T */
MP.t = ts;
if (n >= 0) {
- mppwr(&t1, n - 1, &t1);
- mpmul(x, &t1, z);
+ mp_pwr_integer(&t1, n - 1, &t1);
+ mp_multiply(x, &t1, z);
return;
}
@@ -2176,12 +2176,12 @@ mp_init(int idecpl, int itmax2)
(float) 2.0);
/* SEE IF T TOO LARGE FOR DIMENSION STATEMENTS */
- i2 = MP.t + 2;
+ i2 = MP.t;
if (i2 > itmax2) {
mperr("ITMAX2 TOO SMALL IN CALL TO MPSET, INCREASE ITMAX2 AND DIMENSIONS OF MP ARRAYS TO AT LEAST %d ***", i2);
/* REDUCE TO MAXIMUM ALLOWED BY DIMENSION STATEMENTS */
- MP.t = itmax2 - 2;
+ MP.t = itmax2;
}
/* CHECK LEGALITY OF B, T, M AND MXR (AT LEAST T+4) */
@@ -2210,7 +2210,7 @@ mp_sqrt(const MPNumber *x, MPNumber *z)
} else {
mp_root(x, -2, &t);
i = t.fraction[0];
- mpmul(x, &t, z);
+ mp_multiply(x, &t, z);
iy3 = z->fraction[0];
mpext(i, iy3, z);
}
@@ -2259,9 +2259,8 @@ pow_ii(int x, int n)
return(pow);
}
-/* Calculate the factorial of MPval. */
void
-mp_factorial(MPNumber *x, MPNumber *z)
+mp_factorial(const MPNumber *x, MPNumber *z)
{
int i, value;
@@ -2278,60 +2277,46 @@ mp_factorial(MPNumber *x, MPNumber *z)
value = mp_cast_to_int(x);
mp_set_from_mp(x, z);
for (i = 2; i < value; i++)
- mpmuli(z, i, z);
+ mp_multiply_integer(z, i, z);
}
int
-mp_modulus_divide(MPNumber *op1,
- MPNumber *op2,
- MPNumber *result)
+mp_modulus_divide(const MPNumber *x, const MPNumber *y, MPNumber *z)
{
- MPNumber MP1, MP2;
+ MPNumber t1, t2;
- if (!mp_is_integer(op1) || !mp_is_integer(op2)) {
+ if (!mp_is_integer(x) || !mp_is_integer(y)) {
return -EINVAL;
}
- mpdiv(op1, op2, &MP1);
- mpcmim(&MP1, &MP1);
- mpmul(&MP1, op2, &MP2);
- mp_subtract(op1, &MP2, result);
+ mp_divide(x, y, &t1);
+ mp_integer_component(&t1, &t1);
+ mp_multiply(&t1, y, &t2);
+ mp_subtract(x, &t2, z);
- mp_set_from_integer(0, &MP1);
- if ((mp_is_less_than(op2, &MP1)
- && mp_is_greater_than(result, &MP1)) ||
- mp_is_less_than(result, &MP1)) {
- mp_add(result, op2, result);
+ mp_set_from_integer(0, &t1);
+ if ((mp_is_less_than(y, &t1) && mp_is_greater_than(z, &t1)) || mp_is_less_than(z, &t1)) {
+ mp_add(z, y, z);
}
return 0;
}
-/* Do x^y */
void
-mp_xpowy(MPNumber *x, MPNumber *y, MPNumber *res)
+mp_xpowy(const MPNumber *x, const MPNumber *y, MPNumber *z)
{
if (mp_is_integer(y)) {
- mppwr(x, mp_cast_to_int(y), res);
+ mp_pwr_integer(x, mp_cast_to_int(y), z);
} else {
- mppwr2(x, y, res);
+ mp_pwr(x, y, z);
}
}
void
-mp_percent(MPNumber *s1, MPNumber *t1)
+mp_percent(const MPNumber *x, MPNumber *z)
{
- MPNumber MP1;
-
- mp_set_from_string("0.01", 10, &MP1);
- mpmul(s1, &MP1, t1);
-}
+ MPNumber t;
-void
-mp_epowy(MPNumber *s, MPNumber *t)
-{
- MPNumber MP1;
-
- mp_set_from_mp(s, &MP1);
- mpexp(&MP1, t);
+ mp_set_from_string("0.01", 10, &t);
+ mp_multiply(x, &t, z);
}
diff --git a/gcalctool/mp.h b/gcalctool/mp.h
index e127ec5..9302a3d 100644
--- a/gcalctool/mp.h
+++ b/gcalctool/mp.h
@@ -44,13 +44,14 @@
typedef struct
{
- /* sign (0, -1 or +1)
- * exponent (to base MP.b)
- * fraction = normalized fraction.
- */
+ /* Sign (+1, -1) or 0 for the value zero */
int sign;
+
+ /* Exponent (to base MP.b) */
int exponent;
- int fraction[MP_SIZE-2]; // Size MP.t?
+
+ /* Normalized fraction */
+ int fraction[MP_SIZE]; // Size MP.t?
} MPNumber;
/* If we're not using GNU C, elide __attribute__ */
@@ -58,93 +59,218 @@ typedef struct
# define __attribute__(x) /*NOTHING*/
#endif
-void mp_init(int, int);
-
-void mperr(const char *format, ...) __attribute__((format(printf, 1, 2)));
-
-int mp_compare_mp_to_mp(const MPNumber *x, const MPNumber *y);
-
-int mp_is_zero(const MPNumber *x);
-int mp_is_negative(const MPNumber *x);
-
-/* return true if parameter is integer */
-int mp_is_integer(const MPNumber *);
-
-/* return true if parameter is natural number, that is, a positive integer */
-int mp_is_natural(const MPNumber *);
-
-int mp_is_equal(const MPNumber *, const MPNumber *);
-int mp_is_greater_equal(const MPNumber *, const MPNumber *);
-int mp_is_greater_than(const MPNumber *, const MPNumber *);
-int mp_is_less_equal(const MPNumber *, const MPNumber *);
-int mp_is_less_than(const MPNumber *, const MPNumber *);
-
-void mp_abs(const MPNumber *, MPNumber *);
-void mp_invert_sign(const MPNumber *, MPNumber *);
-
-void mp_add(const MPNumber *, const MPNumber *, MPNumber *);
-void mp_add_integer(const MPNumber *, int, MPNumber *);
-void mp_add_fraction(const MPNumber *, int, int, MPNumber *);
-void mp_subtract(const MPNumber *, const MPNumber *, MPNumber *);
-
-void mpcmf(const MPNumber *, MPNumber *);
-void mpcmim(const MPNumber *, MPNumber *);
-void mpdiv(const MPNumber *, const MPNumber *, MPNumber *);
-void mpdivi(const MPNumber *, int, MPNumber *);
-void mpexp(const MPNumber *, MPNumber *);
-void mpln(MPNumber *, MPNumber *);
-void mp_logarithm(int n, MPNumber *MPx, MPNumber *MPretval);
-void mpmul(const MPNumber *, const MPNumber *, MPNumber *);
-void mpmuli(MPNumber *, int, MPNumber *);
-void mp_get_pi(MPNumber *z);
-void mppwr(const MPNumber *, int, MPNumber *);
-void mppwr2(MPNumber *, MPNumber *, MPNumber *);
-void mp_root(const MPNumber *x, int n, MPNumber *z);
-void mp_sqrt(const MPNumber *x, MPNumber *z);
-void mp_factorial(MPNumber *, MPNumber *);
-int mp_modulus_divide(MPNumber *op1, MPNumber *op2, MPNumber *result);
-void mp_percent(MPNumber *s1, MPNumber *t1);
-void mp_xpowy(MPNumber *MPx, MPNumber *MPy, MPNumber *MPres);
-void mp_epowy(MPNumber *s, MPNumber *t);
-
-/* mp-convert.c */
-void mp_set_from_mp(const MPNumber *, MPNumber *);
-void mp_set_from_float(float, MPNumber *);
-void mp_set_from_double(double, MPNumber *);
-void mp_set_from_integer(int, MPNumber *);
-void mp_set_from_fraction(int, int, MPNumber *);
-void mp_set_from_random(MPNumber *t);
-void mp_set_from_string(const char *number, int base, MPNumber *t);
-float mp_cast_to_float(const MPNumber *);
-double mp_cast_to_double(const MPNumber *);
-int mp_cast_to_int(const MPNumber *);
-void mp_cast_to_string(char *, int, const MPNumber *, int, int);
-
-/* mp-trigonometric.c */
-void mp_acos(const MPNumber *x, MPNumber *z);
-void mp_acosh(const MPNumber *x, MPNumber *z);
-void mp_asin(const MPNumber *x, MPNumber *z);
-void mp_asinh(const MPNumber *x, MPNumber *z);
-void mp_atan(const MPNumber *x, MPNumber *z);
-void mp_atanh(const MPNumber *x, MPNumber *z);
-void mp_cos(const MPNumber *x, MPNumber *z);
-void mp_cosh(const MPNumber *x, MPNumber *z);
-void mp_sin(const MPNumber *x, MPNumber *z);
-void mp_sinh(const MPNumber *x, MPNumber *z);
-void mp_tan(const MPNumber *x, MPNumber *z);
-void mp_tanh(const MPNumber *x, MPNumber *z);
-
-/* mp-binary.c */
-void mp_set_wordlen(int);
-int mp_is_overflow(const MPNumber *s1);
-void mp_and(const MPNumber *s1, const MPNumber *s2, MPNumber *t);
-void mp_or(const MPNumber *s1, const MPNumber *s2, MPNumber *t);
-void mp_xor(const MPNumber *s1, const MPNumber *s2, MPNumber *t);
-void mp_xnor(const MPNumber *s1, const MPNumber *s2, MPNumber *t);
-void mp_not(const MPNumber *s1, MPNumber *t);
-void mp_mask(const MPNumber *s1, MPNumber *t1);
-void mp_shift(MPNumber *s, MPNumber *t, int times);
-void mp_1s_complement(const MPNumber *s, MPNumber *res);
-void mp_2s_complement(const MPNumber *s, MPNumber *res);
+void mp_init(int, int);
+
+void mperr(const char *format, ...) __attribute__((format(printf, 1, 2)));
+
+/* Returns:
+ * 0 if x == y
+ * <0 if x < y
+ * >0 if x > y
+ */
+int mp_compare_mp_to_mp(const MPNumber *x, const MPNumber *y);
+
+/* Return true if the value is x == 0 */
+int mp_is_zero(const MPNumber *x);
+
+/* Return true if x < 0 */
+int mp_is_negative(const MPNumber *x);
+
+/* Return true if x is integer */
+int mp_is_integer(const MPNumber *x);
+
+/* Return true if x is a natural number (an integer >= 0) */
+int mp_is_natural(const MPNumber *x);
+
+/* Return true if x == y */
+int mp_is_equal(const MPNumber *x, const MPNumber *y);
+
+/* Return true if x >= y */
+int mp_is_greater_equal(const MPNumber *x, const MPNumber *y);
+
+/* Return true if x > y */
+int mp_is_greater_than(const MPNumber *x, const MPNumber *y);
+
+/* Return true if x <= y */
+int mp_is_less_equal(const MPNumber *x, const MPNumber *y);
+
+/* Return true if x < y */
+int mp_is_less_than(const MPNumber *x, const MPNumber *y);
+
+/* Sets z = |x| */
+void mp_abs(const MPNumber *x, MPNumber *z);
+
+/* Sets z = â??x */
+void mp_invert_sign(const MPNumber *x, MPNumber *z);
+
+/* Sets z = x + y */
+void mp_add(const MPNumber *x, const MPNumber *y, MPNumber *z);
+
+/* Sets z = x + y */
+void mp_add_integer(const MPNumber *x, int y, MPNumber *z);
+
+/* Sets z = x + numerator ÷ denominator */
+void mp_add_fraction(const MPNumber *x, int numerator, int denominator, MPNumber *z);
+
+/* Sets z = x â?? y */
+void mp_subtract(const MPNumber *x, const MPNumber *y, MPNumber *z);
+
+/* Sets z = x Ã? y */
+void mp_multiply(const MPNumber *x, const MPNumber *y, MPNumber *z);
+
+/* Sets z = x Ã? y */
+void mp_multiply_integer(const MPNumber *x, int y, MPNumber *z);
+
+/* Sets z = x ÷ y */
+void mp_divide(const MPNumber *x, const MPNumber *y, MPNumber *z);
+
+/* Sets z = x ÷ y */
+void mp_divide_integer(const MPNumber *x, int y, MPNumber *z);
+
+/* Sets z = fractional part of x */
+void mp_fractional_component(const MPNumber *x, MPNumber *z);
+
+/* Sets z = integer part of x */
+void mp_integer_component(const MPNumber *x, MPNumber *z);
+
+/* Sets z = ln(x) */
+void mp_ln(const MPNumber *x, MPNumber *z);
+
+/* Sets z = log_n(x) */
+void mp_logarithm(int n, MPNumber *x, MPNumber *z);
+
+/* Sets z = Ï? */
+void mp_get_pi(MPNumber *z);
+
+/* Sets z = x^y */
+void mp_pwr(const MPNumber *x, const MPNumber *y, MPNumber *z);
+
+/* Sets z = x^y */
+void mp_pwr_integer(const MPNumber *x, int y, MPNumber *z);
+
+/* Sets z = nâ??x */
+void mp_root(const MPNumber *x, int n, MPNumber *z);
+
+/* Sets z = â??x */
+void mp_sqrt(const MPNumber *x, MPNumber *z);
+
+/* Sets z = x! */
+void mp_factorial(const MPNumber *x, MPNumber *z);
+
+/* Sets z = x modulo y */
+int mp_modulus_divide(const MPNumber *x, const MPNumber *y, MPNumber *z);
+
+/* Sets z = x% (i.e. x / 100)*/
+void mp_percent(const MPNumber *x, MPNumber *z);
+
+/* Sets z = x^y */
+void mp_xpowy(const MPNumber *x, const MPNumber *y, MPNumber *z);
+
+/* Sets z = e^x */
+void mp_epowy(const MPNumber *x, MPNumber *z);
+
+/* Sets z = x */
+void mp_set_from_mp(const MPNumber *x, MPNumber *z);
+
+/* Sets z = x */
+void mp_set_from_float(float x, MPNumber *z);
+
+/* Sets z = x */
+void mp_set_from_double(double x, MPNumber *z);
+
+/* Sets z = x */
+void mp_set_from_integer(int x, MPNumber *z);
+
+/* Sets z = numerator ÷ denominator */
+void mp_set_from_fraction(int numerator, int denominator, MPNumber *z);
+
+/* Sets z to be a uniform random number in the range [0, 1] */
+void mp_set_from_random(MPNumber *z);
+
+/* Sets z from a string representation in 'text' in base 'base' */
+void mp_set_from_string(const char *text, int base, MPNumber *z);
+
+/* Returns x as a native single-precision floating point number */
+float mp_cast_to_float(const MPNumber *x);
+
+/* Returns x as a native double-precision floating point number */
+double mp_cast_to_double(const MPNumber *x);
+
+/* Returns x as a native integer */
+int mp_cast_to_int(const MPNumber *x);
+
+/* Converts x to a string representation.
+ * The string is written into 'buffer' which is guaranteed to be at least 'buffer_length' octets in size.
+ * If not enough space is available the string is truncated.
+ * The numbers are written in 'base' (e.g. 10).
+ * Fractional components are truncated at 'max_digits' digits.
+ */
+void mp_cast_to_string(const MPNumber *x, int base, int max_digits, char *buffer, int buffer_length);
+
+/* Sets z = sin(x) */
+void mp_sin(const MPNumber *x, MPNumber *z);
+
+/* Sets z = cos(x) */
+void mp_cos(const MPNumber *x, MPNumber *z);
+
+/* Sets z = tan(x) */
+void mp_tan(const MPNumber *x, MPNumber *z);
+
+/* Sets z = asin(x) */
+void mp_asin(const MPNumber *x, MPNumber *z);
+
+/* Sets z = acos(x) */
+void mp_acos(const MPNumber *x, MPNumber *z);
+
+/* Sets z = atan(x) */
+void mp_atan(const MPNumber *x, MPNumber *z);
+
+/* Sets z = sinh(x) */
+void mp_sinh(const MPNumber *x, MPNumber *z);
+
+/* Sets z = cosh(x) */
+void mp_cosh(const MPNumber *x, MPNumber *z);
+
+/* Sets z = tanh(x) */
+void mp_tanh(const MPNumber *x, MPNumber *z);
+
+/* Sets z = asinh(x) */
+void mp_asinh(const MPNumber *x, MPNumber *z);
+
+/* Sets z = acosh(x) */
+void mp_acosh(const MPNumber *x, MPNumber *z);
+
+/* Sets z = atanh(x) */
+void mp_atanh(const MPNumber *x, MPNumber *z);
+
+/* Returns true if x is cannot be represented in a binary word of length 'wordlen' */
+int mp_is_overflow(const MPNumber *x, int wordlen);
+
+/* Sets z = boolean AND for each bit in x and z */
+void mp_and(const MPNumber *x, const MPNumber *y, MPNumber *z);
+
+/* Sets z = boolean OR for each bit in x and z */
+void mp_or(const MPNumber *x, const MPNumber *y, MPNumber *z);
+
+/* Sets z = boolean XOR for each bit in x and z */
+void mp_xor(const MPNumber *x, const MPNumber *y, MPNumber *z);
+
+/* Sets z = boolean XNOR for each bit in x and z for word of length 'wordlen' */
+void mp_xnor(const MPNumber *x, const MPNumber *y, int wordlen, MPNumber *z);
+
+/* Sets z = boolean NOT for each bit in x and z for word of length 'wordlen' */
+void mp_not(const MPNumber *x, int wordlen, MPNumber *z);
+
+/* Sets z = x masked to 'wordlen' bits */
+void mp_mask(const MPNumber *x, int wordlen, MPNumber *z);
+
+/* Sets z = x shifted by 'count' bits. Positive shift increases the value, negative decreases */
+void mp_shift(const MPNumber *x, int count, MPNumber *z);
+
+/* Sets z to be the ones complement of x for word of length 'wordlen' */
+void mp_1s_complement(const MPNumber *x, int wordlen, MPNumber *z);
+
+/* Sets z to be the twos complement of x for word of length 'wordlen' */
+void mp_2s_complement(const MPNumber *x, int wordlen, MPNumber *z);
#endif /* MP_H */
diff --git a/gcalctool/register.c b/gcalctool/register.c
index 8a0f11d..f117821 100644
--- a/gcalctool/register.c
+++ b/gcalctool/register.c
@@ -153,7 +153,7 @@ void constant_set(int index, const char *name, MPNumber *value)
/* NOTE: Constants are written out with no thousands separator and with a
radix character of ".". */
- mp_cast_to_string(text, MAX_LOCALIZED, value, 10, MAX_DIGITS);
+ mp_cast_to_string(value, 10, MAX_DIGITS, text, MAX_LOCALIZED);
SNPRINTF(key, MAXLINE, "constant%1dvalue", index);
set_resource(key, text);
}
diff --git a/gcalctool/unittest.c b/gcalctool/unittest.c
index d0ee0dc..e7ddc88 100644
--- a/gcalctool/unittest.c
+++ b/gcalctool/unittest.c
@@ -19,6 +19,9 @@
* 02111-1307, USA.
*/
+#include <stdio.h>
+#include <stdarg.h>
+
#include "unittest.h"
#include "display.h"
@@ -28,30 +31,56 @@
static int fails = 0;
+static void pass(const char *format, ...) __attribute__((format(printf, 1, 2)));
+static void fail(const char *format, ...) __attribute__((format(printf, 1, 2)));
+
+static void pass(const char *format, ...)
+{
+ va_list args;
+
+ printf(" PASS: ");
+ va_start(args, format);
+ vprintf(format, args);
+ va_end(args);
+ printf("\n");
+}
+
+static void fail(const char *format, ...)
+{
+ va_list args;
+
+ printf("*FAIL: ");
+ va_start(args, format);
+ vprintf(format, args);
+ va_end(args);
+ printf("\n");
+ fails++;
+}
+
static void
test(char *expression, char *expected, int expected_error)
{
int error;
MPNumber result;
- char result_str[MAXLINE];
+ char result_str[MAXLINE] = "";
error = ce_parse(expression, &result);
- if(error != 0 || error != expected_error)
- {
- if(error == expected_error)
- printf("SUCCESS: '%s' -> error %d\n", expression, error);
+
+ if(error == 0) {
+ mp_cast_to_string(&result, basevals[v->base], 9, result_str, MAXLINE);
+ if(expected_error != 0)
+ fail("'%s' -> %s, expected error %d", expression, result_str, expected_error);
+ else if(strcmp(result_str, expected) != 0)
+ fail("'%s' -> '%s', expected '%s'", expression, result_str, expected);
else
- printf("FAIL: '%s' -> error %d, expected error %d and result '%s'\n", expression, error, expected_error, expected);
- return;
+ pass("'%s' -> '%s'", expression, result_str);
}
-
- mp_cast_to_string(result_str, MAXLINE, &result, basevals[v->base], 9);
- if(strcmp(result_str, expected) != 0) {
- fails++;
- printf("FAIL: '%s' -> '%s', expected '%s'\n", expression, result_str, expected);
+ else {
+ if(error == expected_error)
+ pass("'%s' -> error %d", expression, error);
+ else
+ fail("'%s' -> error %d, expected error %d", expression, error, expected_error);
}
- else
- printf("SUCCESS: '%s' -> '%s'\n", expression, result_str);
}
@@ -107,6 +136,7 @@ test_parser()
test("(-3)/(-6)", "0.5", 0);
test("1+2*3", "7", 0);
+ test("1+(2*3)", "7", 0);
test("(1+2)*3", "9", 0);
test("100%", "1", 0);
@@ -118,6 +148,8 @@ test_parser()
test("-10^2", "-100", 0);
test("(-10)^2", "100", 0);
test("4^3^2", "262144", 0);
+ test("4^(3^2)", "262144", 0);
+ test("(4^3)^2", "4096", 0);
test("0!", "1", 0);
test("1!", "1", 0);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]