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