| 1 |
% This file is part of MetaPost; |
| 2 |
% the MetaPost program is in the public domain. |
| 3 |
% See the <Show version...> code in mpost.w for more info. |
| 4 |
|
| 5 |
% Here is TeX material that gets inserted after \input webmac |
| 6 |
|
| 7 |
\font\tenlogo=logo10 % font used for the METAFONT logo |
| 8 |
\font\logos=logosl10 |
| 9 |
\def\MF{{\tenlogo META}\-{\tenlogo FONT}} |
| 10 |
\def\MP{{\tenlogo META}\-{\tenlogo POST}} |
| 11 |
\def\pct!{{\char`\%}} % percent sign in ordinary text |
| 12 |
\def\psqrt#1{\sqrt{\mathstrut#1}} |
| 13 |
|
| 14 |
|
| 15 |
\def\title{Math support functions for decNumber based math} |
| 16 |
\pdfoutput=1 |
| 17 |
|
| 18 |
@ Introduction. |
| 19 |
|
| 20 |
@c |
| 21 |
#include "mpconfig.h" |
| 22 |
#include <w2c/config.h> |
| 23 |
#include <stdio.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <string.h> |
| 26 |
#include <math.h> |
| 27 |
#include "mpmathdecimal.h" /* internal header */ |
| 28 |
#define ROUND(a) floor((a)+0.5) |
| 29 |
@h |
| 30 |
|
| 31 |
@ @c |
| 32 |
@<Declarations@> |
| 33 |
|
| 34 |
@ @(mpmathdecimal.h@>= |
| 35 |
#ifndef MPMATHDECIMAL_H |
| 36 |
#define MPMATHDECIMAL_H 1 |
| 37 |
#include "mplib.h" |
| 38 |
#include "mpmp.h" /* internal header */ |
| 39 |
#define DECNUMDIGITS 1000 |
| 40 |
#include "decNumber.h" |
| 41 |
@<Internal library declarations@> |
| 42 |
#endif |
| 43 |
|
| 44 |
@* Math initialization. |
| 45 |
|
| 46 |
First, here are some very important constants. |
| 47 |
|
| 48 |
@d E_STRING "2.7182818284590452353602874713526624977572470936999595749669676277240766303535" |
| 49 |
@d PI_STRING "3.1415926535897932384626433832795028841971693993751058209749445923078164062862" |
| 50 |
@d fraction_multiplier 4096 |
| 51 |
@d angle_multiplier 16 |
| 52 |
|
| 53 |
@ Here are the functions that are static as they are not used elsewhere |
| 54 |
|
| 55 |
@<Declarations@>= |
| 56 |
|
| 57 |
static void mp_decimal_scan_fractional_token (MP mp, integer64 n); |
| 58 |
static void mp_decimal_scan_numeric_token (MP mp, integer64 n); |
| 59 |
static void mp_ab_vs_cd (MP mp, mp_number *ret, mp_number a, mp_number b, mp_number c, mp_number d); |
| 60 |
/*|static void mp_decimal_ab_vs_cd (MP mp, mp_number *ret, mp_number a, mp_number b, mp_number c, mp_number d);|*/ |
| 61 |
static void mp_decimal_crossing_point (MP mp, mp_number *ret, mp_number a, mp_number b, mp_number c); |
| 62 |
static void mp_decimal_number_modulo (mp_number *a, mp_number b); |
| 63 |
static void mp_decimal_print_number (MP mp, mp_number n); |
| 64 |
static char * mp_decimal_number_tostring (MP mp, mp_number n); |
| 65 |
static void mp_decimal_slow_add (MP mp, mp_number *ret, mp_number x_orig, mp_number y_orig); |
| 66 |
static void mp_decimal_square_rt (MP mp, mp_number *ret, mp_number x_orig); |
| 67 |
static void mp_decimal_sin_cos (MP mp, mp_number z_orig, mp_number *n_cos, mp_number *n_sin); |
| 68 |
static void mp_init_randoms (MP mp, int seed); |
| 69 |
static void mp_number_angle_to_scaled (mp_number *A); |
| 70 |
static void mp_number_fraction_to_scaled (mp_number *A); |
| 71 |
static void mp_number_scaled_to_fraction (mp_number *A); |
| 72 |
static void mp_number_scaled_to_angle (mp_number *A); |
| 73 |
static void mp_decimal_m_unif_rand (MP mp, mp_number *ret, mp_number x_orig); |
| 74 |
static void mp_decimal_m_norm_rand (MP mp, mp_number *ret); |
| 75 |
static void mp_decimal_m_exp (MP mp, mp_number *ret, mp_number x_orig); |
| 76 |
static void mp_decimal_m_log (MP mp, mp_number *ret, mp_number x_orig); |
| 77 |
static void mp_decimal_pyth_sub (MP mp, mp_number *r, mp_number a, mp_number b); |
| 78 |
static void mp_decimal_pyth_add (MP mp, mp_number *r, mp_number a, mp_number b); |
| 79 |
static void mp_decimal_n_arg (MP mp, mp_number *ret, mp_number x, mp_number y); |
| 80 |
static void mp_decimal_velocity (MP mp, mp_number *ret, mp_number st, mp_number ct, mp_number sf, mp_number cf, mp_number t); |
| 81 |
static void mp_set_decimal_from_int(mp_number *A, integer64 B); |
| 82 |
static void mp_set_decimal_from_boolean(mp_number *A, integer64 B); |
| 83 |
static void mp_set_decimal_from_scaled(mp_number *A, integer64 B); |
| 84 |
static void mp_set_decimal_from_addition(mp_number *A, mp_number B, mp_number C); |
| 85 |
static void mp_set_decimal_from_substraction (mp_number *A, mp_number B, mp_number C); |
| 86 |
static void mp_set_decimal_from_div(mp_number *A, mp_number B, mp_number C); |
| 87 |
static void mp_set_decimal_from_mul(mp_number *A, mp_number B, mp_number C); |
| 88 |
static void mp_set_decimal_from_int_div(mp_number *A, mp_number B, integer64 C); |
| 89 |
static void mp_set_decimal_from_int_mul(mp_number *A, mp_number B, integer64 C); |
| 90 |
static void mp_set_decimal_from_of_the_way(MP mp, mp_number *A, mp_number t, mp_number B, mp_number C); |
| 91 |
static void mp_number_negate(mp_number *A); |
| 92 |
static void mp_number_add(mp_number *A, mp_number B); |
| 93 |
static void mp_number_substract(mp_number *A, mp_number B); |
| 94 |
static void mp_number_half(mp_number *A); |
| 95 |
static void mp_number_halfp(mp_number *A); |
| 96 |
static void mp_number_double(mp_number *A); |
| 97 |
static void mp_number_add_scaled(mp_number *A, integer64 B); /* also for negative B */ |
| 98 |
static void mp_number_multiply_int(mp_number *A, integer64 B); |
| 99 |
static void mp_number_divide_int(mp_number *A, integer64 B); |
| 100 |
static void mp_decimal_abs(mp_number *A); |
| 101 |
static void mp_number_clone(mp_number *A, mp_number B); |
| 102 |
static void mp_number_swap(mp_number *A, mp_number *B); |
| 103 |
static integer64 mp_round_unscaled(mp_number x_orig); |
| 104 |
static integer64 mp_number_to_int(mp_number A); |
| 105 |
static integer64 mp_number_to_scaled(mp_number A); |
| 106 |
static integer64 mp_number_to_boolean(mp_number A); |
| 107 |
static double mp_number_to_double(mp_number A); |
| 108 |
static int mp_number_odd(mp_number A); |
| 109 |
static int mp_number_equal(mp_number A, mp_number B); |
| 110 |
static int mp_number_greater(mp_number A, mp_number B); |
| 111 |
static int mp_number_less(mp_number A, mp_number B); |
| 112 |
static int mp_number_nonequalabs(mp_number A, mp_number B); |
| 113 |
static void mp_number_floor (mp_number *i); |
| 114 |
static void mp_decimal_fraction_to_round_scaled (mp_number *x); |
| 115 |
static void mp_decimal_number_make_scaled (MP mp, mp_number *r, mp_number p, mp_number q); |
| 116 |
static void mp_decimal_number_make_fraction (MP mp, mp_number *r, mp_number p, mp_number q); |
| 117 |
static void mp_decimal_number_take_fraction (MP mp, mp_number *r, mp_number p, mp_number q); |
| 118 |
static void mp_decimal_number_take_scaled (MP mp, mp_number *r, mp_number p, mp_number q); |
| 119 |
static void mp_new_number (MP mp, mp_number *n, mp_number_type t) ; |
| 120 |
static void mp_free_number (MP mp, mp_number *n) ; |
| 121 |
static void mp_set_decimal_from_double(mp_number *A, double B); |
| 122 |
static void mp_free_decimal_math (MP mp); |
| 123 |
static void mp_decimal_set_precision (MP mp); |
| 124 |
static void mp_check_decNumber (MP mp, decNumber *dec, decContext *context); |
| 125 |
static int decNumber_check (decNumber *dec, decContext *context); |
| 126 |
static char * mp_decnumber_tostring (decNumber *n); |
| 127 |
|
| 128 |
static void decNumber_from_int64(decNumber *result, integer64 A,decContext *context); |
| 129 |
@ We do not want special numbers as return values for functions, so: |
| 130 |
|
| 131 |
|
| 132 |
@c |
| 133 |
int decNumber_check (decNumber *dec, decContext *context) |
| 134 |
{ |
| 135 |
int test = false; |
| 136 |
if (context->status & DEC_Overflow) { |
| 137 |
test = true; |
| 138 |
context->status &= ~DEC_Overflow; |
| 139 |
} |
| 140 |
if (context->status & DEC_Underflow) { |
| 141 |
test = true; |
| 142 |
context->status &= ~DEC_Underflow; |
| 143 |
} |
| 144 |
if (context->status & DEC_Errors) { |
| 145 |
/*|fprintf(stdout, "DEC_ERROR %x (%s)\n", context->status, decContextStatusToString(context));|*/ |
| 146 |
test = true; |
| 147 |
decNumberZero(dec); |
| 148 |
} |
| 149 |
context->status = 0; |
| 150 |
if (decNumberIsSpecial(dec)) { |
| 151 |
test = true; |
| 152 |
if (decNumberIsInfinite(dec)) { |
| 153 |
if (decNumberIsNegative(dec)) { |
| 154 |
decNumberCopyNegate(dec, &EL_GORDO_decNumber); |
| 155 |
} else { |
| 156 |
decNumberCopy(dec, &EL_GORDO_decNumber); |
| 157 |
} |
| 158 |
} else { /* Nan */ |
| 159 |
decNumberZero(dec); |
| 160 |
} |
| 161 |
} |
| 162 |
if (decNumberIsZero(dec) && decNumberIsNegative(dec)) { |
| 163 |
decNumberZero(dec); |
| 164 |
} |
| 165 |
return test; |
| 166 |
} |
| 167 |
void mp_check_decNumber (MP mp, decNumber *dec, decContext *context) |
| 168 |
{ |
| 169 |
mp->arith_error = decNumber_check (dec, context); |
| 170 |
} |
| 171 |
void decNumber_from_int64(decNumber *result, integer64 A,decContext *context){ |
| 172 |
if((INT32_MIN<=A) && (A<=INT32_MAX)){ |
| 173 |
decNumberFromInt32(result, (int32_t)(A)); |
| 174 |
return; |
| 175 |
} else { |
| 176 |
decNumber sc,dec_ms,dec_ls; |
| 177 |
int s = 1; |
| 178 |
uint32_t ms,ls;; |
| 179 |
if (A<0){ |
| 180 |
s=-1; |
| 181 |
A=-A; |
| 182 |
} |
| 183 |
ms=(uint32_t)( (integer64)0x00000000ffffffff & (A>>32)); |
| 184 |
ls=(uint32_t)( (integer64)0x00000000ffffffff & (A)); |
| 185 |
decNumberFromUInt32(&sc,UINT32_MAX); /* sc =1<<32 - 1 */ |
| 186 |
decNumberAdd(&sc, &sc, &one, context); /* sc =1<<32 */ |
| 187 |
decNumberFromUInt32(&dec_ms,ms); |
| 188 |
decNumberFromUInt32(&dec_ls,ls); |
| 189 |
decNumberMultiply(&dec_ms, &dec_ms, &sc, context); |
| 190 |
decNumberAdd(result, &dec_ms, &dec_ls, context); |
| 191 |
if (s==-1){ |
| 192 |
decNumberCopyNegate(result,result); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
@ There are a few short decNumber functions that do not exist, but |
| 203 |
make life easier for us: |
| 204 |
|
| 205 |
@d decNumberIsPositive(A) !(decNumberIsZero(A) || decNumberIsNegative(A)) |
| 206 |
|
| 207 |
@c |
| 208 |
static decContext set; |
| 209 |
static decContext limitedset; |
| 210 |
static void checkZero (decNumber *ret) { |
| 211 |
if (decNumberIsZero(ret) && decNumberIsNegative(ret)) |
| 212 |
decNumberZero(ret); |
| 213 |
} |
| 214 |
static int decNumberLess(decNumber *a, decNumber *b) { |
| 215 |
decNumber comp; |
| 216 |
decNumberCompare(&comp, a, b, &set); |
| 217 |
return decNumberIsNegative(&comp); |
| 218 |
} |
| 219 |
static int decNumberGreater(decNumber *a, decNumber *b) { |
| 220 |
decNumber comp; |
| 221 |
decNumberCompare(&comp, a, b, &set); |
| 222 |
return decNumberIsPositive(&comp); |
| 223 |
} |
| 224 |
static void decNumberFromDouble(decNumber *A, double B) { |
| 225 |
char buf[1000]; |
| 226 |
char *c; |
| 227 |
snprintf(buf,1000,"%-650.325lf",B); |
| 228 |
c = buf; |
| 229 |
while (*c++) { |
| 230 |
if (*c == ' ') { |
| 231 |
*c = '\0'; |
| 232 |
break; |
| 233 |
} |
| 234 |
} |
| 235 |
decNumberFromString(A, buf, &set); |
| 236 |
} |
| 237 |
static double decNumberToDouble(decNumber *A) { |
| 238 |
char *buffer = malloc(A->digits + 14); |
| 239 |
double res = 0.0; |
| 240 |
assert (buffer); |
| 241 |
decNumberToString(A, buffer); |
| 242 |
if (sscanf(buffer, "%lf", &res)) { |
| 243 |
free(buffer); |
| 244 |
return res; |
| 245 |
} else { |
| 246 |
free(buffer); |
| 247 |
/*|mp->arith_error = 1;|*/ |
| 248 |
return 0.0; /* whatever*/ |
| 249 |
} |
| 250 |
} |
| 251 |
@ Borrowed code from libdfp: |
| 252 |
|
| 253 |
% x^3 x^5 x^7 |
| 254 |
%arctan(x) = x - --- + --- - --- + ... |
| 255 |
% 3 5 7 |
| 256 |
$$ \arctan(x) = x - {x^3\over3} + {x^5\over5} - {x^7\over7} + \ldots$$ |
| 257 |
|
| 258 |
|
| 259 |
This power series works well, if $x$ is close to zero ($|x|<0.5$). |
| 260 |
If x is larger, the series converges too slowly, |
| 261 |
so in order to get a smaller x, we apply the identity |
| 262 |
|
| 263 |
% sqrt(1+x^2) - 1 |
| 264 |
%arctan(x) = 2*arctan --------------- |
| 265 |
% x |
| 266 |
$$ \arctan(x) = 2\,\arctan{{\sqrt{1+x^2}-1}\over x}$$ |
| 267 |
|
| 268 |
twice. The first application gives us a new $x$ with $x < 1$. |
| 269 |
The second application gives us a new x with $x < 0.4142136$. |
| 270 |
For that $x$, we use the power series and multiply the result by four. |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
@c |
| 275 |
static void decNumberAtan (decNumber *result, decNumber *x_orig, decContext *set) |
| 276 |
{ |
| 277 |
decNumber x, f, g, mx2, term; |
| 278 |
int i; |
| 279 |
decNumberCopy(&x, x_orig); |
| 280 |
if (decNumberIsZero (&x)) { |
| 281 |
decNumberCopy (result, &x); |
| 282 |
return; |
| 283 |
} |
| 284 |
for (i=0; i<2; i++) { |
| 285 |
decNumber y; |
| 286 |
decNumberMultiply (&y, &x, &x, set); /* $y = x^2$ */ |
| 287 |
decNumberAdd (&y, &y, &one, set); /* $y = y+1$*/ |
| 288 |
decNumberSquareRoot (&y, &y, set); /* $y = sqrt(y)$ */ |
| 289 |
decNumberSubtract (&y, &y, &one, set); /* $y = y-1$ */ |
| 290 |
decNumberDivide (&x, &y, &x, set); /* $x = y/x$ */ |
| 291 |
if (decNumberIsZero (&x)) { |
| 292 |
decNumberCopy (result, &x); |
| 293 |
return; |
| 294 |
} |
| 295 |
} |
| 296 |
decNumberCopy (&f, &x); /* $f(0) = x$ */ |
| 297 |
decNumberCopy (&g, &one); /*$ g(0) = 1$*/ |
| 298 |
decNumberCopy (&term, &x); /*$ term = x$*/ |
| 299 |
decNumberCopy (result, &x); /*$ sum = x $*/ |
| 300 |
decNumberMultiply (&mx2, &x, &x, set); /*$ mx2 = x^2$*/ |
| 301 |
decNumberMinus (&mx2, &mx2, set); /*$ mx2 = -x^2 $*/ |
| 302 |
for (i=0; i<2*set->digits; i++) { |
| 303 |
decNumberMultiply (&f, &f, &mx2, set); |
| 304 |
decNumberAdd (&g, &g, &two_decNumber, set); |
| 305 |
decNumberDivide (&term, &f, &g, set); |
| 306 |
decNumberAdd (result, result, &term, set); |
| 307 |
} |
| 308 |
decNumberAdd (result, result, result, set); |
| 309 |
decNumberAdd (result, result, result, set); |
| 310 |
return; |
| 311 |
} |
| 312 |
static void decNumberAtan2 (decNumber *result, decNumber *y, decNumber *x, decContext *set) |
| 313 |
{ |
| 314 |
decNumber temp; |
| 315 |
if (!decNumberIsInfinite (x) && !decNumberIsZero (y) |
| 316 |
&& !decNumberIsInfinite (y) && !decNumberIsZero (x)) { |
| 317 |
decNumberDivide (&temp, y, x, set); |
| 318 |
decNumberAtan (result, &temp, set); |
| 319 |
/* decNumberAtan doesn't quite return the values in the ranges we |
| 320 |
* want for x < 0. So we need to do some correction */ |
| 321 |
if (decNumberIsNegative (x)) { |
| 322 |
if (decNumberIsNegative (y)) { |
| 323 |
decNumberSubtract(result, result, &PI_decNumber, set); |
| 324 |
} else { |
| 325 |
decNumberAdd(result, result, &PI_decNumber, set); |
| 326 |
} |
| 327 |
} |
| 328 |
return; |
| 329 |
} |
| 330 |
if (decNumberIsInfinite (y) && decNumberIsInfinite (x)) { |
| 331 |
/* If x and y are both inf, the result depends on the sign of x */ |
| 332 |
decNumberDivide(result, &PI_decNumber, &four_decNumber, set); |
| 333 |
if (decNumberIsNegative (x) ) { |
| 334 |
decNumber a; |
| 335 |
decNumberFromDouble(&a, 3.0); |
| 336 |
decNumberMultiply(result, result, &a, set); |
| 337 |
} |
| 338 |
} else if (!decNumberIsZero (y) && !decNumberIsInfinite (x) ) { |
| 339 |
/* If y is non-zero and x is non-inf, the result is +-pi/2 */ |
| 340 |
decNumberDivide(result, &PI_decNumber, &two_decNumber, set); |
| 341 |
} else { /* Otherwise it is +0 if x is positive, +pi if x is neg */ |
| 342 |
if (decNumberIsNegative (x)) { |
| 343 |
decNumberCopy(result, &PI_decNumber); |
| 344 |
} else { |
| 345 |
decNumberZero(result); |
| 346 |
} |
| 347 |
} |
| 348 |
/* Atan2 will be negative if y<0 */ |
| 349 |
if (decNumberIsNegative (y)) { |
| 350 |
decNumberMinus(result, result, set); |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
@ And these are the ones that {\it are} used elsewhere |
| 355 |
|
| 356 |
@<Internal library declarations@>= |
| 357 |
void * mp_initialize_decimal_math (MP mp); |
| 358 |
|
| 359 |
@ |
| 360 |
|
| 361 |
@d unity 1 |
| 362 |
@d two 2 |
| 363 |
@d three 3 |
| 364 |
@d four 4 |
| 365 |
@d half_unit 0.5 |
| 366 |
@d three_quarter_unit 0.75 |
| 367 |
@d coef_bound ((7.0/3.0)*fraction_multiplier) /* |fraction| approximation to 7/3 */ |
| 368 |
@d fraction_threshold 0.04096 /* a |fraction| coefficient less than this is zeroed */ |
| 369 |
@d half_fraction_threshold (fraction_threshold/2) /* half of |fraction_threshold| */ |
| 370 |
@d scaled_threshold 0.000122 /* a |scaled| coefficient less than this is zeroed */ |
| 371 |
@d half_scaled_threshold (scaled_threshold/2) /* half of |scaled_threshold| */ |
| 372 |
@d near_zero_angle (0.0256*angle_multiplier) /* an angle of about 0.0256 */ |
| 373 |
@d p_over_v_threshold 0x80000 /* TODO */ |
| 374 |
@d equation_threshold 0.001 |
| 375 |
@d tfm_warn_threshold 0.0625 |
| 376 |
@d epsilon pow(2.0,-173.0) /* almost "1E-52" */ |
| 377 |
@d epsilonf pow(2.0,-52.0) |
| 378 |
@d EL_GORDO "1E1000000" /* the largest value that \MP\ likes. */ |
| 379 |
@d warning_limit "1E1000000" /* this is a large value that can just be expressed without loss of precision */ |
| 380 |
@d DECPRECISION_DEFAULT 34 |
| 381 |
|
| 382 |
@<Declarations@>= |
| 383 |
static decNumber zero; |
| 384 |
static decNumber one; |
| 385 |
static decNumber minusone; |
| 386 |
static decNumber two_decNumber; |
| 387 |
static decNumber three_decNumber; |
| 388 |
static decNumber four_decNumber; |
| 389 |
static decNumber fraction_multiplier_decNumber; |
| 390 |
static decNumber angle_multiplier_decNumber; |
| 391 |
static decNumber fraction_one_decNumber; |
| 392 |
static decNumber fraction_one_plus_decNumber; |
| 393 |
static decNumber PI_decNumber; |
| 394 |
static decNumber epsilon_decNumber; |
| 395 |
static decNumber EL_GORDO_decNumber; |
| 396 |
static decNumber **factorials = NULL; |
| 397 |
static int last_cached_factorial = 0; |
| 398 |
static boolean initialized = false ; |
| 399 |
@ @c |
| 400 |
void * mp_initialize_decimal_math (MP mp) { |
| 401 |
math_data *math = (math_data *)mp_xmalloc(mp,1,sizeof(math_data)); |
| 402 |
/* various decNumber initializations */ |
| 403 |
decContextDefault(&set, DEC_INIT_BASE); /* initialize */ |
| 404 |
set.traps=0; /* no traps, thank you */ |
| 405 |
decContextDefault(&limitedset, DEC_INIT_BASE); /* initialize */ |
| 406 |
limitedset.traps=0; /* no traps, thank you */ |
| 407 |
limitedset.emax = 999999; |
| 408 |
limitedset.emin = -999999; |
| 409 |
set.digits = DECPRECISION_DEFAULT; |
| 410 |
limitedset.digits = DECPRECISION_DEFAULT; |
| 411 |
if (!initialized) { |
| 412 |
initialized = true ; |
| 413 |
decNumberFromInt32(&one, 1); |
| 414 |
decNumberFromInt32(&minusone, -1); |
| 415 |
decNumberFromInt32(&zero, 0); |
| 416 |
decNumberFromInt32(&two_decNumber, two); |
| 417 |
decNumberFromInt32(&three_decNumber, three); |
| 418 |
decNumberFromInt32(&four_decNumber, four); |
| 419 |
decNumberFromInt32(&fraction_multiplier_decNumber, fraction_multiplier); |
| 420 |
decNumberFromInt32(&fraction_one_decNumber, fraction_one); |
| 421 |
decNumberFromInt32(&fraction_one_plus_decNumber, (fraction_one+1)); |
| 422 |
decNumberFromInt32(&angle_multiplier_decNumber, angle_multiplier); |
| 423 |
decNumberFromString(&PI_decNumber, PI_STRING, &set); |
| 424 |
decNumberFromDouble(&epsilon_decNumber, epsilon); |
| 425 |
decNumberFromString(&EL_GORDO_decNumber, EL_GORDO, &set); |
| 426 |
factorials = (decNumber **)mp_xmalloc(mp,PRECALC_FACTORIALS_CACHESIZE,sizeof(decNumber *)); |
| 427 |
factorials[0] = (decNumber *)mp_xmalloc(mp,1,sizeof(decNumber)); |
| 428 |
decNumberCopy(factorials[0], &one); |
| 429 |
} |
| 430 |
|
| 431 |
/* alloc */ |
| 432 |
math->allocate = mp_new_number; |
| 433 |
math->free = mp_free_number; |
| 434 |
mp_new_number (mp, &math->precision_default, mp_scaled_type); |
| 435 |
decNumberFromInt32(math->precision_default.data.num, DECPRECISION_DEFAULT); |
| 436 |
mp_new_number (mp, &math->precision_max, mp_scaled_type); |
| 437 |
decNumberFromInt32(math->precision_max.data.num, DECNUMDIGITS); |
| 438 |
mp_new_number (mp, &math->precision_min, mp_scaled_type); |
| 439 |
decNumberFromInt32(math->precision_min.data.num, 2); |
| 440 |
/* here are the constants for |scaled| objects */ |
| 441 |
mp_new_number (mp, &math->epsilon_t, mp_scaled_type); |
| 442 |
decNumberCopy(math->epsilon_t.data.num, &epsilon_decNumber); |
| 443 |
mp_new_number (mp, &math->inf_t, mp_scaled_type); |
| 444 |
decNumberCopy(math->inf_t.data.num, &EL_GORDO_decNumber); |
| 445 |
mp_new_number (mp, &math->warning_limit_t, mp_scaled_type); |
| 446 |
decNumberFromString(math->warning_limit_t.data.num, warning_limit, &set); |
| 447 |
mp_new_number (mp, &math->one_third_inf_t, mp_scaled_type); |
| 448 |
decNumberDivide(math->one_third_inf_t.data.num, math->inf_t.data.num, &three_decNumber, &set); |
| 449 |
mp_new_number (mp, &math->unity_t, mp_scaled_type); |
| 450 |
decNumberCopy(math->unity_t.data.num, &one); |
| 451 |
mp_new_number (mp, &math->two_t, mp_scaled_type); |
| 452 |
decNumberFromInt32(math->two_t.data.num, two); |
| 453 |
mp_new_number (mp, &math->three_t, mp_scaled_type); |
| 454 |
decNumberFromInt32(math->three_t.data.num, three); |
| 455 |
mp_new_number (mp, &math->half_unit_t, mp_scaled_type); |
| 456 |
decNumberFromString(math->half_unit_t.data.num, "0.5", &set); |
| 457 |
mp_new_number (mp, &math->three_quarter_unit_t, mp_scaled_type); |
| 458 |
decNumberFromString(math->three_quarter_unit_t.data.num, "0.75", &set); |
| 459 |
mp_new_number (mp, &math->zero_t, mp_scaled_type); |
| 460 |
decNumberZero(math->zero_t.data.num); |
| 461 |
/* |fractions| */ |
| 462 |
mp_new_number (mp, &math->arc_tol_k, mp_fraction_type); |
| 463 |
{ |
| 464 |
decNumber fourzeroninesix; |
| 465 |
decNumberFromInt32(&fourzeroninesix, 4096); |
| 466 |
decNumberDivide(math->arc_tol_k.data.num, &one, &fourzeroninesix, &set); |
| 467 |
/* quit when change in arc length estimate reaches this */ |
| 468 |
} |
| 469 |
mp_new_number (mp, &math->fraction_one_t, mp_fraction_type); |
| 470 |
decNumberFromInt32(math->fraction_one_t.data.num, fraction_one); |
| 471 |
mp_new_number (mp, &math->fraction_half_t, mp_fraction_type); |
| 472 |
decNumberFromInt32(math->fraction_half_t.data.num, fraction_half); |
| 473 |
mp_new_number (mp, &math->fraction_three_t, mp_fraction_type); |
| 474 |
decNumberFromInt32(math->fraction_three_t.data.num, fraction_three); |
| 475 |
mp_new_number (mp, &math->fraction_four_t, mp_fraction_type); |
| 476 |
decNumberFromInt32(math->fraction_four_t.data.num, fraction_four); |
| 477 |
/* |angles| */ |
| 478 |
mp_new_number (mp, &math->three_sixty_deg_t, mp_angle_type); |
| 479 |
decNumberFromInt32(math->three_sixty_deg_t.data.num, 360 * angle_multiplier); |
| 480 |
mp_new_number (mp, &math->one_eighty_deg_t, mp_angle_type); |
| 481 |
decNumberFromInt32(math->one_eighty_deg_t.data.num, 180 * angle_multiplier); |
| 482 |
/* various approximations */ |
| 483 |
mp_new_number (mp, &math->one_k, mp_scaled_type); |
| 484 |
decNumberFromDouble(math->one_k.data.num, 1.0/64); |
| 485 |
mp_new_number (mp, &math->sqrt_8_e_k, mp_scaled_type); |
| 486 |
{ |
| 487 |
decNumberFromDouble(math->sqrt_8_e_k.data.num, 112428.82793 / 65536.0); |
| 488 |
/* $2^{16}\sqrt{8/e}\approx 112428.82793$ */ |
| 489 |
} |
| 490 |
mp_new_number (mp, &math->twelve_ln_2_k, mp_fraction_type); |
| 491 |
{ |
| 492 |
decNumberFromDouble(math->twelve_ln_2_k.data.num, 139548959.6165 / 65536.0); |
| 493 |
/* $2^{24}\cdot12\ln2\approx139548959.6165$ */ |
| 494 |
} |
| 495 |
mp_new_number (mp, &math->coef_bound_k, mp_fraction_type); |
| 496 |
decNumberFromDouble(math->coef_bound_k.data.num,coef_bound); |
| 497 |
mp_new_number (mp, &math->coef_bound_minus_1, mp_fraction_type); |
| 498 |
decNumberFromDouble(math->coef_bound_minus_1.data.num,coef_bound - 1 / 65536.0); |
| 499 |
mp_new_number (mp, &math->twelvebits_3, mp_scaled_type); |
| 500 |
{ |
| 501 |
decNumberFromDouble(math->twelvebits_3.data.num, 1365 / 65536.0); |
| 502 |
/* $1365\approx 2^{12}/3$ */ |
| 503 |
} |
| 504 |
mp_new_number (mp, &math->twentysixbits_sqrt2_t, mp_fraction_type); |
| 505 |
{ |
| 506 |
decNumberFromDouble(math->twentysixbits_sqrt2_t.data.num, 94906265.62 / 65536.0); |
| 507 |
/* $2^{26}\sqrt2\approx94906265.62$ */ |
| 508 |
} |
| 509 |
mp_new_number (mp, &math->twentyeightbits_d_t, mp_fraction_type); |
| 510 |
{ |
| 511 |
decNumberFromDouble(math->twentyeightbits_d_t.data.num, 35596754.69 / 65536.0); |
| 512 |
/* $2^{28}d\approx35596754.69$ */ |
| 513 |
} |
| 514 |
mp_new_number (mp, &math->twentysevenbits_sqrt2_d_t, mp_fraction_type); |
| 515 |
{ |
| 516 |
decNumberFromDouble(math->twentysevenbits_sqrt2_d_t.data.num, 25170706.63 / 65536.0); |
| 517 |
/* $2^{27}\sqrt2\,d\approx25170706.63$ */ |
| 518 |
} |
| 519 |
/* thresholds */ |
| 520 |
mp_new_number (mp, &math->fraction_threshold_t, mp_fraction_type); |
| 521 |
decNumberFromDouble(math->fraction_threshold_t.data.num, fraction_threshold); |
| 522 |
mp_new_number (mp, &math->half_fraction_threshold_t, mp_fraction_type); |
| 523 |
decNumberFromDouble(math->half_fraction_threshold_t.data.num, half_fraction_threshold); |
| 524 |
mp_new_number (mp, &math->scaled_threshold_t, mp_scaled_type); |
| 525 |
decNumberFromDouble(math->scaled_threshold_t.data.num, scaled_threshold); |
| 526 |
mp_new_number (mp, &math->half_scaled_threshold_t, mp_scaled_type); |
| 527 |
decNumberFromDouble(math->half_scaled_threshold_t.data.num, half_scaled_threshold); |
| 528 |
mp_new_number (mp, &math->near_zero_angle_t, mp_angle_type); |
| 529 |
decNumberFromDouble(math->near_zero_angle_t.data.num, near_zero_angle); |
| 530 |
mp_new_number (mp, &math->p_over_v_threshold_t, mp_fraction_type); |
| 531 |
decNumberFromDouble(math->p_over_v_threshold_t.data.num, p_over_v_threshold); |
| 532 |
mp_new_number (mp, &math->equation_threshold_t, mp_scaled_type); |
| 533 |
decNumberFromDouble(math->equation_threshold_t.data.num, equation_threshold); |
| 534 |
mp_new_number (mp, &math->tfm_warn_threshold_t, mp_scaled_type); |
| 535 |
decNumberFromDouble(math->tfm_warn_threshold_t.data.num, tfm_warn_threshold); |
| 536 |
/* functions */ |
| 537 |
math->from_int = mp_set_decimal_from_int; |
| 538 |
math->from_boolean = mp_set_decimal_from_boolean; |
| 539 |
math->from_scaled = mp_set_decimal_from_scaled; |
| 540 |
math->from_double = mp_set_decimal_from_double; |
| 541 |
math->from_addition = mp_set_decimal_from_addition; |
| 542 |
math->from_substraction = mp_set_decimal_from_substraction; |
| 543 |
math->from_oftheway = mp_set_decimal_from_of_the_way; |
| 544 |
math->from_div = mp_set_decimal_from_div; |
| 545 |
math->from_mul = mp_set_decimal_from_mul; |
| 546 |
math->from_int_div = mp_set_decimal_from_int_div; |
| 547 |
math->from_int_mul = mp_set_decimal_from_int_mul; |
| 548 |
math->negate = mp_number_negate; |
| 549 |
math->add = mp_number_add; |
| 550 |
math->substract = mp_number_substract; |
| 551 |
math->half = mp_number_half; |
| 552 |
math->halfp = mp_number_halfp; |
| 553 |
math->do_double = mp_number_double; |
| 554 |
math->abs = mp_decimal_abs; |
| 555 |
math->clone = mp_number_clone; |
| 556 |
math->swap = mp_number_swap; |
| 557 |
math->add_scaled = mp_number_add_scaled; |
| 558 |
math->multiply_int = mp_number_multiply_int; |
| 559 |
math->divide_int = mp_number_divide_int; |
| 560 |
math->to_boolean = mp_number_to_boolean; |
| 561 |
math->to_scaled = mp_number_to_scaled; |
| 562 |
math->to_double = mp_number_to_double; |
| 563 |
math->to_int = mp_number_to_int; |
| 564 |
math->odd = mp_number_odd; |
| 565 |
math->equal = mp_number_equal; |
| 566 |
math->less = mp_number_less; |
| 567 |
math->greater = mp_number_greater; |
| 568 |
math->nonequalabs = mp_number_nonequalabs; |
| 569 |
math->round_unscaled = mp_round_unscaled; |
| 570 |
math->floor_scaled = mp_number_floor; |
| 571 |
math->fraction_to_round_scaled = mp_decimal_fraction_to_round_scaled; |
| 572 |
math->make_scaled = mp_decimal_number_make_scaled; |
| 573 |
math->make_fraction = mp_decimal_number_make_fraction; |
| 574 |
math->take_fraction = mp_decimal_number_take_fraction; |
| 575 |
math->take_scaled = mp_decimal_number_take_scaled; |
| 576 |
math->velocity = mp_decimal_velocity; |
| 577 |
math->n_arg = mp_decimal_n_arg; |
| 578 |
math->m_log = mp_decimal_m_log; |
| 579 |
math->m_exp = mp_decimal_m_exp; |
| 580 |
math->m_unif_rand = mp_decimal_m_unif_rand; |
| 581 |
math->m_norm_rand = mp_decimal_m_norm_rand; |
| 582 |
math->pyth_add = mp_decimal_pyth_add; |
| 583 |
math->pyth_sub = mp_decimal_pyth_sub; |
| 584 |
math->fraction_to_scaled = mp_number_fraction_to_scaled; |
| 585 |
math->scaled_to_fraction = mp_number_scaled_to_fraction; |
| 586 |
math->scaled_to_angle = mp_number_scaled_to_angle; |
| 587 |
math->angle_to_scaled = mp_number_angle_to_scaled; |
| 588 |
math->init_randoms = mp_init_randoms; |
| 589 |
math->sin_cos = mp_decimal_sin_cos; |
| 590 |
math->slow_add = mp_decimal_slow_add; |
| 591 |
math->sqrt = mp_decimal_square_rt; |
| 592 |
math->print = mp_decimal_print_number; |
| 593 |
math->tostring = mp_decimal_number_tostring; |
| 594 |
math->modulo = mp_decimal_number_modulo; |
| 595 |
math->ab_vs_cd = mp_ab_vs_cd; |
| 596 |
math->crossing_point = mp_decimal_crossing_point; |
| 597 |
math->scan_numeric = mp_decimal_scan_numeric_token; |
| 598 |
math->scan_fractional = mp_decimal_scan_fractional_token; |
| 599 |
math->free_math = mp_free_decimal_math; |
| 600 |
math->set_precision = mp_decimal_set_precision; |
| 601 |
return (void *)math; |
| 602 |
} |
| 603 |
|
| 604 |
void mp_decimal_set_precision (MP mp) { |
| 605 |
int i; |
| 606 |
i = decNumberToInt32((decNumber *)internal_value (mp_number_precision).data.num, &set); |
| 607 |
set.digits = i; |
| 608 |
limitedset.digits = i; |
| 609 |
} |
| 610 |
|
| 611 |
void mp_free_decimal_math (MP mp) { |
| 612 |
free_number (((math_data *)mp->math)->three_sixty_deg_t); |
| 613 |
free_number (((math_data *)mp->math)->one_eighty_deg_t); |
| 614 |
free_number (((math_data *)mp->math)->fraction_one_t); |
| 615 |
free_number (((math_data *)mp->math)->zero_t); |
| 616 |
free_number (((math_data *)mp->math)->half_unit_t); |
| 617 |
free_number (((math_data *)mp->math)->three_quarter_unit_t); |
| 618 |
free_number (((math_data *)mp->math)->unity_t); |
| 619 |
free_number (((math_data *)mp->math)->two_t); |
| 620 |
free_number (((math_data *)mp->math)->three_t); |
| 621 |
free_number (((math_data *)mp->math)->one_third_inf_t); |
| 622 |
free_number (((math_data *)mp->math)->inf_t); |
| 623 |
free_number (((math_data *)mp->math)->warning_limit_t); |
| 624 |
free_number (((math_data *)mp->math)->one_k); |
| 625 |
free_number (((math_data *)mp->math)->sqrt_8_e_k); |
| 626 |
free_number (((math_data *)mp->math)->twelve_ln_2_k); |
| 627 |
free_number (((math_data *)mp->math)->coef_bound_k); |
| 628 |
free_number (((math_data *)mp->math)->coef_bound_minus_1); |
| 629 |
free_number (((math_data *)mp->math)->fraction_threshold_t); |
| 630 |
free_number (((math_data *)mp->math)->half_fraction_threshold_t); |
| 631 |
free_number (((math_data *)mp->math)->scaled_threshold_t); |
| 632 |
free_number (((math_data *)mp->math)->half_scaled_threshold_t); |
| 633 |
free_number (((math_data *)mp->math)->near_zero_angle_t); |
| 634 |
free_number (((math_data *)mp->math)->p_over_v_threshold_t); |
| 635 |
free_number (((math_data *)mp->math)->equation_threshold_t); |
| 636 |
free_number (((math_data *)mp->math)->tfm_warn_threshold_t); |
| 637 |
/* For sake of speed, we accept this memory leak. */ |
| 638 |
/* for (i = 0; i <= last_cached_factorial; i++) \{*/ |
| 639 |
/* free(factorials[i]);*/ |
| 640 |
/* \}*/ |
| 641 |
/* free(factorials); */ |
| 642 |
free(mp->math); |
| 643 |
} |
| 644 |
|
| 645 |
@ Creating an destroying |mp_number| objects |
| 646 |
|
| 647 |
@ @c |
| 648 |
void mp_new_number (MP mp, mp_number *n, mp_number_type t) { |
| 649 |
(void)mp; |
| 650 |
n->data.num = mp_xmalloc(mp,1,sizeof(decNumber)); |
| 651 |
decNumberZero(n->data.num); |
| 652 |
n->type = t; |
| 653 |
} |
| 654 |
|
| 655 |
@ |
| 656 |
|
| 657 |
@c |
| 658 |
void mp_free_number (MP mp, mp_number *n) { |
| 659 |
(void)mp; |
| 660 |
free(n->data.num); |
| 661 |
n->data.num = NULL; |
| 662 |
n->type = mp_nan_type; |
| 663 |
} |
| 664 |
|
| 665 |
@ Here are the low-level functions on |mp_number| items, setters first. |
| 666 |
|
| 667 |
@c |
| 668 |
void mp_set_decimal_from_int(mp_number *A, integer64 B) { |
| 669 |
decNumber_from_int64(A->data.num,B,&set); |
| 670 |
//decNumberFromInt32(A->data.num,B); |
| 671 |
} |
| 672 |
void mp_set_decimal_from_boolean(mp_number *A, integer64 B) { |
| 673 |
decNumber_from_int64(A->data.num,B,&set); |
| 674 |
//decNumberFromInt32(A->data.num,B); |
| 675 |
} |
| 676 |
void mp_set_decimal_from_scaled(mp_number *A, integer64 B) { |
| 677 |
decNumber c; |
| 678 |
decNumberFromInt32(&c, 65536); |
| 679 |
decNumber_from_int64(A->data.num,B,&set); |
| 680 |
//decNumberFromInt32(A->data.num,B); |
| 681 |
decNumberDivide(A->data.num,A->data.num,&c, &set); |
| 682 |
} |
| 683 |
void mp_set_decimal_from_double(mp_number *A, double B) { |
| 684 |
decNumberFromDouble(A->data.num, B); |
| 685 |
} |
| 686 |
void mp_set_decimal_from_addition(mp_number *A, mp_number B, mp_number C) { |
| 687 |
decNumberAdd(A->data.num,B.data.num,C.data.num, &set); |
| 688 |
} |
| 689 |
void mp_set_decimal_from_substraction (mp_number *A, mp_number B, mp_number C) { |
| 690 |
decNumberSubtract(A->data.num,B.data.num,C.data.num, &set); |
| 691 |
} |
| 692 |
void mp_set_decimal_from_div(mp_number *A, mp_number B, mp_number C) { |
| 693 |
decNumberDivide(A->data.num,B.data.num,C.data.num, &set); |
| 694 |
} |
| 695 |
void mp_set_decimal_from_mul(mp_number *A, mp_number B, mp_number C) { |
| 696 |
decNumberMultiply(A->data.num,B.data.num,C.data.num, &set); |
| 697 |
} |
| 698 |
void mp_set_decimal_from_int_div(mp_number *A, mp_number B, integer64 C) { |
| 699 |
decNumber c; |
| 700 |
decNumber_from_int64(&c,C,&set); |
| 701 |
//decNumberFromInt32(&c, C); |
| 702 |
decNumberDivide(A->data.num,B.data.num,&c, &set); |
| 703 |
} |
| 704 |
void mp_set_decimal_from_int_mul(mp_number *A, mp_number B, integer64 C) { |
| 705 |
decNumber c; |
| 706 |
decNumber_from_int64(&c,C,&set); |
| 707 |
//decNumberFromInt32(&c, C); |
| 708 |
decNumberMultiply(A->data.num,B.data.num,&c, &set); |
| 709 |
} |
| 710 |
void mp_set_decimal_from_of_the_way(MP mp, mp_number *A, mp_number t, mp_number B, mp_number C) { |
| 711 |
decNumber c; |
| 712 |
decNumber r1; |
| 713 |
decNumberSubtract(&c,B.data.num, C.data.num, &set); |
| 714 |
mp_decimal_take_fraction(mp, &r1, &c, t.data.num); |
| 715 |
decNumberSubtract(A->data.num, B.data.num, &r1, &set); |
| 716 |
mp_check_decNumber(mp, A->data.num, &set); |
| 717 |
} |
| 718 |
void mp_number_negate(mp_number *A) { |
| 719 |
decNumberCopyNegate(A->data.num, A->data.num); |
| 720 |
checkZero(A->data.num); |
| 721 |
} |
| 722 |
void mp_number_add(mp_number *A, mp_number B) { |
| 723 |
decNumberAdd(A->data.num,A->data.num,B.data.num, &set); |
| 724 |
} |
| 725 |
void mp_number_substract(mp_number *A, mp_number B) { |
| 726 |
decNumberSubtract(A->data.num,A->data.num,B.data.num, &set); |
| 727 |
} |
| 728 |
void mp_number_half(mp_number *A) { |
| 729 |
decNumber c; |
| 730 |
decNumberFromInt32(&c, 2); |
| 731 |
decNumberDivide(A->data.num,A->data.num, &c, &set); |
| 732 |
} |
| 733 |
void mp_number_halfp(mp_number *A) { |
| 734 |
decNumber c; |
| 735 |
decNumberFromInt32(&c, 2); |
| 736 |
decNumberDivide(A->data.num,A->data.num, &c, &set); |
| 737 |
} |
| 738 |
void mp_number_double(mp_number *A) { |
| 739 |
decNumber c; |
| 740 |
decNumberFromInt32(&c, 2); |
| 741 |
decNumberMultiply(A->data.num,A->data.num, &c, &set); |
| 742 |
} |
| 743 |
void mp_number_add_scaled(mp_number *A, integer64 B) { /* also for negative B */ |
| 744 |
decNumber b,c; |
| 745 |
decNumberFromInt32(&c, 65536); |
| 746 |
decNumber_from_int64(&b,B,&set); |
| 747 |
//decNumberFromInt32(&b, B); |
| 748 |
decNumberDivide(&b,&b, &c, &set); |
| 749 |
decNumberAdd(A->data.num,A->data.num, &b, &set); |
| 750 |
} |
| 751 |
void mp_number_multiply_int(mp_number *A, integer64 B) { |
| 752 |
decNumber b; |
| 753 |
decNumber_from_int64(&b,B,&set); |
| 754 |
//decNumberFromInt32(&b, B); |
| 755 |
decNumberMultiply(A->data.num,A->data.num, &b, &set); |
| 756 |
} |
| 757 |
void mp_number_divide_int(mp_number *A, integer64 B) { |
| 758 |
decNumber b; |
| 759 |
decNumber_from_int64(&b,B,&set); |
| 760 |
//decNumberFromInt32(&b, B); |
| 761 |
decNumberDivide(A->data.num,A->data.num,&b, &set); |
| 762 |
} |
| 763 |
void mp_decimal_abs(mp_number *A) { |
| 764 |
decNumberAbs(A->data.num, A->data.num, &set); |
| 765 |
} |
| 766 |
void mp_number_clone(mp_number *A, mp_number B) { |
| 767 |
decNumberCopy(A->data.num, B.data.num); |
| 768 |
} |
| 769 |
void mp_number_swap(mp_number *A, mp_number *B) { |
| 770 |
decNumber swap_tmp; |
| 771 |
decNumberCopy(&swap_tmp, A->data.num); |
| 772 |
decNumberCopy(A->data.num, B->data.num); |
| 773 |
decNumberCopy(B->data.num, &swap_tmp); |
| 774 |
} |
| 775 |
void mp_number_fraction_to_scaled (mp_number *A) { |
| 776 |
A->type = mp_scaled_type; |
| 777 |
decNumberDivide(A->data.num, A->data.num, &fraction_multiplier_decNumber, &set); |
| 778 |
} |
| 779 |
void mp_number_angle_to_scaled (mp_number *A) { |
| 780 |
A->type = mp_scaled_type; |
| 781 |
decNumberDivide(A->data.num, A->data.num, &angle_multiplier_decNumber, &set); |
| 782 |
} |
| 783 |
void mp_number_scaled_to_fraction (mp_number *A) { |
| 784 |
A->type = mp_fraction_type; |
| 785 |
decNumberMultiply(A->data.num, A->data.num, &fraction_multiplier_decNumber, &set); |
| 786 |
} |
| 787 |
void mp_number_scaled_to_angle (mp_number *A) { |
| 788 |
A->type = mp_angle_type; |
| 789 |
decNumberMultiply(A->data.num, A->data.num, &angle_multiplier_decNumber, &set); |
| 790 |
} |
| 791 |
|
| 792 |
|
| 793 |
@* Query functions. |
| 794 |
|
| 795 |
@ Convert a number to a scaled value. |decNumberToInt32| is not |
| 796 |
able to make this conversion properly, so instead we are using |
| 797 |
|decNumberToDouble| and a typecast. Bad! |
| 798 |
|
| 799 |
@c |
| 800 |
integer64 mp_number_to_scaled(mp_number A) { |
| 801 |
integer64 result; |
| 802 |
decNumber corrected; |
| 803 |
decNumberFromInt32(&corrected, 65536); |
| 804 |
decNumberMultiply(&corrected,&corrected,A.data.num, &set); |
| 805 |
decNumberReduce(&corrected, &corrected, &set); |
| 806 |
result = (integer64)floor(decNumberToDouble(&corrected)+0.5); |
| 807 |
return result; |
| 808 |
} |
| 809 |
|
| 810 |
@ |
| 811 |
|
| 812 |
@d odd(A) (MPOST_ABS(A)%2==1) |
| 813 |
|
| 814 |
@c |
| 815 |
integer64 mp_number_to_int(mp_number A) { |
| 816 |
int32_t result; |
| 817 |
set.status = 0; |
| 818 |
result = decNumberToInt32(A.data.num, &set); |
| 819 |
if (set.status == DEC_Invalid_operation) { |
| 820 |
set.status = 0; |
| 821 |
/* |mp->arith_error = 1;| */ |
| 822 |
return 0; /* whatever */ |
| 823 |
} else { |
| 824 |
return (integer64)result; |
| 825 |
} |
| 826 |
} |
| 827 |
integer64 mp_number_to_boolean(mp_number A) { |
| 828 |
uint32_t result; |
| 829 |
set.status = 0; |
| 830 |
result = decNumberToUInt32(A.data.num, &set); |
| 831 |
if (set.status == DEC_Invalid_operation) { |
| 832 |
set.status = 0; |
| 833 |
/* |mp->arith_error = 1;| */ |
| 834 |
return mp_false_code; /* whatever */ |
| 835 |
} else { |
| 836 |
return (integer64)result ; |
| 837 |
} |
| 838 |
} |
| 839 |
double mp_number_to_double(mp_number A) { |
| 840 |
char *buffer = malloc(((decNumber *)A.data.num)->digits + 14); |
| 841 |
double res = 0.0; |
| 842 |
assert (buffer); |
| 843 |
decNumberToString(A.data.num, buffer); |
| 844 |
if (sscanf(buffer, "%lf", &res)) { |
| 845 |
free(buffer); |
| 846 |
return res; |
| 847 |
} else { |
| 848 |
free(buffer); |
| 849 |
/* |mp->arith_error = 1;| */ |
| 850 |
return 0.0; /* whatever */ |
| 851 |
} |
| 852 |
} |
| 853 |
int mp_number_odd(mp_number A) { |
| 854 |
return odd(mp_number_to_int(A)); |
| 855 |
} |
| 856 |
int mp_number_equal(mp_number A, mp_number B) { |
| 857 |
decNumber res; |
| 858 |
decNumberCompare(&res,A.data.num,B.data.num, &set); |
| 859 |
return decNumberIsZero(&res); |
| 860 |
} |
| 861 |
int mp_number_greater(mp_number A, mp_number B) { |
| 862 |
decNumber res; |
| 863 |
decNumberCompare(&res,A.data.num,B.data.num, &set); |
| 864 |
return decNumberIsPositive(&res); |
| 865 |
} |
| 866 |
int mp_number_less(mp_number A, mp_number B) { |
| 867 |
decNumber res; |
| 868 |
decNumberCompare(&res,A.data.num,B.data.num, &set); |
| 869 |
return decNumberIsNegative(&res); |
| 870 |
} |
| 871 |
int mp_number_nonequalabs(mp_number A, mp_number B) { |
| 872 |
decNumber res, a, b; |
| 873 |
decNumberCopyAbs(&a, A.data.num); |
| 874 |
decNumberCopyAbs(&b, B.data.num); |
| 875 |
decNumberCompare(&res, &a, &b, &set); |
| 876 |
return !decNumberIsZero(&res); |
| 877 |
} |
| 878 |
|
| 879 |
@ Fixed-point arithmetic is done on {\sl scaled integers\/} that are multiples |
| 880 |
of $2^{-16}$. In other words, a binary point is assumed to be sixteen bit |
| 881 |
positions from the right end of a binary computer word. |
| 882 |
|
| 883 |
@ One of \MP's most common operations is the calculation of |
| 884 |
$\lfloor{a+b\over2}\rfloor$, |
| 885 |
the midpoint of two given integers |a| and~|b|. The most decent way to do |
| 886 |
this is to write `|(a+b)/2|'; but on many machines it is more efficient |
| 887 |
to calculate `|(a+b)>>1|'. |
| 888 |
|
| 889 |
Therefore the midpoint operation will always be denoted by `|half(a+b)|' |
| 890 |
in this program. If \MP\ is being implemented with languages that permit |
| 891 |
binary shifting, the |half| macro should be changed to make this operation |
| 892 |
as efficient as possible. Since some systems have shift operators that can |
| 893 |
only be trusted to work on positive numbers, there is also a macro |halfp| |
| 894 |
that is used only when the quantity being halved is known to be positive |
| 895 |
or zero. |
| 896 |
|
| 897 |
@ Here is a procedure analogous to |print_int|. The current version |
| 898 |
is fairly stupid, and it is not round-trip safe, but this is good |
| 899 |
enough for a beta test. |
| 900 |
|
| 901 |
@c |
| 902 |
char * mp_decnumber_tostring (decNumber *n) { |
| 903 |
decNumber corrected; |
| 904 |
char *buffer = malloc(((decNumber *)n)->digits + 14); |
| 905 |
assert (buffer); |
| 906 |
decNumberCopy(&corrected,n); |
| 907 |
decNumberTrim(&corrected); |
| 908 |
decNumberToString(&corrected, buffer); |
| 909 |
return buffer; |
| 910 |
} |
| 911 |
char * mp_decimal_number_tostring (MP mp, mp_number n) { |
| 912 |
(void)mp; |
| 913 |
return mp_decnumber_tostring(n.data.num); |
| 914 |
} |
| 915 |
|
| 916 |
|
| 917 |
@ @c |
| 918 |
void mp_decimal_print_number (MP mp, mp_number n) { |
| 919 |
char *str = mp_decimal_number_tostring(mp, n); |
| 920 |
mp_print (mp, str); |
| 921 |
free (str); |
| 922 |
} |
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
@ Addition is not always checked to make sure that it doesn't overflow, |
| 928 |
but in places where overflow isn't too unlikely the |slow_add| routine |
| 929 |
is used. |
| 930 |
|
| 931 |
@c |
| 932 |
void mp_decimal_slow_add (MP mp, mp_number *ret, mp_number A, mp_number B) { |
| 933 |
(void)mp; |
| 934 |
decNumberAdd(ret->data.num,A.data.num,B.data.num, &set); |
| 935 |
} |
| 936 |
|
| 937 |
@ The |make_fraction| routine produces the |fraction| equivalent of |
| 938 |
|p/q|, given integers |p| and~|q|; it computes the integer |
| 939 |
$f=\lfloor2^{28}p/q+{1\over2}\rfloor$, when $p$ and $q$ are |
| 940 |
positive. If |p| and |q| are both of the same scaled type |t|, |
| 941 |
the ``type relation'' |make_fraction(t,t)=fraction| is valid; |
| 942 |
and it's also possible to use the subroutine ``backwards,'' using |
| 943 |
the relation |make_fraction(t,fraction)=t| between scaled types. |
| 944 |
|
| 945 |
If the result would have magnitude $2^{31}$ or more, |make_fraction| |
| 946 |
sets |arith_error:=true|. Most of \MP's internal computations have |
| 947 |
been designed to avoid this sort of error. |
| 948 |
|
| 949 |
If this subroutine were programmed in assembly language on a typical |
| 950 |
machine, we could simply compute |(@t$2^{28}$@>*p)div q|, since a |
| 951 |
double-precision product can often be input to a fixed-point division |
| 952 |
instruction. But when we are restricted to int-eger arithmetic it |
| 953 |
is necessary either to resort to multiple-precision maneuvering |
| 954 |
or to use a simple but slow iteration. The multiple-precision technique |
| 955 |
would be about three times faster than the code adopted here, but it |
| 956 |
would be comparatively long and tricky, involving about sixteen |
| 957 |
additional multiplications and divisions. |
| 958 |
|
| 959 |
This operation is part of \MP's ``inner loop''; indeed, it will |
| 960 |
consume nearly 10\pct! of the running time (exclusive of input and output) |
| 961 |
if the code below is left unchanged. A machine-dependent recoding |
| 962 |
will therefore make \MP\ run faster. The present implementation |
| 963 |
is highly portable, but slow; it avoids multiplication and division |
| 964 |
except in the initial stage. System wizards should be careful to |
| 965 |
replace it with a routine that is guaranteed to produce identical |
| 966 |
results in all cases. |
| 967 |
@^system dependencies@> |
| 968 |
|
| 969 |
As noted below, a few more routines should also be replaced by machine-dependent |
| 970 |
code, for efficiency. But when a procedure is not part of the ``inner loop,'' |
| 971 |
such changes aren't advisable; simplicity and robustness are |
| 972 |
preferable to trickery, unless the cost is too high. |
| 973 |
@^inner loop@> |
| 974 |
|
| 975 |
@c |
| 976 |
void mp_decimal_make_fraction (MP mp, decNumber *ret, decNumber *p, decNumber *q) { |
| 977 |
decNumberDivide(ret, p, q, &set); |
| 978 |
mp_check_decNumber(mp, ret, &set); |
| 979 |
decNumberMultiply(ret, ret, &fraction_multiplier_decNumber, &set); |
| 980 |
} |
| 981 |
void mp_decimal_number_make_fraction (MP mp, mp_number *ret, mp_number p, mp_number q) { |
| 982 |
mp_decimal_make_fraction (mp, ret->data.num, p.data.num, q.data.num); |
| 983 |
} |
| 984 |
|
| 985 |
@ @<Declarations@>= |
| 986 |
void mp_decimal_make_fraction (MP mp, decNumber *ret, decNumber *p, decNumber *q); |
| 987 |
|
| 988 |
@ The dual of |make_fraction| is |take_fraction|, which multiplies a |
| 989 |
given integer~|q| by a fraction~|f|. When the operands are positive, it |
| 990 |
computes $p=\lfloor qf/2^{28}+{1\over2}\rfloor$, a symmetric function |
| 991 |
of |q| and~|f|. |
| 992 |
|
| 993 |
This routine is even more ``inner loopy'' than |make_fraction|; |
| 994 |
the present implementation consumes almost 20\pct! of \MP's computation |
| 995 |
time during typical jobs, so a machine-language substitute is advisable. |
| 996 |
@^inner loop@> @^system dependencies@> |
| 997 |
|
| 998 |
@c |
| 999 |
void mp_decimal_take_fraction (MP mp, decNumber *ret, decNumber *p, decNumber *q) { |
| 1000 |
(void)mp; |
| 1001 |
decNumberMultiply(ret, p, q, &set); |
| 1002 |
decNumberDivide(ret, ret, &fraction_multiplier_decNumber, &set); |
| 1003 |
} |
| 1004 |
void mp_decimal_number_take_fraction (MP mp, mp_number *ret, mp_number p, mp_number q) { |
| 1005 |
mp_decimal_take_fraction (mp, ret->data.num, p.data.num, q.data.num); |
| 1006 |
} |
| 1007 |
|
| 1008 |
@ @<Declarations@>= |
| 1009 |
void mp_decimal_take_fraction (MP mp, decNumber *ret, decNumber *p, decNumber *q); |
| 1010 |
|
| 1011 |
@ When we want to multiply something by a |scaled| quantity, we use a scheme |
| 1012 |
analogous to |take_fraction| but with a different scaling. |
| 1013 |
Given positive operands, |take_scaled| |
| 1014 |
computes the quantity $p=\lfloor qf/2^{16}+{1\over2}\rfloor$. |
| 1015 |
|
| 1016 |
Once again it is a good idea to use a machine-language replacement if |
| 1017 |
possible; otherwise |take_scaled| will use more than 2\pct! of the running time |
| 1018 |
when the Computer Modern fonts are being generated. |
| 1019 |
@^inner loop@> |
| 1020 |
|
| 1021 |
@c |
| 1022 |
void mp_decimal_number_take_scaled (MP mp, mp_number *ret, mp_number p_orig, mp_number q_orig) { |
| 1023 |
(void)mp; |
| 1024 |
decNumberMultiply(ret->data.num, p_orig.data.num, q_orig.data.num, &set); |
| 1025 |
} |
| 1026 |
|
| 1027 |
|
| 1028 |
@ For completeness, there's also |make_scaled|, which computes a |
| 1029 |
quotient as a |scaled| number instead of as a |fraction|. |
| 1030 |
In other words, the result is $\lfloor2^{16}p/q+{1\over2}\rfloor$, if the |
| 1031 |
operands are positive. \ (This procedure is not used especially often, |
| 1032 |
so it is not part of \MP's inner loop.) |
| 1033 |
|
| 1034 |
@c |
| 1035 |
void mp_decimal_number_make_scaled (MP mp, mp_number *ret, mp_number p_orig, mp_number q_orig) { |
| 1036 |
decNumberDivide(ret->data.num, p_orig.data.num, q_orig.data.num, &set); |
| 1037 |
mp_check_decNumber(mp, ret->data.num, &set); |
| 1038 |
} |
| 1039 |
|
| 1040 |
@ |
| 1041 |
@d halfp(A) (integer)((unsigned)(A) >> 1) |
| 1042 |
|
| 1043 |
@* Scanning numbers in the input. |
| 1044 |
|
| 1045 |
The definitions below are temporarily here |
| 1046 |
|
| 1047 |
@d set_cur_cmd(A) mp->cur_mod_->type=(A) |
| 1048 |
@d set_cur_mod(A) decNumberCopy((decNumber *)(mp->cur_mod_->data.n.data.num),&A) |
| 1049 |
|
| 1050 |
@<Declarations...@>= |
| 1051 |
static void mp_wrapup_numeric_token(MP mp, unsigned char *start, unsigned char *stop); |
| 1052 |
|
| 1053 |
@ |
| 1054 |
@d too_precise(a) (a == (DEC_Inexact+DEC_Rounded)) |
| 1055 |
@d too_large(a) (a & DEC_Overflow) |
| 1056 |
@c |
| 1057 |
void mp_wrapup_numeric_token(MP mp, unsigned char *start, unsigned char *stop) { |
| 1058 |
decNumber result; |
| 1059 |
size_t l ; |
| 1060 |
char *buf ; |
| 1061 |
|
| 1062 |
if ((stop-start+1)<0) { |
| 1063 |
const char *hlp[] = {"I could not handle this number specification", |
| 1064 |
"because an error of the internal buffer.", |
| 1065 |
NULL }; |
| 1066 |
mp_error (mp, "Internal buffer error", hlp, false); |
| 1067 |
} |
| 1068 |
l = (size_t)(stop-start+1); |
| 1069 |
buf = mp_xmalloc(mp, l+1, 1); |
| 1070 |
buf[l] = '\0'; |
| 1071 |
(void)strncpy(buf,(const char *)start, l); |
| 1072 |
set.status = 0; |
| 1073 |
decNumberFromString(&result,buf, &set); |
| 1074 |
free(buf); |
| 1075 |
if (set.status == 0) { |
| 1076 |
set_cur_mod(result); |
| 1077 |
} else if (mp->scanner_status != tex_flushing) { |
| 1078 |
if (too_large(set.status)) { |
| 1079 |
const char *hlp[] = {"I could not handle this number specification", |
| 1080 |
"because it is out of range.", |
| 1081 |
NULL }; |
| 1082 |
decNumber_check (&result, &set); |
| 1083 |
set_cur_mod(result); |
| 1084 |
mp_error (mp, "Enormous number has been reduced", hlp, false); |
| 1085 |
} else if (too_precise(set.status)) { |
| 1086 |
set_cur_mod(result); |
| 1087 |
if (decNumberIsPositive((decNumber *)internal_value (mp_warning_check).data.num) && |
| 1088 |
(mp->scanner_status != tex_flushing)) { |
| 1089 |
char msg[256]; |
| 1090 |
const char *hlp[] = {"Continue and I'll round the value until it fits the current numberprecision", |
| 1091 |
"(Set warningcheck:=0 to suppress this message.)", |
| 1092 |
NULL }; |
| 1093 |
mp_snprintf (msg, 256, "Number is too precise (numberprecision = %d)", set.digits); |
| 1094 |
mp_error (mp, msg, hlp, true); |
| 1095 |
} |
| 1096 |
} else { /* this also captures underflow */ |
| 1097 |
const char *hlp[] = {"I could not handle this number specification", |
| 1098 |
"Error:", |
| 1099 |
"", |
| 1100 |
NULL }; |
| 1101 |
hlp[2] = decContextStatusToString(&set); |
| 1102 |
mp_error (mp, "Erroneous number specification changed to zero", hlp, false); |
| 1103 |
decNumberZero(&result); |
| 1104 |
set_cur_mod(result); |
| 1105 |
} |
| 1106 |
} |
| 1107 |
set_cur_cmd((mp_variable_type)mp_numeric_token); |
| 1108 |
} |
| 1109 |
|
| 1110 |
@ @c |
| 1111 |
static void find_exponent (MP mp) { |
| 1112 |
if (mp->buffer[mp->cur_input.loc_field] == 'e' || |
| 1113 |
mp->buffer[mp->cur_input.loc_field] == 'E') { |
| 1114 |
mp->cur_input.loc_field++; |
| 1115 |
if (!(mp->buffer[mp->cur_input.loc_field] == '+' || |
| 1116 |
mp->buffer[mp->cur_input.loc_field] == '-' || |
| 1117 |
mp->char_class[mp->buffer[mp->cur_input.loc_field]] == digit_class)) { |
| 1118 |
mp->cur_input.loc_field--; |
| 1119 |
return; |
| 1120 |
} |
| 1121 |
if (mp->buffer[mp->cur_input.loc_field] == '+' || |
| 1122 |
mp->buffer[mp->cur_input.loc_field] == '-') { |
| 1123 |
mp->cur_input.loc_field++; |
| 1124 |
} |
| 1125 |
while (mp->char_class[mp->buffer[mp->cur_input.loc_field]] == digit_class) { |
| 1126 |
mp->cur_input.loc_field++; |
| 1127 |
} |
| 1128 |
} |
| 1129 |
} |
| 1130 |
void mp_decimal_scan_fractional_token (MP mp, integer64 n) { /* n: scaled */ |
| 1131 |
unsigned char *start = &mp->buffer[mp->cur_input.loc_field -1]; |
| 1132 |
unsigned char *stop; |
| 1133 |
(void)n; |
| 1134 |
while (mp->char_class[mp->buffer[mp->cur_input.loc_field]] == digit_class) { |
| 1135 |
mp->cur_input.loc_field++; |
| 1136 |
} |
| 1137 |
find_exponent(mp); |
| 1138 |
stop = &mp->buffer[mp->cur_input.loc_field-1]; |
| 1139 |
mp_wrapup_numeric_token (mp, start, stop); |
| 1140 |
} |
| 1141 |
|
| 1142 |
|
| 1143 |
@ We just have to collect bytes. |
| 1144 |
|
| 1145 |
@c |
| 1146 |
void mp_decimal_scan_numeric_token (MP mp, integer64 n) { /* n: scaled */ |
| 1147 |
unsigned char *start = &mp->buffer[mp->cur_input.loc_field -1]; |
| 1148 |
unsigned char *stop; |
| 1149 |
(void)n; |
| 1150 |
while (mp->char_class[mp->buffer[mp->cur_input.loc_field]] == digit_class) { |
| 1151 |
mp->cur_input.loc_field++; |
| 1152 |
} |
| 1153 |
if (mp->buffer[mp->cur_input.loc_field] == '.' && |
| 1154 |
mp->buffer[mp->cur_input.loc_field+1] != '.') { |
| 1155 |
mp->cur_input.loc_field++; |
| 1156 |
while (mp->char_class[mp->buffer[mp->cur_input.loc_field]] == digit_class) { |
| 1157 |
mp->cur_input.loc_field++; |
| 1158 |
} |
| 1159 |
} |
| 1160 |
find_exponent(mp); |
| 1161 |
stop = &mp->buffer[mp->cur_input.loc_field-1]; |
| 1162 |
mp_wrapup_numeric_token (mp, start, stop); |
| 1163 |
} |
| 1164 |
|
| 1165 |
@ The |scaled| quantities in \MP\ programs are generally supposed to be |
| 1166 |
less than $2^{12}$ in absolute value, so \MP\ does much of its internal |
| 1167 |
arithmetic with 28~significant bits of precision. A |fraction| denotes |
| 1168 |
a scaled integer whose binary point is assumed to be 28 bit positions |
| 1169 |
from the right. |
| 1170 |
|
| 1171 |
@d fraction_half (fraction_multiplier/2) |
| 1172 |
@d fraction_one (1*fraction_multiplier) |
| 1173 |
@d fraction_two (2*fraction_multiplier) |
| 1174 |
@d fraction_three (3*fraction_multiplier) |
| 1175 |
@d fraction_four (4*fraction_multiplier) |
| 1176 |
|
| 1177 |
@ Here is a typical example of how the routines above can be used. |
| 1178 |
It computes the function |
| 1179 |
$${1\over3\tau}f(\theta,\phi)= |
| 1180 |
{\tau^{-1}\bigl(2+\sqrt2\,(\sin\theta-{1\over16}\sin\phi) |
| 1181 |
(\sin\phi-{1\over16}\sin\theta)(\cos\theta-\cos\phi)\bigr)\over |
| 1182 |
3\,\bigl(1+{1\over2}(\sqrt5-1)\cos\theta+{1\over2}(3-\sqrt5\,)\cos\phi\bigr)},$$ |
| 1183 |
where $\tau$ is a |scaled| ``tension'' parameter. This is \MP's magic |
| 1184 |
fudge factor for placing the first control point of a curve that starts |
| 1185 |
at an angle $\theta$ and ends at an angle $\phi$ from the straight path. |
| 1186 |
(Actually, if the stated quantity exceeds 4, \MP\ reduces it to~4.) |
| 1187 |
|
| 1188 |
The trigonometric quantity to be multiplied by $\sqrt2$ is less than $\sqrt2$. |
| 1189 |
(It's a sum of eight terms whose absolute values can be bounded using |
| 1190 |
relations such as $\sin\theta\cos\theta\L{1\over2}$.) Thus the numerator |
| 1191 |
is positive; and since the tension $\tau$ is constrained to be at least |
| 1192 |
$3\over4$, the numerator is less than $16\over3$. The denominator is |
| 1193 |
nonnegative and at most~6. |
| 1194 |
|
| 1195 |
The angles $\theta$ and $\phi$ are given implicitly in terms of |fraction| |
| 1196 |
arguments |st|, |ct|, |sf|, and |cf|, representing $\sin\theta$, $\cos\theta$, |
| 1197 |
$\sin\phi$, and $\cos\phi$, respectively. |
| 1198 |
|
| 1199 |
@c |
| 1200 |
void mp_decimal_velocity (MP mp, mp_number *ret, mp_number st, mp_number ct, mp_number sf, |
| 1201 |
mp_number cf, mp_number t) { |
| 1202 |
decNumber acc, num, denom; /* registers for intermediate calculations */ |
| 1203 |
decNumber r1, r2; |
| 1204 |
decNumber arg1, arg2; |
| 1205 |
decNumber i16, fone, fhalf, ftwo, sqrtfive; |
| 1206 |
decNumberFromInt32(&i16, 16); |
| 1207 |
decNumberFromInt32(&fone, fraction_one); |
| 1208 |
decNumberFromInt32(&fhalf, fraction_half); |
| 1209 |
decNumberFromInt32(&ftwo, fraction_two); |
| 1210 |
decNumberFromInt32(&sqrtfive, 5); /*$\sqrt{5}$*/ |
| 1211 |
decNumberSquareRoot(&sqrtfive, &sqrtfive, &set); |
| 1212 |
|
| 1213 |
|
| 1214 |
decNumberDivide(&arg1,sf.data.num, &i16, &set); /* arg1 = sf / 16*/ |
| 1215 |
decNumberSubtract(&arg1,st.data.num,&arg1, &set); /* arg1 = st - arg1*/ |
| 1216 |
decNumberDivide(&arg2,st.data.num, &i16, &set); /* arg2 = st / 16*/ |
| 1217 |
decNumberSubtract(&arg2,sf.data.num,&arg2, &set); /* arg2 = sf - arg2*/ |
| 1218 |
mp_decimal_take_fraction (mp, &acc, &arg1, &arg2); /* acc = (arg1 * arg2) / fmul*/ |
| 1219 |
|
| 1220 |
decNumberCopy(&arg1, &acc); |
| 1221 |
decNumberSubtract(&arg2, ct.data.num, cf.data.num, &set); /* arg2 = ct - cf*/ |
| 1222 |
mp_decimal_take_fraction (mp, &acc, &arg1, &arg2); /* acc = (arg1 * arg2 ) / fmul*/ |
| 1223 |
|
| 1224 |
decNumberSquareRoot(&arg1, &two_decNumber, &set); /* arg1 = $\sqrt{2}$*/ |
| 1225 |
decNumberMultiply(&arg1, &arg1, &fone, &set); /* arg1 = arg1 * fmul*/ |
| 1226 |
mp_decimal_take_fraction (mp, &r1, &acc, &arg1); /* r1 = (acc * arg1) / fmul*/ |
| 1227 |
decNumberAdd(&num, &ftwo, &r1, &set); /* num = ftwo + r1*/ |
| 1228 |
|
| 1229 |
decNumberSubtract(&arg1,&sqrtfive, &one, &set); /* arg1 = $\sqrt{5}$ - 1*/ |
| 1230 |
decNumberMultiply(&arg1,&arg1,&fhalf, &set); /* arg1 = arg1 * fmul/2*/ |
| 1231 |
decNumberMultiply(&arg1,&arg1,&three_decNumber, &set); /* arg1 = arg1 * 3*/ |
| 1232 |
|
| 1233 |
decNumberSubtract(&arg2,&three_decNumber, &sqrtfive, &set); /* arg2 = 3 - $\sqrt{5}$*/ |
| 1234 |
decNumberMultiply(&arg2,&arg2,&fhalf, &set); /* arg2 = arg2 * fmul/2*/ |
| 1235 |
decNumberMultiply(&arg2,&arg2,&three_decNumber, &set); /* arg2 = arg2 * 3*/ |
| 1236 |
mp_decimal_take_fraction (mp, &r1, ct.data.num, &arg1) ; /* r1 = (ct * arg1) / fmul*/ |
| 1237 |
mp_decimal_take_fraction (mp, &r2, cf.data.num, &arg2); /* r2 = (cf * arg2) / fmul*/ |
| 1238 |
|
| 1239 |
decNumberFromInt32(&denom, fraction_three); /* denom = 3fmul*/ |
| 1240 |
decNumberAdd(&denom, &denom, &r1, &set); /* denom = denom + r1*/ |
| 1241 |
decNumberAdd(&denom, &denom, &r2, &set); /* denom = denom + r1*/ |
| 1242 |
|
| 1243 |
decNumberCompare(&arg1, t.data.num, &one, &set); |
| 1244 |
if (!decNumberIsZero(&arg1)) { /* t != r1*/ |
| 1245 |
decNumberDivide(&num, &num, t.data.num, &set); /* num = num / t*/ |
| 1246 |
} |
| 1247 |
decNumberCopy(&r2, &num); /* r2 = num / 4*/ |
| 1248 |
decNumberDivide(&r2, &r2, &four_decNumber, &set); |
| 1249 |
if (decNumberLess(&denom,&r2)) { /* num/4 >= denom => denom < num/4*/ |
| 1250 |
decNumberFromInt32(ret->data.num,fraction_four); |
| 1251 |
} else { |
| 1252 |
mp_decimal_make_fraction (mp, ret->data.num, &num, &denom); |
| 1253 |
} |
| 1254 |
#if MPOST_DEBUG |
| 1255 |
fprintf(stdout, "\n%f = velocity(%f,%f,%f,%f,%f)", mp_number_to_double(*ret), |
| 1256 |
mp_number_to_double(st),mp_number_to_double(ct), |
| 1257 |
mp_number_to_double(sf),mp_number_to_double(cf), |
| 1258 |
mp_number_to_double(t)); |
| 1259 |
#endif |
| 1260 |
mp_check_decNumber(mp, ret->data.num, &set); |
| 1261 |
} |
| 1262 |
|
| 1263 |
|
| 1264 |
@ The following somewhat different subroutine tests rigorously if $ab$ is |
| 1265 |
greater than, equal to, or less than~$cd$, |
| 1266 |
given integers $(a,b,c,d)$. In most cases a quick decision is reached. |
| 1267 |
The result is $+1$, 0, or~$-1$ in the three respective cases. |
| 1268 |
|
| 1269 |
@c |
| 1270 |
void mp_ab_vs_cd (MP mp, mp_number *ret, mp_number a_orig, mp_number b_orig, mp_number c_orig, mp_number d_orig) { |
| 1271 |
decNumber q, r, test; /* temporary registers */ |
| 1272 |
decNumber a, b, c, d; |
| 1273 |
decNumber ab, cd; |
| 1274 |
(void)mp; |
| 1275 |
decNumberCopy(&a, (decNumber *)a_orig.data.num); |
| 1276 |
decNumberCopy(&b, (decNumber *)b_orig.data.num); |
| 1277 |
decNumberCopy(&c, (decNumber *)c_orig.data.num); |
| 1278 |
decNumberCopy(&d, (decNumber *)d_orig.data.num); |
| 1279 |
|
| 1280 |
decNumberMultiply (&ab, (decNumber *)a_orig.data.num, (decNumber *)b_orig.data.num, &set); |
| 1281 |
decNumberMultiply (&cd, (decNumber *)c_orig.data.num, (decNumber *)d_orig.data.num, &set); |
| 1282 |
decNumberCompare(ret->data.num, &ab, &cd, &set); |
| 1283 |
mp_check_decNumber(mp, ret->data.num, &set); |
| 1284 |
if (1>0) |
| 1285 |
return; |
| 1286 |
|
| 1287 |
|
| 1288 |
@<Reduce to the case that |a,c>=0|, |b,d>0|@>; |
| 1289 |
while (1) { |
| 1290 |
decNumberDivide(&q,&a,&d, &set); |
| 1291 |
decNumberDivide(&r,&c,&b, &set); |
| 1292 |
decNumberCompare(&test,&q,&r, &set); |
| 1293 |
if (!decNumberIsZero(&test)) { |
| 1294 |
if (decNumberIsPositive(&test)) { |
| 1295 |
decNumberCopy(ret->data.num, &one); |
| 1296 |
} else { |
| 1297 |
decNumberCopy(ret->data.num, &minusone); |
| 1298 |
} |
| 1299 |
goto RETURN; |
| 1300 |
} |
| 1301 |
decNumberRemainder(&q,&a,&d, &set); |
| 1302 |
decNumberRemainder(&r,&c,&b, &set); |
| 1303 |
if (decNumberIsZero(&r)) { |
| 1304 |
if (decNumberIsZero(&q)) { |
| 1305 |
decNumberCopy(ret->data.num, &zero); |
| 1306 |
} else { |
| 1307 |
decNumberCopy(ret->data.num, &one); |
| 1308 |
} |
| 1309 |
goto RETURN; |
| 1310 |
} |
| 1311 |
if (decNumberIsZero(&q)) { |
| 1312 |
decNumberCopy(ret->data.num, &minusone); |
| 1313 |
goto RETURN; |
| 1314 |
} |
| 1315 |
decNumberCopy(&a,&b); |
| 1316 |
decNumberCopy(&b,&q); |
| 1317 |
decNumberCopy(&c,&d); |
| 1318 |
decNumberCopy(&d,&r); |
| 1319 |
} /* now |a>d>0| and |c>b>0| */ |
| 1320 |
RETURN: |
| 1321 |
#if MPOST_DEBUG |
| 1322 |
fprintf(stdout, "\n%f = ab_vs_cd(%f,%f,%f,%f)", mp_number_to_double(*ret), |
| 1323 |
mp_number_to_double(a_orig),mp_number_to_double(b_orig), |
| 1324 |
mp_number_to_double(c_orig),mp_number_to_double(d_orig)); |
| 1325 |
#endif |
| 1326 |
mp_check_decNumber(mp, ret->data.num, &set); |
| 1327 |
return; |
| 1328 |
} |
| 1329 |
|
| 1330 |
|
| 1331 |
@ @<Reduce to the case that |a...@>= |
| 1332 |
if (decNumberIsNegative(&a)) { |
| 1333 |
decNumberCopyNegate(&a, &a); |
| 1334 |
decNumberCopyNegate(&b, &b); |
| 1335 |
} |
| 1336 |
if (decNumberIsNegative(&c)) { |
| 1337 |
decNumberCopyNegate(&c, &c); |
| 1338 |
decNumberCopyNegate(&d, &d); |
| 1339 |
} |
| 1340 |
if (!decNumberIsPositive(&d)) { |
| 1341 |
if (!decNumberIsNegative(&b)) { |
| 1342 |
if ((decNumberIsZero(&a) || decNumberIsZero(&b)) && (decNumberIsZero(&c) || decNumberIsZero(&d))) |
| 1343 |
decNumberCopy(ret->data.num, &zero); |
| 1344 |
else |
| 1345 |
decNumberCopy(ret->data.num, &one); |
| 1346 |
goto RETURN; |
| 1347 |
} |
| 1348 |
if (decNumberIsZero(&d)) { |
| 1349 |
if (decNumberIsZero(&a)) |
| 1350 |
decNumberCopy(ret->data.num, &zero); |
| 1351 |
else |
| 1352 |
decNumberCopy(ret->data.num, &minusone); |
| 1353 |
goto RETURN; |
| 1354 |
} |
| 1355 |
decNumberCopy(&q, &a); |
| 1356 |
decNumberCopy(&a, &c); |
| 1357 |
decNumberCopy(&c, &q); |
| 1358 |
decNumberCopyNegate(&q, &b); |
| 1359 |
decNumberCopyNegate(&b, &d); |
| 1360 |
decNumberCopy(&d, &q); |
| 1361 |
} else if (!decNumberIsPositive(&b)) { |
| 1362 |
if (decNumberIsNegative(&b) && decNumberIsPositive(&a)) { |
| 1363 |
decNumberCopy(ret->data.num, &minusone); |
| 1364 |
goto RETURN; |
| 1365 |
} |
| 1366 |
if (decNumberIsZero(&c)) |
| 1367 |
decNumberCopy(ret->data.num, &zero); |
| 1368 |
else |
| 1369 |
decNumberCopy(ret->data.num, &minusone); |
| 1370 |
goto RETURN; |
| 1371 |
} |
| 1372 |
|
| 1373 |
@ Now here's a subroutine that's handy for all sorts of path computations: |
| 1374 |
Given a quadratic polynomial $B(a,b,c;t)$, the |crossing_point| function |
| 1375 |
returns the unique |fraction| value |t| between 0 and~1 at which |
| 1376 |
$B(a,b,c;t)$ changes from positive to negative, or returns |
| 1377 |
|t=fraction_one+1| if no such value exists. If |a<0| (so that $B(a,b,c;t)$ |
| 1378 |
is already negative at |t=0|), |crossing_point| returns the value zero. |
| 1379 |
|
| 1380 |
The general bisection method is quite simple when $n=2$, hence |
| 1381 |
|crossing_point| does not take much time. At each stage in the |
| 1382 |
recursion we have a subinterval defined by |l| and~|j| such that |
| 1383 |
$B(a,b,c;2^{-l}(j+t))=B(x_0,x_1,x_2;t)$, and we want to ``zero in'' on |
| 1384 |
the subinterval where $x_0\G0$ and $\min(x_1,x_2)<0$. |
| 1385 |
|
| 1386 |
It is convenient for purposes of calculation to combine the values |
| 1387 |
of |l| and~|j| in a single variable $d=2^l+j$, because the operation |
| 1388 |
of bisection then corresponds simply to doubling $d$ and possibly |
| 1389 |
adding~1. Furthermore it proves to be convenient to modify |
| 1390 |
our previous conventions for bisection slightly, maintaining the |
| 1391 |
variables $X_0=2^lx_0$, $X_1=2^l(x_0-x_1)$, and $X_2=2^l(x_1-x_2)$. |
| 1392 |
With these variables the conditions $x_0\ge0$ and $\min(x_1,x_2)<0$ are |
| 1393 |
equivalent to $\max(X_1,X_1+X_2)>X_0\ge0$. |
| 1394 |
|
| 1395 |
The following code maintains the invariant relations |
| 1396 |
$0\L|x0|<\max(|x1|,|x1|+|x2|)$, |
| 1397 |
$\vert|x1|\vert<2^{30}$, $\vert|x2|\vert<2^{30}$; |
| 1398 |
it has been constructed in such a way that no arithmetic overflow |
| 1399 |
will occur if the inputs satisfy |
| 1400 |
$a<2^{30}$, $\vert a-b\vert<2^{30}$, and $\vert b-c\vert<2^{30}$. |
| 1401 |
|
| 1402 |
@d no_crossing { decNumberCopy(ret->data.num, &fraction_one_plus_decNumber); goto RETURN; } |
| 1403 |
@d one_crossing { decNumberCopy(ret->data.num, &fraction_one_decNumber); goto RETURN; } |
| 1404 |
@d zero_crossing { decNumberCopy(ret->data.num, &zero); goto RETURN; } |
| 1405 |
|
| 1406 |
@c |
| 1407 |
static void mp_decimal_crossing_point (MP mp, mp_number *ret, mp_number aa, mp_number bb, mp_number cc) { |
| 1408 |
decNumber a,b,c; |
| 1409 |
double d; /* recursive counter */ |
| 1410 |
decNumber x, xx, x0, x1, x2; /* temporary registers for bisection */ |
| 1411 |
decNumber scratch, scratch2; |
| 1412 |
decNumberCopy(&a, (decNumber *)aa.data.num); |
| 1413 |
decNumberCopy(&b, (decNumber *)bb.data.num); |
| 1414 |
decNumberCopy(&c, (decNumber *)cc.data.num); |
| 1415 |
if (decNumberIsNegative(&a)) |
| 1416 |
zero_crossing; |
| 1417 |
if (!decNumberIsNegative(&c)) { |
| 1418 |
if (!decNumberIsNegative(&b)) { |
| 1419 |
if (decNumberIsPositive(&c)) { |
| 1420 |
no_crossing; |
| 1421 |
} else if (decNumberIsZero(&a) && decNumberIsZero(&b)) { |
| 1422 |
no_crossing; |
| 1423 |
} else { |
| 1424 |
one_crossing; |
| 1425 |
} |
| 1426 |
} |
| 1427 |
if (decNumberIsZero(&a)) |
| 1428 |
zero_crossing; |
| 1429 |
} else if (decNumberIsZero(&a)) { |
| 1430 |
if (!decNumberIsPositive(&b)) |
| 1431 |
zero_crossing; |
| 1432 |
} |
| 1433 |
|
| 1434 |
/* Use bisection to find the crossing point... */ |
| 1435 |
d = epsilonf; |
| 1436 |
decNumberCopy(&x0, &a); |
| 1437 |
decNumberSubtract(&x1,&a, &b, &set); |
| 1438 |
decNumberSubtract(&x2,&b, &c, &set); |
| 1439 |
/* not sure why the error correction has to be >= 1E-12 */ |
| 1440 |
decNumberFromDouble(&scratch2, 1E-12); |
| 1441 |
do { |
| 1442 |
decNumberAdd(&x, &x1, &x2, &set); |
| 1443 |
decNumberDivide(&x, &x, &two_decNumber, &set); |
| 1444 |
decNumberAdd(&x, &x, &scratch2, &set); |
| 1445 |
decNumberSubtract(&scratch, &x1, &x0, &set); |
| 1446 |
if (decNumberGreater(&scratch, &x0)) { |
| 1447 |
decNumberCopy(&x2, &x); |
| 1448 |
decNumberAdd(&x0, &x0, &x0, &set); |
| 1449 |
d += d; |
| 1450 |
} else { |
| 1451 |
decNumberAdd(&xx, &scratch, &x, &set); |
| 1452 |
if (decNumberGreater(&xx,&x0)) { |
| 1453 |
decNumberCopy(&x2,&x); |
| 1454 |
decNumberAdd(&x0, &x0, &x0, &set); |
| 1455 |
d += d; |
| 1456 |
} else { |
| 1457 |
decNumberSubtract(&x0, &x0, &xx, &set); |
| 1458 |
if (!decNumberGreater(&x,&x0)) { |
| 1459 |
decNumberAdd(&scratch, &x, &x2, &set); |
| 1460 |
if (!decNumberGreater(&scratch, &x0)) |
| 1461 |
no_crossing; |
| 1462 |
} |
| 1463 |
decNumberCopy(&x1,&x); |
| 1464 |
d = d + d + epsilonf; |
| 1465 |
} |
| 1466 |
} |
| 1467 |
} while (d < fraction_one); |
| 1468 |
decNumberFromDouble(&scratch, d); |
| 1469 |
decNumberSubtract(ret->data.num,&scratch, &fraction_one_decNumber, &set); |
| 1470 |
RETURN: |
| 1471 |
#if MPOST_DEBUG |
| 1472 |
fprintf(stdout, "\n%f = crossing_point(%f,%f,%f)", mp_number_to_double(*ret), |
| 1473 |
mp_number_to_double(aa),mp_number_to_double(bb),mp_number_to_double(cc)); |
| 1474 |
#endif |
| 1475 |
mp_check_decNumber(mp, ret->data.num, &set); |
| 1476 |
return; |
| 1477 |
} |
| 1478 |
|
| 1479 |
|
| 1480 |
@ We conclude this set of elementary routines with some simple rounding |
| 1481 |
and truncation operations. |
| 1482 |
|
| 1483 |
|
| 1484 |
@ |round_unscaled| rounds a |scaled| and converts it to |int| |
| 1485 |
@c |
| 1486 |
integer64 mp_round_unscaled(mp_number x_orig) { |
| 1487 |
double xx = mp_number_to_double(x_orig); |
| 1488 |
integer64 x = (integer64)ROUND(xx); |
| 1489 |
return x; |
| 1490 |
} |
| 1491 |
|
| 1492 |
@ |number_floor| floors a number |
| 1493 |
|
| 1494 |
@c |
| 1495 |
void mp_number_floor (mp_number *i) { |
| 1496 |
int round = set.round; |
| 1497 |
set.round = DEC_ROUND_FLOOR; |
| 1498 |
decNumberToIntegralValue(i->data.num, i->data.num, &set); |
| 1499 |
set.round = round; |
| 1500 |
} |
| 1501 |
|
| 1502 |
@ |fraction_to_scaled| rounds a |fraction| and converts it to |scaled| |
| 1503 |
@c |
| 1504 |
void mp_decimal_fraction_to_round_scaled (mp_number *x_orig) { |
| 1505 |
x_orig->type = mp_scaled_type; |
| 1506 |
decNumberDivide(x_orig->data.num, x_orig->data.num, &fraction_multiplier_decNumber, &set); |
| 1507 |
} |
| 1508 |
|
| 1509 |
|
| 1510 |
|
| 1511 |
@* Algebraic and transcendental functions. |
| 1512 |
\MP\ computes all of the necessary special functions from scratch, without |
| 1513 |
relying on |real| arithmetic or system subroutines for sines, cosines, etc. |
| 1514 |
|
| 1515 |
@ |
| 1516 |
|
| 1517 |
@c |
| 1518 |
void mp_decimal_square_rt (MP mp, mp_number *ret, mp_number x_orig) { /* return, x: scaled */ |
| 1519 |
decNumber x; |
| 1520 |
decNumberCopy(&x, x_orig.data.num); |
| 1521 |
if (!decNumberIsPositive(&x)) { |
| 1522 |
@<Handle square root of zero or negative argument@>; |
| 1523 |
} else { |
| 1524 |
decNumberSquareRoot(ret->data.num, &x, &set); |
| 1525 |
} |
| 1526 |
mp_check_decNumber(mp, ret->data.num, &set); |
| 1527 |
} |
| 1528 |
|
| 1529 |
|
| 1530 |
@ @<Handle square root of zero...@>= |
| 1531 |
{ |
| 1532 |
if (decNumberIsNegative(&x)) { |
| 1533 |
char msg[256]; |
| 1534 |
const char *hlp[] = { |
| 1535 |
"Since I don't take square roots of negative numbers,", |
| 1536 |
"I'm zeroing this one. Proceed, with fingers crossed.", |
| 1537 |
NULL }; |
| 1538 |
char *xstr = mp_decimal_number_tostring (mp, x_orig); |
| 1539 |
mp_snprintf(msg, 256, "Square root of %s has been replaced by 0", xstr); |
| 1540 |
free(xstr); |
| 1541 |
@.Square root...replaced by 0@>; |
| 1542 |
mp_error (mp, msg, hlp, true); |
| 1543 |
} |
| 1544 |
decNumberZero(ret->data.num); |
| 1545 |
return; |
| 1546 |
} |
| 1547 |
|
| 1548 |
|
| 1549 |
@ Pythagorean addition $\psqrt{a^2+b^2}$ is implemented by a quick hack |
| 1550 |
|
| 1551 |
@c |
| 1552 |
void mp_decimal_pyth_add (MP mp, mp_number *ret, mp_number a_orig, mp_number b_orig) { |
| 1553 |
decNumber a, b; |
| 1554 |
decNumber asq, bsq; |
| 1555 |
decNumberCopyAbs(&a, a_orig.data.num); |
| 1556 |
decNumberCopyAbs(&b, b_orig.data.num); |
| 1557 |
decNumberMultiply(&asq, &a, &a, &set); |
| 1558 |
decNumberMultiply(&bsq, &b, &b, &set); |
| 1559 |
decNumberAdd(&a, &asq, &bsq, &set); |
| 1560 |
decNumberSquareRoot(ret->data.num, &a, &set); |
| 1561 |
/*|if (set.status != 0) {|*/ |
| 1562 |
/*| mp->arith_error = true;|*/ |
| 1563 |
/*| decNumberCopy(ret->data.num, &EL_GORDO_decNumber);|*/ |
| 1564 |
/*|}|*/ |
| 1565 |
mp_check_decNumber(mp, ret->data.num, &set); |
| 1566 |
} |
| 1567 |
|
| 1568 |
@ Here is a similar algorithm for $\psqrt{a^2-b^2}$. Same quick hack, also. |
| 1569 |
|
| 1570 |
@c |
| 1571 |
void mp_decimal_pyth_sub (MP mp, mp_number *ret, mp_number a_orig, mp_number b_orig) { |
| 1572 |
decNumber a, b; |
| 1573 |
decNumberCopyAbs(&a, a_orig.data.num); |
| 1574 |
decNumberCopyAbs(&b, b_orig.data.num); |
| 1575 |
if (!decNumberGreater(&a,&b)) { |
| 1576 |
@<Handle erroneous |pyth_sub| and set |a:=0|@>; |
| 1577 |
} else { |
| 1578 |
decNumber asq, bsq; |
| 1579 |
decNumberMultiply(&asq, &a, &a, &set); |
| 1580 |
decNumberMultiply(&bsq, &b, &b, &set); |
| 1581 |
decNumberSubtract(&a, &asq, &bsq, &set); |
| 1582 |
decNumberSquareRoot(&a, &a, &set); |
| 1583 |
} |
| 1584 |
decNumberCopy(ret->data.num, &a); |
| 1585 |
mp_check_decNumber(mp, ret->data.num, &set); |
| 1586 |
} |
| 1587 |
|
| 1588 |
|
| 1589 |
@ @<Handle erroneous |pyth_sub| and set |a:=0|@>= |
| 1590 |
{ |
| 1591 |
if (decNumberLess(&a, &b)) { |
| 1592 |
char msg[256]; |
| 1593 |
const char *hlp[] = { |
| 1594 |
"Since I don't take square roots of negative numbers,", |
| 1595 |
"I'm zeroing this one. Proceed, with fingers crossed.", |
| 1596 |
NULL }; |
| 1597 |
char *astr = mp_decimal_number_tostring (mp, a_orig); |
| 1598 |
char *bstr = mp_decimal_number_tostring (mp, b_orig); |
| 1599 |
mp_snprintf (msg, 256, "Pythagorean subtraction %s+-+%s has been replaced by 0", astr, bstr); |
| 1600 |
free(astr); |
| 1601 |
free(bstr); |
| 1602 |
@.Pythagorean...@>; |
| 1603 |
mp_error (mp, msg, hlp, true); |
| 1604 |
} |
| 1605 |
decNumberZero(&a); |
| 1606 |
} |
| 1607 |
|
| 1608 |
|
| 1609 |
@ Here is the routine that calculates $2^8$ times the natural logarithm |
| 1610 |
of a |scaled| quantity; |
| 1611 |
|
| 1612 |
@c |
| 1613 |
void mp_decimal_m_log (MP mp, mp_number *ret, mp_number x_orig) { |
| 1614 |
if (!decNumberIsPositive((decNumber *)x_orig.data.num)) { |
| 1615 |
@<Handle non-positive logarithm@>; |
| 1616 |
} else { |
| 1617 |
decNumber twofivesix; |
| 1618 |
decNumberFromInt32(&twofivesix, 256); |
| 1619 |
decNumberLn(ret->data.num, x_orig.data.num, &limitedset); |
| 1620 |
mp_check_decNumber(mp, ret->data.num, &limitedset); |
| 1621 |
decNumberMultiply(ret->data.num, ret->data.num, &twofivesix, &set); |
| 1622 |
} |
| 1623 |
mp_check_decNumber(mp, ret->data.num, &set); |
| 1624 |
} |
| 1625 |
|
| 1626 |
@ @<Handle non-positive logarithm@>= |
| 1627 |
{ |
| 1628 |
char msg[256]; |
| 1629 |
const char *hlp[] = { |
| 1630 |
"Since I don't take logs of non-positive numbers,", |
| 1631 |
"I'm zeroing this one. Proceed, with fingers crossed.", |
| 1632 |
NULL }; |
| 1633 |
char *xstr = mp_decimal_number_tostring (mp, x_orig); |
| 1634 |
mp_snprintf (msg, 256, "Logarithm of %s has been replaced by 0", xstr); |
| 1635 |
free (xstr); |
| 1636 |
@.Logarithm...replaced by 0@>; |
| 1637 |
mp_error (mp, msg, hlp, true); |
| 1638 |
decNumberZero(ret->data.num); |
| 1639 |
} |
| 1640 |
|
| 1641 |
|
| 1642 |
@ Conversely, the exponential routine calculates $\exp(x/2^8)$, |
| 1643 |
when |x| is |scaled|. |
| 1644 |
|
| 1645 |
@c |
| 1646 |
void mp_decimal_m_exp (MP mp, mp_number *ret, mp_number x_orig) { |
| 1647 |
decNumber temp, twofivesix; |
| 1648 |
decNumberFromInt32(&twofivesix, 256); |
| 1649 |
decNumberDivide(&temp, x_orig.data.num, &twofivesix, &set); |
| 1650 |
limitedset.status = 0; |
| 1651 |
decNumberExp(ret->data.num, &temp, &limitedset); |
| 1652 |
if (limitedset.status & DEC_Clamped) { |
| 1653 |
if (decNumberIsPositive((decNumber *)x_orig.data.num)) { |
| 1654 |
mp->arith_error = true; |
| 1655 |
decNumberCopy(ret->data.num, &EL_GORDO_decNumber); |
| 1656 |
} else { |
| 1657 |
decNumberZero(ret->data.num); |
| 1658 |
} |
| 1659 |
} |
| 1660 |
mp_check_decNumber(mp, ret->data.num, &limitedset); |
| 1661 |
limitedset.status = 0; |
| 1662 |
} |
| 1663 |
|
| 1664 |
|
| 1665 |
@ Given integers |x| and |y|, not both zero, the |n_arg| function |
| 1666 |
returns the |angle| whose tangent points in the direction $(x,y)$. |
| 1667 |
|
| 1668 |
@c |
| 1669 |
void mp_decimal_n_arg (MP mp, mp_number *ret, mp_number x_orig, mp_number y_orig) { |
| 1670 |
if (decNumberIsZero((decNumber *)x_orig.data.num) && decNumberIsZero((decNumber *)y_orig.data.num)) { |
| 1671 |
@<Handle undefined arg@>; |
| 1672 |
} else { |
| 1673 |
decNumber atan2val, oneeighty_angle; |
| 1674 |
ret->type = mp_angle_type; |
| 1675 |
decNumberFromInt32(&oneeighty_angle, 180 * angle_multiplier); |
| 1676 |
decNumberDivide(&oneeighty_angle, &oneeighty_angle, &PI_decNumber, &set); |
| 1677 |
checkZero(y_orig.data.num); |
| 1678 |
checkZero(x_orig.data.num); |
| 1679 |
decNumberAtan2(&atan2val, y_orig.data.num, x_orig.data.num, &set); |
| 1680 |
#if MPOST_DEBUG |
| 1681 |
fprintf(stdout, "\n%g = atan2(%g,%g)", decNumberToDouble(&atan2val),mp_number_to_double(x_orig),mp_number_to_double(y_orig)); |
| 1682 |
#endif |
| 1683 |
decNumberMultiply(ret->data.num,&atan2val, &oneeighty_angle, &set); |
| 1684 |
checkZero(ret->data.num); |
| 1685 |
#if MPOST_DEBUG |
| 1686 |
fprintf(stdout, "\nn_arg(%g,%g,%g)", mp_number_to_double(*ret), |
| 1687 |
mp_number_to_double(x_orig),mp_number_to_double(y_orig)); |
| 1688 |
#endif |
| 1689 |
} |
| 1690 |
mp_check_decNumber(mp, ret->data.num, &set); |
| 1691 |
} |
| 1692 |
|
| 1693 |
|
| 1694 |
@ @<Handle undefined arg@>= |
| 1695 |
{ |
| 1696 |
const char *hlp[] = { |
| 1697 |
"The `angle' between two identical points is undefined.", |
| 1698 |
"I'm zeroing this one. Proceed, with fingers crossed.", |
| 1699 |
NULL }; |
| 1700 |
mp_error (mp, "angle(0,0) is taken as zero", hlp, true); |
| 1701 |
@.angle(0,0)...zero@>; |
| 1702 |
decNumberZero(ret->data.num); |
| 1703 |
} |
| 1704 |
|
| 1705 |
|
| 1706 |
@ Conversely, the |n_sin_cos| routine takes an |angle| and produces the sine |
| 1707 |
and cosine of that angle. The results of this routine are |
| 1708 |
stored in global integer variables |n_sin| and |n_cos|. |
| 1709 |
|
| 1710 |
First, we need a decNumber function that calculates sines and cosines |
| 1711 |
using the Taylor series. This function is fairly optimized. |
| 1712 |
|
| 1713 |
@d PRECALC_FACTORIALS_CACHESIZE 50 |
| 1714 |
|
| 1715 |
@c |
| 1716 |
static void sinecosine(decNumber *theangle, decNumber *c, decNumber *s) |
| 1717 |
{ |
| 1718 |
int n, i, prec; |
| 1719 |
decNumber p, pxa, fac, cc; |
| 1720 |
decNumber n1, n2, p1; |
| 1721 |
decNumberZero(c); |
| 1722 |
decNumberZero(s); |
| 1723 |
prec = (set.digits/2); |
| 1724 |
if (prec < DECPRECISION_DEFAULT) prec = DECPRECISION_DEFAULT; |
| 1725 |
for (n=0;n<prec;n++) |
| 1726 |
{ |
| 1727 |
decNumberFromInt32(&p1, n); |
| 1728 |
decNumberFromInt32(&n1, 2*n); |
| 1729 |
decNumberPower(&p, &minusone, &p1, &limitedset); |
| 1730 |
if (n==0) { |
| 1731 |
decNumberCopy(&pxa, &one); |
| 1732 |
} else { |
| 1733 |
decNumberPower(&pxa, theangle, &n1, &limitedset); |
| 1734 |
} |
| 1735 |
|
| 1736 |
if (2*n<last_cached_factorial) { |
| 1737 |
decNumberCopy(&fac,factorials[2*n]); |
| 1738 |
} else { |
| 1739 |
decNumberCopy(&fac,factorials[last_cached_factorial]); |
| 1740 |
for (i = last_cached_factorial+1; i <= 2*n; i++) { |
| 1741 |
decNumberFromInt32(&cc, i); |
| 1742 |
decNumberMultiply (&fac, &fac, &cc, &set); |
| 1743 |
if (i<PRECALC_FACTORIALS_CACHESIZE) { |
| 1744 |
factorials[i] = malloc(sizeof(decNumber)); |
| 1745 |
decNumberCopy(factorials[i],&fac); |
| 1746 |
last_cached_factorial = i; |
| 1747 |
} |
| 1748 |
} |
| 1749 |
} |
| 1750 |
|
| 1751 |
decNumberDivide (&pxa, &pxa, &fac, &set); |
| 1752 |
decNumberMultiply (&pxa, &pxa, &p, &set); |
| 1753 |
decNumberAdd (s, s, &pxa, &set); |
| 1754 |
|
| 1755 |
decNumberFromInt32(&n2, 2*n+1); |
| 1756 |
decNumberMultiply (&fac, &fac, &n2, &set); /* fac = fac * (2*n+1)*/ |
| 1757 |
decNumberPower(&pxa, theangle, &n2, &limitedset); |
| 1758 |
decNumberDivide (&pxa, &pxa, &fac, &set); |
| 1759 |
decNumberMultiply (&pxa, &pxa, &p, &set); |
| 1760 |
decNumberAdd (c, c, &pxa, &set); |
| 1761 |
/* |printf("\niteration %2d: %-42s %-42s",n,tostring(c), tostring(s));|*/ |
| 1762 |
} |
| 1763 |
} |
| 1764 |
|
| 1765 |
@ Calculate sines and cosines. |
| 1766 |
@c |
| 1767 |
void mp_decimal_sin_cos (MP mp, mp_number z_orig, mp_number *n_cos, mp_number *n_sin) { |
| 1768 |
decNumber rad; |
| 1769 |
double tmp; |
| 1770 |
decNumber one_eighty; |
| 1771 |
tmp = mp_number_to_double(z_orig)/16.0; |
| 1772 |
|
| 1773 |
#if MPOST_DEBUG |
| 1774 |
fprintf(stdout, "\nsin_cos(%f)", mp_number_to_double(z_orig)); |
| 1775 |
#endif |
| 1776 |
#if 0 |
| 1777 |
if (decNumberIsNegative(&rad)) { |
| 1778 |
while (decNumberLess(&rad,&PI_decNumber)) |
| 1779 |
decNumberAdd(&rad, &rad, &PI_decNumber, &set); |
| 1780 |
} else { |
| 1781 |
while (decNumberGreater(&rad,&PI_decNumber)) |
| 1782 |
decNumberSubtract(&rad, &rad, &PI_decNumber, &set); |
| 1783 |
} |
| 1784 |
#endif |
| 1785 |
if ((tmp == 90.0)||(tmp == -270)){ |
| 1786 |
decNumberZero(n_cos->data.num); |
| 1787 |
decNumberCopy(n_sin->data.num,&fraction_multiplier_decNumber); |
| 1788 |
} else if ((tmp == -90.0)||(tmp == 270.0)) { |
| 1789 |
decNumberZero(n_cos->data.num); |
| 1790 |
decNumberCopyNegate(n_sin->data.num,&fraction_multiplier_decNumber); |
| 1791 |
} else if ((tmp == 180.0) || (tmp == -180.0)) { |
| 1792 |
decNumberCopyNegate(n_cos->data.num,&fraction_multiplier_decNumber); |
| 1793 |
decNumberZero(n_sin->data.num); |
| 1794 |
} else { |
| 1795 |
decNumberFromInt32(&one_eighty, 180 * 16); |
| 1796 |
decNumberMultiply(&rad, z_orig.data.num, &PI_decNumber, &set); |
| 1797 |
decNumberDivide(&rad, &rad, &one_eighty, &set); |
| 1798 |
sinecosine(&rad, n_sin->data.num, n_cos->data.num); |
| 1799 |
decNumberMultiply(n_cos->data.num,n_cos->data.num,&fraction_multiplier_decNumber, &set); |
| 1800 |
decNumberMultiply(n_sin->data.num,n_sin->data.num,&fraction_multiplier_decNumber, &set); |
| 1801 |
} |
| 1802 |
#if MPOST_DEBUG |
| 1803 |
fprintf(stdout, "\nsin_cos(%f,%f,%f)", decNumberToDouble(&rad), |
| 1804 |
mp_number_to_double(*n_cos), mp_number_to_double(*n_sin)); |
| 1805 |
#endif |
| 1806 |
mp_check_decNumber(mp, n_cos->data.num, &set); |
| 1807 |
mp_check_decNumber(mp, n_sin->data.num, &set); |
| 1808 |
} |
| 1809 |
|
| 1810 |
@ This is the {\tt http://www-cs-faculty.stanford.edu/~uno/programs/rng.c} |
| 1811 |
with small cosmetic modifications. |
| 1812 |
|
| 1813 |
@c |
| 1814 |
#define KK 100 /* the long lag */ |
| 1815 |
#define LL 37 /* the short lag */ |
| 1816 |
#define MM (1L<<30) /* the modulus */ |
| 1817 |
#define mod_diff(x,y) (((x)-(y))&(MM-1)) /* subtraction mod MM */ |
| 1818 |
/* */ |
| 1819 |
static long ran_x[KK]; /* the generator state */ |
| 1820 |
/* */ |
| 1821 |
static void ran_array(long aa[],int n) /* put n new random numbers in aa */ |
| 1822 |
/* long aa[] destination */ |
| 1823 |
/* int n array length (must be at least KK) */ |
| 1824 |
{ |
| 1825 |
register int i,j; |
| 1826 |
for (j=0;j<KK;j++) aa[j]=ran_x[j]; |
| 1827 |
for (;j<n;j++) aa[j]=mod_diff(aa[j-KK],aa[j-LL]); |
| 1828 |
for (i=0;i<LL;i++,j++) ran_x[i]=mod_diff(aa[j-KK],aa[j-LL]); |
| 1829 |
for (;i<KK;i++,j++) ran_x[i]=mod_diff(aa[j-KK],ran_x[i-LL]); |
| 1830 |
} |
| 1831 |
/* */ |
| 1832 |
/* the following routines are from exercise 3.6--15 */ |
| 1833 |
/* after calling |ran_start|, get new randoms by, e.g., "|x=ran_arr_next()|" */ |
| 1834 |
/* */ |
| 1835 |
#define QUALITY 1009 /* recommended quality level for high-res use */ |
| 1836 |
static long ran_arr_buf[QUALITY]; |
| 1837 |
static long ran_arr_dummy=-1, ran_arr_started=-1; |
| 1838 |
static long *ran_arr_ptr=&ran_arr_dummy; /* the next random number, or -1 */ |
| 1839 |
/* */ |
| 1840 |
#define TT 70 /* guaranteed separation between streams */ |
| 1841 |
#define is_odd(x) ((x)&1) /* units bit of x */ |
| 1842 |
/* */ |
| 1843 |
static void ran_start(long seed) /* do this before using |ran_array| */ |
| 1844 |
/* |long seed| selector for different streams */ |
| 1845 |
{ |
| 1846 |
register int t,j; |
| 1847 |
long x[KK+KK-1]; /* the preparation buffer */ |
| 1848 |
register long ss=(seed+2)&(MM-2); |
| 1849 |
for (j=0;j<KK;j++) { |
| 1850 |
x[j]=ss; /* bootstrap the buffer */ |
| 1851 |
ss<<=1; if (ss>=MM) ss-=MM-2; /* cyclic shift 29 bits */ |
| 1852 |
} |
| 1853 |
x[1]++; /* make x[1] (and only x[1]) odd */ |
| 1854 |
for (ss=seed&(MM-1),t=TT-1; t; ) { |
| 1855 |
for (j=KK-1;j>0;j--) x[j+j]=x[j], x[j+j-1]=0; /* "square" */ |
| 1856 |
for (j=KK+KK-2;j>=KK;j--) |
| 1857 |
x[j-(KK-LL)]=mod_diff(x[j-(KK-LL)],x[j]), |
| 1858 |
x[j-KK]=mod_diff(x[j-KK],x[j]); |
| 1859 |
if (is_odd(ss)) { /* "multiply by z" */ |
| 1860 |
for (j=KK;j>0;j--) x[j]=x[j-1]; |
| 1861 |
x[0]=x[KK]; /* shift the buffer cyclically */ |
| 1862 |
x[LL]=mod_diff(x[LL],x[KK]); |
| 1863 |
} |
| 1864 |
if (ss) ss>>=1; else t--; |
| 1865 |
} |
| 1866 |
for (j=0;j<LL;j++) ran_x[j+KK-LL]=x[j]; |
| 1867 |
for (;j<KK;j++) ran_x[j-LL]=x[j]; |
| 1868 |
for (j=0;j<10;j++) ran_array(x,KK+KK-1); /* warm things up */ |
| 1869 |
ran_arr_ptr=&ran_arr_started; |
| 1870 |
} |
| 1871 |
/* */ |
| 1872 |
#define ran_arr_next() (*ran_arr_ptr>=0? *ran_arr_ptr++: ran_arr_cycle()) |
| 1873 |
static long ran_arr_cycle(void) |
| 1874 |
{ |
| 1875 |
if (ran_arr_ptr==&ran_arr_dummy) |
| 1876 |
ran_start(314159L); /* the user forgot to initialize */ |
| 1877 |
ran_array(ran_arr_buf,QUALITY); |
| 1878 |
ran_arr_buf[KK]=-1; |
| 1879 |
ran_arr_ptr=ran_arr_buf+1; |
| 1880 |
return ran_arr_buf[0]; |
| 1881 |
} |
| 1882 |
|
| 1883 |
|
| 1884 |
|
| 1885 |
@ To initialize the |randoms| table, we call the following routine. |
| 1886 |
|
| 1887 |
@c |
| 1888 |
void mp_init_randoms (MP mp, int seed) { |
| 1889 |
int j, jj, k; /* more or less random integers */ |
| 1890 |
int i; /* index into |randoms| */ |
| 1891 |
j = abs (seed); |
| 1892 |
while (j >= fraction_one) { |
| 1893 |
j = j/2; |
| 1894 |
} |
| 1895 |
k = 1; |
| 1896 |
for (i = 0; i <= 54; i++) { |
| 1897 |
jj = k; |
| 1898 |
k = j - k; |
| 1899 |
j = jj; |
| 1900 |
if (k<0) |
| 1901 |
k += fraction_one; |
| 1902 |
decNumberFromInt32(mp->randoms[(i * 21) % 55].data.num, j); |
| 1903 |
} |
| 1904 |
mp_new_randoms (mp); |
| 1905 |
mp_new_randoms (mp); |
| 1906 |
mp_new_randoms (mp); /* ``warm up'' the array */ |
| 1907 |
|
| 1908 |
ran_start((long) seed); |
| 1909 |
|
| 1910 |
} |
| 1911 |
|
| 1912 |
@ @c |
| 1913 |
void mp_decimal_number_modulo (mp_number *a, mp_number b) { |
| 1914 |
decNumberRemainder(a->data.num, a->data.num, b.data.num, &set); |
| 1915 |
} |
| 1916 |
|
| 1917 |
|
| 1918 |
@ To consume a random integer for the uniform generator, the program below will say `|next_unif_random|'. |
| 1919 |
|
| 1920 |
@c |
| 1921 |
static void mp_next_unif_random (MP mp, mp_number *ret) { |
| 1922 |
decNumber a; |
| 1923 |
decNumber b; |
| 1924 |
/*unsigned long int op;*/ |
| 1925 |
uint32_t op; |
| 1926 |
(void)mp; |
| 1927 |
/*op = (unsigned)ran_arr_next(); */ |
| 1928 |
op = (uint32_t)ran_arr_next(); |
| 1929 |
decNumberFromUInt32(&a, op); |
| 1930 |
decNumberFromInt32(&b, MM); |
| 1931 |
decNumberDivide (&a, &a, &b, &set); /* a = a/b */ |
| 1932 |
decNumberCopy(ret->data.num, &a); |
| 1933 |
mp_check_decNumber(mp, ret->data.num, &set); |
| 1934 |
} |
| 1935 |
|
| 1936 |
|
| 1937 |
@ To consume a random fraction, the program below will say `|next_random|'. |
| 1938 |
|
| 1939 |
@c |
| 1940 |
static void mp_next_random (MP mp, mp_number *ret) { |
| 1941 |
if ( mp->j_random==0 ) |
| 1942 |
mp_new_randoms(mp); |
| 1943 |
else |
| 1944 |
mp->j_random = mp->j_random-1; |
| 1945 |
mp_number_clone (ret, mp->randoms[mp->j_random]); |
| 1946 |
} |
| 1947 |
|
| 1948 |
|
| 1949 |
@ To produce a uniform random number in the range |0<=u<x| or |0>=u>x| |
| 1950 |
or |0=u=x|, given a |scaled| value~|x|, we proceed as shown here. |
| 1951 |
|
| 1952 |
Note that the call of |take_fraction| will produce the values 0 and~|x| |
| 1953 |
with about half the probability that it will produce any other particular |
| 1954 |
values between 0 and~|x|, because it rounds its answers. |
| 1955 |
|
| 1956 |
@c |
| 1957 |
static void mp_decimal_m_unif_rand (MP mp, mp_number *ret, mp_number x_orig) { |
| 1958 |
mp_number y; /* trial value */ |
| 1959 |
mp_number x, abs_x; |
| 1960 |
mp_number u; |
| 1961 |
new_fraction (y); |
| 1962 |
new_number (x); |
| 1963 |
new_number (abs_x); |
| 1964 |
new_number (u); |
| 1965 |
mp_number_clone (&x, x_orig); |
| 1966 |
mp_number_clone (&abs_x, x); |
| 1967 |
mp_decimal_abs (&abs_x); |
| 1968 |
mp_next_unif_random(mp, &u); |
| 1969 |
decNumberMultiply (y.data.num, abs_x.data.num, u.data.num, &set); |
| 1970 |
free_number (u); |
| 1971 |
if (mp_number_equal(y, abs_x)) { |
| 1972 |
mp_number_clone (ret, ((math_data *)mp->math)->zero_t); |
| 1973 |
} else if (mp_number_greater(x, ((math_data *)mp->math)->zero_t)) { |
| 1974 |
mp_number_clone (ret, y); |
| 1975 |
} else { |
| 1976 |
mp_number_clone (ret, y); |
| 1977 |
mp_number_negate (ret); |
| 1978 |
} |
| 1979 |
free_number (abs_x); |
| 1980 |
free_number (x); |
| 1981 |
free_number (y); |
| 1982 |
} |
| 1983 |
|
| 1984 |
|
| 1985 |
|
| 1986 |
@ Finally, a normal deviate with mean zero and unit standard deviation |
| 1987 |
can readily be obtained with the ratio method (Algorithm 3.4.1R in |
| 1988 |
{\sl The Art of Computer Programming\/}). |
| 1989 |
|
| 1990 |
@c |
| 1991 |
static void mp_decimal_m_norm_rand (MP mp, mp_number *ret) { |
| 1992 |
mp_number ab_vs_cd; |
| 1993 |
mp_number abs_x; |
| 1994 |
mp_number u; |
| 1995 |
mp_number r; |
| 1996 |
mp_number la, xa; |
| 1997 |
new_number (ab_vs_cd); |
| 1998 |
new_number (la); |
| 1999 |
new_number (xa); |
| 2000 |
new_number (abs_x); |
| 2001 |
new_number (u); |
| 2002 |
new_number (r); |
| 2003 |
|
| 2004 |
do { |
| 2005 |
do { |
| 2006 |
mp_number v; |
| 2007 |
new_number (v); |
| 2008 |
mp_next_random(mp, &v); |
| 2009 |
mp_number_substract (&v, ((math_data *)mp->math)->fraction_half_t); |
| 2010 |
mp_decimal_number_take_fraction (mp,&xa, ((math_data *)mp->math)->sqrt_8_e_k, v); |
| 2011 |
free_number (v); |
| 2012 |
mp_next_random(mp, &u); |
| 2013 |
mp_number_clone (&abs_x, xa); |
| 2014 |
mp_decimal_abs (&abs_x); |
| 2015 |
} while (!mp_number_less(abs_x, u)); |
| 2016 |
mp_decimal_number_make_fraction (mp, &r, xa, u); |
| 2017 |
mp_number_clone (&xa, r); |
| 2018 |
mp_decimal_m_log (mp,&la, u); |
| 2019 |
mp_set_decimal_from_substraction(&la, ((math_data *)mp->math)->twelve_ln_2_k, la); |
| 2020 |
mp_ab_vs_cd (mp,&ab_vs_cd, ((math_data *)mp->math)->one_k, la, xa, xa); |
| 2021 |
} while (mp_number_less(ab_vs_cd,((math_data *)mp->math)->zero_t)); |
| 2022 |
mp_number_clone (ret, xa); |
| 2023 |
free_number (ab_vs_cd); |
| 2024 |
free_number (r); |
| 2025 |
free_number (abs_x); |
| 2026 |
free_number (la); |
| 2027 |
free_number (xa); |
| 2028 |
free_number (u); |
| 2029 |
} |
| 2030 |
|
| 2031 |
|
| 2032 |
|
| 2033 |
|
| 2034 |
@ The following subroutine could be used in |norm_rand| and tests if $ab$ is |
| 2035 |
greater than, equal to, or less than~$cd$. |
| 2036 |
The result is $+1$, 0, or~$-1$ in the three respective cases. |
| 2037 |
This is not necessary, even if it's shorter than the current |ab_vs_cd| |
| 2038 |
and looks as a native implementation. |
| 2039 |
|
| 2040 |
@c |
| 2041 |
/* |
| 2042 |
|void mp_decimal_ab_vs_cd (MP mp, mp_number *ret, mp_number a_orig, mp_number b_orig, mp_number c_orig, mp_number d_orig) {| |
| 2043 |
| decNumber a, b, c, d;| |
| 2044 |
| decNumber ab, cd;| |
| 2045 |
| (void)mp;| |
| 2046 |
|| |
| 2047 |
| decNumberCopy(&a, (decNumber *)a_orig.data.num);| |
| 2048 |
| decNumberCopy(&b, (decNumber *)b_orig.data.num);| |
| 2049 |
| decNumberCopy(&c, (decNumber *)c_orig.data.num);| |
| 2050 |
| decNumberCopy(&d, (decNumber *)d_orig.data.num);| |
| 2051 |
|| |
| 2052 |
|| |
| 2053 |
| decNumberMultiply (&ab, (decNumber *)a_orig.data.num, (decNumber *)b_orig.data.num, &set);| |
| 2054 |
| decNumberMultiply (&cd, (decNumber *)c_orig.data.num, (decNumber *)d_orig.data.num, &set);| |
| 2055 |
| decNumberCompare(ret->data.num, &ab, &cd, &set);| |
| 2056 |
| mp_check_decNumber(mp, ret->data.num, &set);| |
| 2057 |
| return;| |
| 2058 |
|| |
| 2059 |
|}| |
| 2060 |
*/ |
| 2061 |
|
| 2062 |
|
| 2063 |
|