| 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 IEEE double 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 "mpmathdouble.h" /* internal header */ |
| 28 |
#define ROUND(a) floor((a)+0.5) |
| 29 |
@h |
| 30 |
|
| 31 |
@ @c |
| 32 |
@<Declarations@> |
| 33 |
|
| 34 |
@ @(mpmathdouble.h@>= |
| 35 |
#ifndef MPMATHDOUBLE_H |
| 36 |
#define MPMATHDOUBLE_H 1 |
| 37 |
#include "mplib.h" |
| 38 |
#include "mpmp.h" /* internal header */ |
| 39 |
@<Internal library declarations@> |
| 40 |
#endif |
| 41 |
|
| 42 |
@* Math initialization. |
| 43 |
|
| 44 |
First, here are some very important constants. |
| 45 |
|
| 46 |
@d PI 3.1415926535897932384626433832795028841971 |
| 47 |
@d fraction_multiplier 4096.0 |
| 48 |
@d angle_multiplier 16.0 |
| 49 |
|
| 50 |
@ Here are the functions that are static as they are not used elsewhere |
| 51 |
|
| 52 |
@<Declarations@>= |
| 53 |
static void mp_double_scan_fractional_token (MP mp, integer64 n); |
| 54 |
static void mp_double_scan_numeric_token (MP mp, integer64 n); |
| 55 |
static void mp_ab_vs_cd (MP mp, mp_number *ret, mp_number a, mp_number b, mp_number c, mp_number d); |
| 56 |
static void mp_double_ab_vs_cd (MP mp, mp_number *ret, mp_number a, mp_number b, mp_number c, mp_number d); |
| 57 |
static void mp_double_crossing_point (MP mp, mp_number *ret, mp_number a, mp_number b, mp_number c); |
| 58 |
static void mp_number_modulo (mp_number *a, mp_number b); |
| 59 |
static void mp_double_print_number (MP mp, mp_number n); |
| 60 |
static char * mp_double_number_tostring (MP mp, mp_number n); |
| 61 |
static void mp_double_slow_add (MP mp, mp_number *ret, mp_number x_orig, mp_number y_orig); |
| 62 |
static void mp_double_square_rt (MP mp, mp_number *ret, mp_number x_orig); |
| 63 |
static void mp_double_sin_cos (MP mp, mp_number z_orig, mp_number *n_cos, mp_number *n_sin); |
| 64 |
static void mp_init_randoms (MP mp, int seed); |
| 65 |
static void mp_number_angle_to_scaled (mp_number *A); |
| 66 |
static void mp_number_fraction_to_scaled (mp_number *A); |
| 67 |
static void mp_number_scaled_to_fraction (mp_number *A); |
| 68 |
static void mp_number_scaled_to_angle (mp_number *A); |
| 69 |
static void mp_double_m_unif_rand (MP mp, mp_number *ret, mp_number x_orig); |
| 70 |
static void mp_double_m_norm_rand (MP mp, mp_number *ret); |
| 71 |
static void mp_double_m_exp (MP mp, mp_number *ret, mp_number x_orig); |
| 72 |
static void mp_double_m_log (MP mp, mp_number *ret, mp_number x_orig); |
| 73 |
static void mp_double_pyth_sub (MP mp, mp_number *r, mp_number a, mp_number b); |
| 74 |
static void mp_double_pyth_add (MP mp, mp_number *r, mp_number a, mp_number b); |
| 75 |
static void mp_double_n_arg (MP mp, mp_number *ret, mp_number x, mp_number y); |
| 76 |
static void mp_double_velocity (MP mp, mp_number *ret, mp_number st, mp_number ct, mp_number sf, mp_number cf, mp_number t); |
| 77 |
static void mp_set_double_from_int(mp_number *A, integer64 B); |
| 78 |
static void mp_set_double_from_boolean(mp_number *A, integer64 B); |
| 79 |
static void mp_set_double_from_scaled(mp_number *A, integer64 B); |
| 80 |
static void mp_set_double_from_addition(mp_number *A, mp_number B, mp_number C); |
| 81 |
static void mp_set_double_from_substraction (mp_number *A, mp_number B, mp_number C); |
| 82 |
static void mp_set_double_from_div(mp_number *A, mp_number B, mp_number C); |
| 83 |
static void mp_set_double_from_mul(mp_number *A, mp_number B, mp_number C); |
| 84 |
static void mp_set_double_from_int_div(mp_number *A, mp_number B, integer64 C); |
| 85 |
static void mp_set_double_from_int_mul(mp_number *A, mp_number B, integer64 C); |
| 86 |
static void mp_set_double_from_of_the_way(MP mp, mp_number *A, mp_number t, mp_number B, mp_number C); |
| 87 |
static void mp_number_negate(mp_number *A); |
| 88 |
static void mp_number_add(mp_number *A, mp_number B); |
| 89 |
static void mp_number_substract(mp_number *A, mp_number B); |
| 90 |
static void mp_number_half(mp_number *A); |
| 91 |
static void mp_number_halfp(mp_number *A); |
| 92 |
static void mp_number_double(mp_number *A); |
| 93 |
static void mp_number_add_scaled(mp_number *A, integer64 B); /* also for negative B */ |
| 94 |
static void mp_number_multiply_int(mp_number *A, integer64 B); |
| 95 |
static void mp_number_divide_int(mp_number *A, integer64 B); |
| 96 |
static void mp_double_abs(mp_number *A); |
| 97 |
static void mp_number_clone(mp_number *A, mp_number B); |
| 98 |
static void mp_number_swap(mp_number *A, mp_number *B); |
| 99 |
static integer64 mp_round_unscaled(mp_number x_orig); |
| 100 |
static integer64 mp_number_to_int(mp_number A); |
| 101 |
static integer64 mp_number_to_scaled(mp_number A); |
| 102 |
static integer64 mp_number_to_boolean(mp_number A); |
| 103 |
static double mp_number_to_double(mp_number A); |
| 104 |
static int mp_number_odd(mp_number A); |
| 105 |
static int mp_number_equal(mp_number A, mp_number B); |
| 106 |
static int mp_number_greater(mp_number A, mp_number B); |
| 107 |
static int mp_number_less(mp_number A, mp_number B); |
| 108 |
static int mp_number_nonequalabs(mp_number A, mp_number B); |
| 109 |
static void mp_number_floor (mp_number *i); |
| 110 |
static void mp_double_fraction_to_round_scaled (mp_number *x); |
| 111 |
static void mp_double_number_make_scaled (MP mp, mp_number *r, mp_number p, mp_number q); |
| 112 |
static void mp_double_number_make_fraction (MP mp, mp_number *r, mp_number p, mp_number q); |
| 113 |
static void mp_double_number_take_fraction (MP mp, mp_number *r, mp_number p, mp_number q); |
| 114 |
static void mp_double_number_take_scaled (MP mp, mp_number *r, mp_number p, mp_number q); |
| 115 |
static void mp_new_number (MP mp, mp_number *n, mp_number_type t) ; |
| 116 |
static void mp_free_number (MP mp, mp_number *n) ; |
| 117 |
static void mp_set_double_from_double(mp_number *A, double B); |
| 118 |
static void mp_free_double_math (MP mp); |
| 119 |
static void mp_double_set_precision (MP mp); |
| 120 |
|
| 121 |
@ And these are the ones that {\it are} used elsewhere |
| 122 |
|
| 123 |
@<Internal library declarations@>= |
| 124 |
void * mp_initialize_double_math (MP mp); |
| 125 |
|
| 126 |
@ |
| 127 |
|
| 128 |
@d coef_bound ((7.0/3.0)*fraction_multiplier) /* |fraction| approximation to 7/3 */ |
| 129 |
@d fraction_threshold 0.04096 /* a |fraction| coefficient less than this is zeroed */ |
| 130 |
@d half_fraction_threshold (fraction_threshold/2) /* half of |fraction_threshold| */ |
| 131 |
@d scaled_threshold 0.000122 /* a |scaled| coefficient less than this is zeroed */ |
| 132 |
@d half_scaled_threshold (scaled_threshold/2) /* half of |scaled_threshold| */ |
| 133 |
@d near_zero_angle (0.0256*angle_multiplier) /* an angle of about 0.0256 */ |
| 134 |
@d p_over_v_threshold 0x80000 /* TODO */ |
| 135 |
@d equation_threshold 0.001 |
| 136 |
@d tfm_warn_threshold 0.0625 |
| 137 |
@d warning_limit pow(2.0,52.0) /* this is a large value that can just be expressed without loss of precision */ |
| 138 |
@d epsilon pow(2.0,-52.0) |
| 139 |
|
| 140 |
@c |
| 141 |
void * mp_initialize_double_math (MP mp) { |
| 142 |
math_data *math = (math_data *)mp_xmalloc(mp,1,sizeof(math_data)); |
| 143 |
/* alloc */ |
| 144 |
math->allocate = mp_new_number; |
| 145 |
math->free = mp_free_number; |
| 146 |
mp_new_number (mp, &math->precision_default, mp_scaled_type); |
| 147 |
math->precision_default.data.dval = 16 * unity; |
| 148 |
mp_new_number (mp, &math->precision_max, mp_scaled_type); |
| 149 |
math->precision_max.data.dval = 16 * unity; |
| 150 |
mp_new_number (mp, &math->precision_min, mp_scaled_type); |
| 151 |
math->precision_min.data.dval = 16 * unity; |
| 152 |
/* here are the constants for |scaled| objects */ |
| 153 |
mp_new_number (mp, &math->epsilon_t, mp_scaled_type); |
| 154 |
math->epsilon_t.data.dval = epsilon; |
| 155 |
mp_new_number (mp, &math->inf_t, mp_scaled_type); |
| 156 |
math->inf_t.data.dval = EL_GORDO; |
| 157 |
mp_new_number (mp, &math->warning_limit_t, mp_scaled_type); |
| 158 |
math->warning_limit_t.data.dval = warning_limit; |
| 159 |
mp_new_number (mp, &math->one_third_inf_t, mp_scaled_type); |
| 160 |
math->one_third_inf_t.data.dval = one_third_EL_GORDO; |
| 161 |
mp_new_number (mp, &math->unity_t, mp_scaled_type); |
| 162 |
math->unity_t.data.dval = unity; |
| 163 |
mp_new_number (mp, &math->two_t, mp_scaled_type); |
| 164 |
math->two_t.data.dval = two; |
| 165 |
mp_new_number (mp, &math->three_t, mp_scaled_type); |
| 166 |
math->three_t.data.dval = three; |
| 167 |
mp_new_number (mp, &math->half_unit_t, mp_scaled_type); |
| 168 |
math->half_unit_t.data.dval = half_unit; |
| 169 |
mp_new_number (mp, &math->three_quarter_unit_t, mp_scaled_type); |
| 170 |
math->three_quarter_unit_t.data.dval = three_quarter_unit; |
| 171 |
mp_new_number (mp, &math->zero_t, mp_scaled_type); |
| 172 |
/* |fractions| */ |
| 173 |
mp_new_number (mp, &math->arc_tol_k, mp_fraction_type); |
| 174 |
math->arc_tol_k.data.dval = (unity/4096); /* quit when change in arc length estimate reaches this */ |
| 175 |
mp_new_number (mp, &math->fraction_one_t, mp_fraction_type); |
| 176 |
math->fraction_one_t.data.dval = fraction_one; |
| 177 |
mp_new_number (mp, &math->fraction_half_t, mp_fraction_type); |
| 178 |
math->fraction_half_t.data.dval = fraction_half; |
| 179 |
mp_new_number (mp, &math->fraction_three_t, mp_fraction_type); |
| 180 |
math->fraction_three_t.data.dval = fraction_three; |
| 181 |
mp_new_number (mp, &math->fraction_four_t, mp_fraction_type); |
| 182 |
math->fraction_four_t.data.dval = fraction_four; |
| 183 |
/* |angles| */ |
| 184 |
mp_new_number (mp, &math->three_sixty_deg_t, mp_angle_type); |
| 185 |
math->three_sixty_deg_t.data.dval = three_sixty_deg; |
| 186 |
mp_new_number (mp, &math->one_eighty_deg_t, mp_angle_type); |
| 187 |
math->one_eighty_deg_t.data.dval = one_eighty_deg; |
| 188 |
/* various approximations */ |
| 189 |
mp_new_number (mp, &math->one_k, mp_scaled_type); |
| 190 |
math->one_k.data.dval = 1.0/64 ; |
| 191 |
mp_new_number (mp, &math->sqrt_8_e_k, mp_scaled_type); |
| 192 |
math->sqrt_8_e_k.data.dval = 1.71552776992141359295 ; /* $2^{16}\sqrt{8/e}\approx 112428.82793$ */ |
| 193 |
mp_new_number (mp, &math->twelve_ln_2_k, mp_fraction_type); |
| 194 |
math->twelve_ln_2_k.data.dval = 8.31776616671934371292 *256; /* $2^{24}\cdot12\ln2\approx139548959.6165$ */ |
| 195 |
mp_new_number (mp, &math->coef_bound_k, mp_fraction_type); |
| 196 |
math->coef_bound_k.data.dval = coef_bound; |
| 197 |
mp_new_number (mp, &math->coef_bound_minus_1, mp_fraction_type); |
| 198 |
math->coef_bound_minus_1.data.dval = coef_bound - 1/65536.0; |
| 199 |
mp_new_number (mp, &math->twelvebits_3, mp_scaled_type); |
| 200 |
math->twelvebits_3.data.dval = 1365 / 65536.0; /* $1365\approx 2^{12}/3$ */ |
| 201 |
mp_new_number (mp, &math->twentysixbits_sqrt2_t, mp_fraction_type); |
| 202 |
math->twentysixbits_sqrt2_t.data.dval = 94906266 / 65536.0; /* $2^{26}\sqrt2\approx94906265.62$ */ |
| 203 |
mp_new_number (mp, &math->twentyeightbits_d_t, mp_fraction_type); |
| 204 |
math->twentyeightbits_d_t.data.dval = 35596755 / 65536.0; /* $2^{28}d\approx35596754.69$ */ |
| 205 |
mp_new_number (mp, &math->twentysevenbits_sqrt2_d_t, mp_fraction_type); |
| 206 |
math->twentysevenbits_sqrt2_d_t.data.dval = 25170707 / 65536.0; /* $2^{27}\sqrt2\,d\approx25170706.63$ */ |
| 207 |
/* thresholds */ |
| 208 |
mp_new_number (mp, &math->fraction_threshold_t, mp_fraction_type); |
| 209 |
math->fraction_threshold_t.data.dval = fraction_threshold; |
| 210 |
mp_new_number (mp, &math->half_fraction_threshold_t, mp_fraction_type); |
| 211 |
math->half_fraction_threshold_t.data.dval = half_fraction_threshold; |
| 212 |
mp_new_number (mp, &math->scaled_threshold_t, mp_scaled_type); |
| 213 |
math->scaled_threshold_t.data.dval = scaled_threshold; |
| 214 |
mp_new_number (mp, &math->half_scaled_threshold_t, mp_scaled_type); |
| 215 |
math->half_scaled_threshold_t.data.dval = half_scaled_threshold; |
| 216 |
mp_new_number (mp, &math->near_zero_angle_t, mp_angle_type); |
| 217 |
math->near_zero_angle_t.data.dval = near_zero_angle; |
| 218 |
mp_new_number (mp, &math->p_over_v_threshold_t, mp_fraction_type); |
| 219 |
math->p_over_v_threshold_t.data.dval = p_over_v_threshold; |
| 220 |
mp_new_number (mp, &math->equation_threshold_t, mp_scaled_type); |
| 221 |
math->equation_threshold_t.data.dval = equation_threshold; |
| 222 |
mp_new_number (mp, &math->tfm_warn_threshold_t, mp_scaled_type); |
| 223 |
math->tfm_warn_threshold_t.data.dval = tfm_warn_threshold; |
| 224 |
/* functions */ |
| 225 |
math->from_int = mp_set_double_from_int; |
| 226 |
math->from_boolean = mp_set_double_from_boolean; |
| 227 |
math->from_scaled = mp_set_double_from_scaled; |
| 228 |
math->from_double = mp_set_double_from_double; |
| 229 |
math->from_addition = mp_set_double_from_addition; |
| 230 |
math->from_substraction = mp_set_double_from_substraction; |
| 231 |
math->from_oftheway = mp_set_double_from_of_the_way; |
| 232 |
math->from_div = mp_set_double_from_div; |
| 233 |
math->from_mul = mp_set_double_from_mul; |
| 234 |
math->from_int_div = mp_set_double_from_int_div; |
| 235 |
math->from_int_mul = mp_set_double_from_int_mul; |
| 236 |
math->negate = mp_number_negate; |
| 237 |
math->add = mp_number_add; |
| 238 |
math->substract = mp_number_substract; |
| 239 |
math->half = mp_number_half; |
| 240 |
math->halfp = mp_number_halfp; |
| 241 |
math->do_double = mp_number_double; |
| 242 |
math->abs = mp_double_abs; |
| 243 |
math->clone = mp_number_clone; |
| 244 |
math->swap = mp_number_swap; |
| 245 |
math->add_scaled = mp_number_add_scaled; |
| 246 |
math->multiply_int = mp_number_multiply_int; |
| 247 |
math->divide_int = mp_number_divide_int; |
| 248 |
math->to_boolean = mp_number_to_boolean; |
| 249 |
math->to_scaled = mp_number_to_scaled; |
| 250 |
math->to_double = mp_number_to_double; |
| 251 |
math->to_int = mp_number_to_int; |
| 252 |
math->odd = mp_number_odd; |
| 253 |
math->equal = mp_number_equal; |
| 254 |
math->less = mp_number_less; |
| 255 |
math->greater = mp_number_greater; |
| 256 |
math->nonequalabs = mp_number_nonequalabs; |
| 257 |
math->round_unscaled = mp_round_unscaled; |
| 258 |
math->floor_scaled = mp_number_floor; |
| 259 |
math->fraction_to_round_scaled = mp_double_fraction_to_round_scaled; |
| 260 |
math->make_scaled = mp_double_number_make_scaled; |
| 261 |
math->make_fraction = mp_double_number_make_fraction; |
| 262 |
math->take_fraction = mp_double_number_take_fraction; |
| 263 |
math->take_scaled = mp_double_number_take_scaled; |
| 264 |
math->velocity = mp_double_velocity; |
| 265 |
math->n_arg = mp_double_n_arg; |
| 266 |
math->m_log = mp_double_m_log; |
| 267 |
math->m_exp = mp_double_m_exp; |
| 268 |
math->m_unif_rand = mp_double_m_unif_rand; |
| 269 |
math->m_norm_rand = mp_double_m_norm_rand; |
| 270 |
math->pyth_add = mp_double_pyth_add; |
| 271 |
math->pyth_sub = mp_double_pyth_sub; |
| 272 |
math->fraction_to_scaled = mp_number_fraction_to_scaled; |
| 273 |
math->scaled_to_fraction = mp_number_scaled_to_fraction; |
| 274 |
math->scaled_to_angle = mp_number_scaled_to_angle; |
| 275 |
math->angle_to_scaled = mp_number_angle_to_scaled; |
| 276 |
math->init_randoms = mp_init_randoms; |
| 277 |
math->sin_cos = mp_double_sin_cos; |
| 278 |
math->slow_add = mp_double_slow_add; |
| 279 |
math->sqrt = mp_double_square_rt; |
| 280 |
math->print = mp_double_print_number; |
| 281 |
math->tostring = mp_double_number_tostring; |
| 282 |
math->modulo = mp_number_modulo; |
| 283 |
math->ab_vs_cd = mp_ab_vs_cd; |
| 284 |
math->crossing_point = mp_double_crossing_point; |
| 285 |
math->scan_numeric = mp_double_scan_numeric_token; |
| 286 |
math->scan_fractional = mp_double_scan_fractional_token; |
| 287 |
math->free_math = mp_free_double_math; |
| 288 |
math->set_precision = mp_double_set_precision; |
| 289 |
return (void *)math; |
| 290 |
} |
| 291 |
|
| 292 |
void mp_double_set_precision (MP mp) { |
| 293 |
(void)mp; |
| 294 |
} |
| 295 |
|
| 296 |
void mp_free_double_math (MP mp) { |
| 297 |
free_number (((math_data *)mp->math)->three_sixty_deg_t); |
| 298 |
free_number (((math_data *)mp->math)->one_eighty_deg_t); |
| 299 |
free_number (((math_data *)mp->math)->fraction_one_t); |
| 300 |
free_number (((math_data *)mp->math)->zero_t); |
| 301 |
free_number (((math_data *)mp->math)->half_unit_t); |
| 302 |
free_number (((math_data *)mp->math)->three_quarter_unit_t); |
| 303 |
free_number (((math_data *)mp->math)->unity_t); |
| 304 |
free_number (((math_data *)mp->math)->two_t); |
| 305 |
free_number (((math_data *)mp->math)->three_t); |
| 306 |
free_number (((math_data *)mp->math)->one_third_inf_t); |
| 307 |
free_number (((math_data *)mp->math)->inf_t); |
| 308 |
free_number (((math_data *)mp->math)->warning_limit_t); |
| 309 |
free_number (((math_data *)mp->math)->one_k); |
| 310 |
free_number (((math_data *)mp->math)->sqrt_8_e_k); |
| 311 |
free_number (((math_data *)mp->math)->twelve_ln_2_k); |
| 312 |
free_number (((math_data *)mp->math)->coef_bound_k); |
| 313 |
free_number (((math_data *)mp->math)->coef_bound_minus_1); |
| 314 |
free_number (((math_data *)mp->math)->fraction_threshold_t); |
| 315 |
free_number (((math_data *)mp->math)->half_fraction_threshold_t); |
| 316 |
free_number (((math_data *)mp->math)->scaled_threshold_t); |
| 317 |
free_number (((math_data *)mp->math)->half_scaled_threshold_t); |
| 318 |
free_number (((math_data *)mp->math)->near_zero_angle_t); |
| 319 |
free_number (((math_data *)mp->math)->p_over_v_threshold_t); |
| 320 |
free_number (((math_data *)mp->math)->equation_threshold_t); |
| 321 |
free_number (((math_data *)mp->math)->tfm_warn_threshold_t); |
| 322 |
free(mp->math); |
| 323 |
} |
| 324 |
|
| 325 |
@ Creating an destroying |mp_number| objects |
| 326 |
|
| 327 |
@ @c |
| 328 |
void mp_new_number (MP mp, mp_number *n, mp_number_type t) { |
| 329 |
(void)mp; |
| 330 |
n->data.dval = 0.0; |
| 331 |
n->type = t; |
| 332 |
} |
| 333 |
|
| 334 |
@ |
| 335 |
|
| 336 |
@c |
| 337 |
void mp_free_number (MP mp, mp_number *n) { |
| 338 |
(void)mp; |
| 339 |
n->type = mp_nan_type; |
| 340 |
} |
| 341 |
|
| 342 |
@ Here are the low-level functions on |mp_number| items, setters first. |
| 343 |
|
| 344 |
@c |
| 345 |
void mp_set_double_from_int(mp_number *A, integer64 B) { |
| 346 |
A->data.dval = (double)B; |
| 347 |
} |
| 348 |
void mp_set_double_from_boolean(mp_number *A, integer64 B) { |
| 349 |
A->data.dval = (double)B; |
| 350 |
} |
| 351 |
void mp_set_double_from_scaled(mp_number *A, integer64 B) { |
| 352 |
A->data.dval = (double)B / 65536.0; |
| 353 |
} |
| 354 |
void mp_set_double_from_double(mp_number *A, double B) { |
| 355 |
A->data.dval = B; |
| 356 |
} |
| 357 |
void mp_set_double_from_addition(mp_number *A, mp_number B, mp_number C) { |
| 358 |
A->data.dval = B.data.dval+C.data.dval; |
| 359 |
} |
| 360 |
void mp_set_double_from_substraction (mp_number *A, mp_number B, mp_number C) { |
| 361 |
A->data.dval = B.data.dval-C.data.dval; |
| 362 |
} |
| 363 |
void mp_set_double_from_div(mp_number *A, mp_number B, mp_number C) { |
| 364 |
A->data.dval = B.data.dval / C.data.dval; |
| 365 |
} |
| 366 |
void mp_set_double_from_mul(mp_number *A, mp_number B, mp_number C) { |
| 367 |
A->data.dval = B.data.dval * C.data.dval; |
| 368 |
} |
| 369 |
void mp_set_double_from_int_div(mp_number *A, mp_number B, integer64 C) { |
| 370 |
A->data.dval = B.data.dval / (double)C; |
| 371 |
} |
| 372 |
void mp_set_double_from_int_mul(mp_number *A, mp_number B, integer64 C) { |
| 373 |
A->data.dval = B.data.dval * (double)C; |
| 374 |
} |
| 375 |
void mp_set_double_from_of_the_way(MP mp, mp_number *A, mp_number t, mp_number B, mp_number C) { |
| 376 |
A->data.dval = B.data.dval - mp_double_take_fraction(mp, (B.data.dval - C.data.dval), t.data.dval); |
| 377 |
} |
| 378 |
void mp_number_negate(mp_number *A) { |
| 379 |
A->data.dval = -A->data.dval; |
| 380 |
if (A->data.dval == -0.0) |
| 381 |
A->data.dval = 0.0; |
| 382 |
} |
| 383 |
void mp_number_add(mp_number *A, mp_number B) { |
| 384 |
A->data.dval = A->data.dval + B.data.dval; |
| 385 |
} |
| 386 |
void mp_number_substract(mp_number *A, mp_number B) { |
| 387 |
A->data.dval = A->data.dval - B.data.dval; |
| 388 |
} |
| 389 |
void mp_number_half(mp_number *A) { |
| 390 |
A->data.dval = A->data.dval/2.0; |
| 391 |
} |
| 392 |
void mp_number_halfp(mp_number *A) { |
| 393 |
A->data.dval = (A->data.dval/2.0); |
| 394 |
} |
| 395 |
void mp_number_double(mp_number *A) { |
| 396 |
A->data.dval = A->data.dval * 2.0; |
| 397 |
} |
| 398 |
void mp_number_add_scaled(mp_number *A, integer64 B) { /* also for negative B */ |
| 399 |
A->data.dval = A->data.dval + ((double)B/65536.0); |
| 400 |
} |
| 401 |
void mp_number_multiply_int(mp_number *A, integer64 B) { |
| 402 |
A->data.dval = (double)(A->data.dval * (double)B); |
| 403 |
} |
| 404 |
void mp_number_divide_int(mp_number *A, integer64 B) { |
| 405 |
A->data.dval = A->data.dval / (double)B; |
| 406 |
} |
| 407 |
void mp_double_abs(mp_number *A) { |
| 408 |
A->data.dval = fabs(A->data.dval); |
| 409 |
} |
| 410 |
void mp_number_clone(mp_number *A, mp_number B) { |
| 411 |
A->data.dval = B.data.dval; |
| 412 |
} |
| 413 |
void mp_number_swap(mp_number *A, mp_number *B) { |
| 414 |
double swap_tmp = A->data.dval; |
| 415 |
A->data.dval = B->data.dval; |
| 416 |
B->data.dval = swap_tmp; |
| 417 |
} |
| 418 |
void mp_number_fraction_to_scaled (mp_number *A) { |
| 419 |
A->type = mp_scaled_type; |
| 420 |
A->data.dval = A->data.dval / fraction_multiplier; |
| 421 |
} |
| 422 |
void mp_number_angle_to_scaled (mp_number *A) { |
| 423 |
A->type = mp_scaled_type; |
| 424 |
A->data.dval = A->data.dval / angle_multiplier; |
| 425 |
} |
| 426 |
void mp_number_scaled_to_fraction (mp_number *A) { |
| 427 |
A->type = mp_fraction_type; |
| 428 |
A->data.dval = A->data.dval * fraction_multiplier; |
| 429 |
} |
| 430 |
void mp_number_scaled_to_angle (mp_number *A) { |
| 431 |
A->type = mp_angle_type; |
| 432 |
A->data.dval = A->data.dval * angle_multiplier; |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
@ Query functions |
| 437 |
|
| 438 |
@c |
| 439 |
integer64 mp_number_to_scaled(mp_number A) { |
| 440 |
return (integer64)ROUND(A.data.dval * 65536.0); |
| 441 |
} |
| 442 |
integer64 mp_number_to_int(mp_number A) { |
| 443 |
return (integer64)(A.data.dval); |
| 444 |
} |
| 445 |
integer64 mp_number_to_boolean(mp_number A) { |
| 446 |
return (integer64)(A.data.dval); |
| 447 |
} |
| 448 |
double mp_number_to_double(mp_number A) { |
| 449 |
return A.data.dval; |
| 450 |
} |
| 451 |
int mp_number_odd(mp_number A) { |
| 452 |
return odd((int)ROUND(A.data.dval * 65536.0)); |
| 453 |
} |
| 454 |
int mp_number_equal(mp_number A, mp_number B) { |
| 455 |
return (A.data.dval==B.data.dval); |
| 456 |
} |
| 457 |
int mp_number_greater(mp_number A, mp_number B) { |
| 458 |
return (A.data.dval>B.data.dval); |
| 459 |
} |
| 460 |
int mp_number_less(mp_number A, mp_number B) { |
| 461 |
return (A.data.dval<B.data.dval); |
| 462 |
} |
| 463 |
int mp_number_nonequalabs(mp_number A, mp_number B) { |
| 464 |
return (!(fabs(A.data.dval)==fabs(B.data.dval))); |
| 465 |
} |
| 466 |
|
| 467 |
@ Fixed-point arithmetic is done on {\sl scaled integers\/} that are multiples |
| 468 |
of $2^{-16}$. In other words, a binary point is assumed to be sixteen bit |
| 469 |
positions from the right end of a binary computer word. |
| 470 |
|
| 471 |
@d unity 1.0 |
| 472 |
@d two 2.0 |
| 473 |
@d three 3.0 |
| 474 |
@d half_unit 0.5 |
| 475 |
@d three_quarter_unit 0.75 |
| 476 |
|
| 477 |
@d EL_GORDO (DBL_MAX/2.0-1.0) /* the largest value that \MP\ likes. */ |
| 478 |
@d one_third_EL_GORDO (EL_GORDO/3.0) |
| 479 |
|
| 480 |
@ One of \MP's most common operations is the calculation of |
| 481 |
$\lfloor{a+b\over2}\rfloor$, |
| 482 |
the midpoint of two given integers |a| and~|b|. The most decent way to do |
| 483 |
this is to write `|(a+b)/2|'; but on many machines it is more efficient |
| 484 |
to calculate `|(a+b)>>1|'. |
| 485 |
|
| 486 |
Therefore the midpoint operation will always be denoted by `|half(a+b)|' |
| 487 |
in this program. If \MP\ is being implemented with languages that permit |
| 488 |
binary shifting, the |half| macro should be changed to make this operation |
| 489 |
as efficient as possible. Since some systems have shift operators that can |
| 490 |
only be trusted to work on positive numbers, there is also a macro |halfp| |
| 491 |
that is used only when the quantity being halved is known to be positive |
| 492 |
or zero. |
| 493 |
|
| 494 |
@ Here is a procedure analogous to |print_int|. The current version |
| 495 |
is fairly stupid, and it is not round-trip safe, but this is good |
| 496 |
enough for a beta test. |
| 497 |
|
| 498 |
@c |
| 499 |
char * mp_double_number_tostring (MP mp, mp_number n) { |
| 500 |
static char set[64]; |
| 501 |
int l = 0; |
| 502 |
char *ret = mp_xmalloc(mp, 64, 1); |
| 503 |
snprintf(set, 64, "%.17g", n.data.dval); |
| 504 |
while (set[l] == ' ') l++; |
| 505 |
strcpy(ret, set+l); |
| 506 |
return ret; |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
@ @c |
| 511 |
void mp_double_print_number (MP mp, mp_number n) { |
| 512 |
char *str = mp_double_number_tostring(mp, n); |
| 513 |
mp_print (mp, str); |
| 514 |
free (str); |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
@ Addition is not always checked to make sure that it doesn't overflow, |
| 521 |
but in places where overflow isn't too unlikely the |slow_add| routine |
| 522 |
is used. |
| 523 |
|
| 524 |
@c |
| 525 |
void mp_double_slow_add (MP mp, mp_number *ret, mp_number x_orig, mp_number y_orig) { |
| 526 |
double x, y; |
| 527 |
x = x_orig.data.dval; |
| 528 |
y = y_orig.data.dval; |
| 529 |
if (x >= 0) { |
| 530 |
if (y <= EL_GORDO - x) { |
| 531 |
ret->data.dval = x + y; |
| 532 |
} else { |
| 533 |
mp->arith_error = true; |
| 534 |
ret->data.dval = EL_GORDO; |
| 535 |
} |
| 536 |
} else if (-y <= EL_GORDO + x) { |
| 537 |
ret->data.dval = x + y; |
| 538 |
} else { |
| 539 |
mp->arith_error = true; |
| 540 |
ret->data.dval = -EL_GORDO; |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
@ The |make_fraction| routine produces the |fraction| equivalent of |
| 545 |
|p/q|, given integers |p| and~|q|; it computes the integer |
| 546 |
$f=\lfloor2^{28}p/q+{1\over2}\rfloor$, when $p$ and $q$ are |
| 547 |
positive. If |p| and |q| are both of the same scaled type |t|, |
| 548 |
the ``type relation'' |make_fraction(t,t)=fraction| is valid; |
| 549 |
and it's also possible to use the subroutine ``backwards,'' using |
| 550 |
the relation |make_fraction(t,fraction)=t| between scaled types. |
| 551 |
|
| 552 |
If the result would have magnitude $2^{31}$ or more, |make_fraction| |
| 553 |
sets |arith_error:=true|. Most of \MP's internal computations have |
| 554 |
been designed to avoid this sort of error. |
| 555 |
|
| 556 |
If this subroutine were programmed in assembly language on a typical |
| 557 |
machine, we could simply compute |(@t$2^{28}$@>*p)div q|, since a |
| 558 |
double-precision product can often be input to a fixed-point division |
| 559 |
instruction. But when we are restricted to int-eger arithmetic it |
| 560 |
is necessary either to resort to multiple-precision maneuvering |
| 561 |
or to use a simple but slow iteration. The multiple-precision technique |
| 562 |
would be about three times faster than the code adopted here, but it |
| 563 |
would be comparatively long and tricky, involving about sixteen |
| 564 |
additional multiplications and divisions. |
| 565 |
|
| 566 |
This operation is part of \MP's ``inner loop''; indeed, it will |
| 567 |
consume nearly 10\pct! of the running time (exclusive of input and output) |
| 568 |
if the code below is left unchanged. A machine-dependent recoding |
| 569 |
will therefore make \MP\ run faster. The present implementation |
| 570 |
is highly portable, but slow; it avoids multiplication and division |
| 571 |
except in the initial stage. System wizards should be careful to |
| 572 |
replace it with a routine that is guaranteed to produce identical |
| 573 |
results in all cases. |
| 574 |
@^system dependencies@> |
| 575 |
|
| 576 |
As noted below, a few more routines should also be replaced by machine-dependent |
| 577 |
code, for efficiency. But when a procedure is not part of the ``inner loop,'' |
| 578 |
such changes aren't advisable; simplicity and robustness are |
| 579 |
preferable to trickery, unless the cost is too high. |
| 580 |
@^inner loop@> |
| 581 |
|
| 582 |
@c |
| 583 |
double mp_double_make_fraction (MP mp, double p, double q) { |
| 584 |
(void)mp; |
| 585 |
return ((p / q) * fraction_multiplier); |
| 586 |
} |
| 587 |
void mp_double_number_make_fraction (MP mp, mp_number *ret, mp_number p, mp_number q) { |
| 588 |
ret->data.dval = mp_double_make_fraction (mp, p.data.dval, q.data.dval); |
| 589 |
} |
| 590 |
|
| 591 |
@ @<Declarations@>= |
| 592 |
double mp_double_make_fraction (MP mp, double p, double q); |
| 593 |
|
| 594 |
@ The dual of |make_fraction| is |take_fraction|, which multiplies a |
| 595 |
given integer~|q| by a fraction~|f|. When the operands are positive, it |
| 596 |
computes $p=\lfloor qf/2^{28}+{1\over2}\rfloor$, a symmetric function |
| 597 |
of |q| and~|f|. |
| 598 |
|
| 599 |
This routine is even more ``inner loopy'' than |make_fraction|; |
| 600 |
the present implementation consumes almost 20\pct! of \MP's computation |
| 601 |
time during typical jobs, so a machine-language substitute is advisable. |
| 602 |
@^inner loop@> @^system dependencies@> |
| 603 |
|
| 604 |
@c |
| 605 |
double mp_double_take_fraction (MP mp, double p, double q) { |
| 606 |
(void)mp; |
| 607 |
return ((p * q) / fraction_multiplier); |
| 608 |
} |
| 609 |
void mp_double_number_take_fraction (MP mp, mp_number *ret, mp_number p, mp_number q) { |
| 610 |
ret->data.dval = mp_double_take_fraction (mp, p.data.dval, q.data.dval); |
| 611 |
} |
| 612 |
|
| 613 |
@ @<Declarations@>= |
| 614 |
double mp_double_take_fraction (MP mp, double p, double q); |
| 615 |
|
| 616 |
@ When we want to multiply something by a |scaled| quantity, we use a scheme |
| 617 |
analogous to |take_fraction| but with a different scaling. |
| 618 |
Given positive operands, |take_scaled| |
| 619 |
computes the quantity $p=\lfloor qf/2^{16}+{1\over2}\rfloor$. |
| 620 |
|
| 621 |
Once again it is a good idea to use a machine-language replacement if |
| 622 |
possible; otherwise |take_scaled| will use more than 2\pct! of the running time |
| 623 |
when the Computer Modern fonts are being generated. |
| 624 |
@^inner loop@> |
| 625 |
|
| 626 |
@c |
| 627 |
void mp_double_number_take_scaled (MP mp, mp_number *ret, mp_number p_orig, mp_number q_orig) { |
| 628 |
(void)mp; |
| 629 |
ret->data.dval = p_orig.data.dval * q_orig.data.dval; |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
@ For completeness, there's also |make_scaled|, which computes a |
| 634 |
quotient as a |scaled| number instead of as a |fraction|. |
| 635 |
In other words, the result is $\lfloor2^{16}p/q+{1\over2}\rfloor$, if the |
| 636 |
operands are positive. \ (This procedure is not used especially often, |
| 637 |
so it is not part of \MP's inner loop.) |
| 638 |
|
| 639 |
@c |
| 640 |
double mp_double_make_scaled (MP mp, double p, double q) { |
| 641 |
(void)mp; |
| 642 |
return p / q; |
| 643 |
} |
| 644 |
void mp_double_number_make_scaled (MP mp, mp_number *ret, mp_number p_orig, mp_number q_orig) { |
| 645 |
(void)mp; |
| 646 |
ret->data.dval = p_orig.data.dval / q_orig.data.dval; |
| 647 |
} |
| 648 |
|
| 649 |
@ @<Declarations@>= |
| 650 |
double mp_double_make_scaled (MP mp, double p, double q); |
| 651 |
|
| 652 |
|
| 653 |
@ |
| 654 |
@d halfp(A) (integer)((unsigned)(A) >> 1) |
| 655 |
|
| 656 |
@* Scanning numbers in the input. |
| 657 |
|
| 658 |
The definitions below are temporarily here |
| 659 |
|
| 660 |
@d set_cur_cmd(A) mp->cur_mod_->type=(A) |
| 661 |
@d set_cur_mod(A) mp->cur_mod_->data.n.data.dval=(A) |
| 662 |
|
| 663 |
@<Declarations...@>= |
| 664 |
static void mp_wrapup_numeric_token(MP mp, unsigned char *start, unsigned char *stop); |
| 665 |
|
| 666 |
@ @c |
| 667 |
void mp_wrapup_numeric_token(MP mp, unsigned char *start, unsigned char *stop) { |
| 668 |
double result; |
| 669 |
char *end = (char *)stop; |
| 670 |
errno = 0; |
| 671 |
result = strtod ((char *)start, &end); |
| 672 |
if (errno == 0) { |
| 673 |
set_cur_mod(result); |
| 674 |
if (result >= warning_limit) { |
| 675 |
if (internal_value (mp_warning_check).data.dval > 0 && |
| 676 |
(mp->scanner_status != tex_flushing)) { |
| 677 |
char msg[256]; |
| 678 |
const char *hlp[] = {"Continue and I'll try to cope", |
| 679 |
"with that big value; but it might be dangerous.", |
| 680 |
"(Set warningcheck:=0 to suppress this message.)", |
| 681 |
NULL }; |
| 682 |
mp_snprintf (msg, 256, "Number is too large (%g)", result); |
| 683 |
@.Number is too large@>; |
| 684 |
mp_error (mp, msg, hlp, true); |
| 685 |
} |
| 686 |
} |
| 687 |
} else if (mp->scanner_status != tex_flushing) { |
| 688 |
const char *hlp[] = {"I could not handle this number specification", |
| 689 |
"probably because it is out of range. Error:", |
| 690 |
"", |
| 691 |
NULL }; |
| 692 |
hlp[2] = strerror(errno); |
| 693 |
mp_error (mp, "Enormous number has been reduced.", hlp, false); |
| 694 |
@.Enormous number...@>; |
| 695 |
set_cur_mod(EL_GORDO); |
| 696 |
} |
| 697 |
set_cur_cmd((mp_variable_type)mp_numeric_token); |
| 698 |
} |
| 699 |
|
| 700 |
@ @c |
| 701 |
static void find_exponent (MP mp) { |
| 702 |
if (mp->buffer[mp->cur_input.loc_field] == 'e' || |
| 703 |
mp->buffer[mp->cur_input.loc_field] == 'E') { |
| 704 |
mp->cur_input.loc_field++; |
| 705 |
if (!(mp->buffer[mp->cur_input.loc_field] == '+' || |
| 706 |
mp->buffer[mp->cur_input.loc_field] == '-' || |
| 707 |
mp->char_class[mp->buffer[mp->cur_input.loc_field]] == digit_class)) { |
| 708 |
mp->cur_input.loc_field--; |
| 709 |
return; |
| 710 |
} |
| 711 |
if (mp->buffer[mp->cur_input.loc_field] == '+' || |
| 712 |
mp->buffer[mp->cur_input.loc_field] == '-') { |
| 713 |
mp->cur_input.loc_field++; |
| 714 |
} |
| 715 |
while (mp->char_class[mp->buffer[mp->cur_input.loc_field]] == digit_class) { |
| 716 |
mp->cur_input.loc_field++; |
| 717 |
} |
| 718 |
} |
| 719 |
} |
| 720 |
void mp_double_scan_fractional_token (MP mp, integer64 n) { /* n: scaled */ |
| 721 |
(void)n; |
| 722 |
unsigned char *start = &mp->buffer[mp->cur_input.loc_field -1]; |
| 723 |
unsigned char *stop; |
| 724 |
while (mp->char_class[mp->buffer[mp->cur_input.loc_field]] == digit_class) { |
| 725 |
mp->cur_input.loc_field++; |
| 726 |
} |
| 727 |
find_exponent(mp); |
| 728 |
stop = &mp->buffer[mp->cur_input.loc_field-1]; |
| 729 |
mp_wrapup_numeric_token (mp, start, stop); |
| 730 |
} |
| 731 |
|
| 732 |
|
| 733 |
@ Input format is the same as for the C language, so we just collect valid |
| 734 |
bytes in the buffer, then call |strtod()| |
| 735 |
|
| 736 |
@c |
| 737 |
void mp_double_scan_numeric_token (MP mp, integer64 n) { /* n: scaled */ |
| 738 |
(void)n; |
| 739 |
unsigned char *start = &mp->buffer[mp->cur_input.loc_field -1]; |
| 740 |
unsigned char *stop; |
| 741 |
while (mp->char_class[mp->buffer[mp->cur_input.loc_field]] == digit_class) { |
| 742 |
mp->cur_input.loc_field++; |
| 743 |
} |
| 744 |
if (mp->buffer[mp->cur_input.loc_field] == '.' && |
| 745 |
mp->buffer[mp->cur_input.loc_field+1] != '.') { |
| 746 |
mp->cur_input.loc_field++; |
| 747 |
while (mp->char_class[mp->buffer[mp->cur_input.loc_field]] == digit_class) { |
| 748 |
mp->cur_input.loc_field++; |
| 749 |
} |
| 750 |
} |
| 751 |
find_exponent(mp); |
| 752 |
stop = &mp->buffer[mp->cur_input.loc_field-1]; |
| 753 |
mp_wrapup_numeric_token (mp, start, stop); |
| 754 |
} |
| 755 |
|
| 756 |
@ The |scaled| quantities in \MP\ programs are generally supposed to be |
| 757 |
less than $2^{12}$ in absolute value, so \MP\ does much of its internal |
| 758 |
arithmetic with 28~significant bits of precision. A |fraction| denotes |
| 759 |
a scaled integer whose binary point is assumed to be 28 bit positions |
| 760 |
from the right. |
| 761 |
|
| 762 |
@d fraction_half (0.5*fraction_multiplier) |
| 763 |
@d fraction_one (1.0*fraction_multiplier) |
| 764 |
@d fraction_two (2.0*fraction_multiplier) |
| 765 |
@d fraction_three (3.0*fraction_multiplier) |
| 766 |
@d fraction_four (4.0*fraction_multiplier) |
| 767 |
|
| 768 |
@ Here is a typical example of how the routines above can be used. |
| 769 |
It computes the function |
| 770 |
$${1\over3\tau}f(\theta,\phi)= |
| 771 |
{\tau^{-1}\bigl(2+\sqrt2\,(\sin\theta-{1\over16}\sin\phi) |
| 772 |
(\sin\phi-{1\over16}\sin\theta)(\cos\theta-\cos\phi)\bigr)\over |
| 773 |
3\,\bigl(1+{1\over2}(\sqrt5-1)\cos\theta+{1\over2}(3-\sqrt5\,)\cos\phi\bigr)},$$ |
| 774 |
where $\tau$ is a |scaled| ``tension'' parameter. This is \MP's magic |
| 775 |
fudge factor for placing the first control point of a curve that starts |
| 776 |
at an angle $\theta$ and ends at an angle $\phi$ from the straight path. |
| 777 |
(Actually, if the stated quantity exceeds 4, \MP\ reduces it to~4.) |
| 778 |
|
| 779 |
The trigonometric quantity to be multiplied by $\sqrt2$ is less than $\sqrt2$. |
| 780 |
(It's a sum of eight terms whose absolute values can be bounded using |
| 781 |
relations such as $\sin\theta\cos\theta\L{1\over2}$.) Thus the numerator |
| 782 |
is positive; and since the tension $\tau$ is constrained to be at least |
| 783 |
$3\over4$, the numerator is less than $16\over3$. The denominator is |
| 784 |
nonnegative and at most~6. |
| 785 |
|
| 786 |
The angles $\theta$ and $\phi$ are given implicitly in terms of |fraction| |
| 787 |
arguments |st|, |ct|, |sf|, and |cf|, representing $\sin\theta$, $\cos\theta$, |
| 788 |
$\sin\phi$, and $\cos\phi$, respectively. |
| 789 |
|
| 790 |
@c |
| 791 |
void mp_double_velocity (MP mp, mp_number *ret, mp_number st, mp_number ct, mp_number sf, |
| 792 |
mp_number cf, mp_number t) { |
| 793 |
double acc, num, denom; /* registers for intermediate calculations */ |
| 794 |
acc = mp_double_take_fraction (mp, st.data.dval - (sf.data.dval / 16.0), |
| 795 |
sf.data.dval - (st.data.dval / 16.0)); |
| 796 |
acc = mp_double_take_fraction (mp, acc, ct.data.dval - cf.data.dval); |
| 797 |
num = fraction_two + mp_double_take_fraction (mp, acc, sqrt(2)*fraction_one); |
| 798 |
denom = |
| 799 |
fraction_three + mp_double_take_fraction (mp, ct.data.dval, 3*fraction_half*(sqrt(5.0)-1.0)) |
| 800 |
+ mp_double_take_fraction (mp, cf.data.dval, 3*fraction_half*(3.0-sqrt(5.0))); |
| 801 |
if (t.data.dval != unity) |
| 802 |
num = mp_double_make_scaled (mp, num, t.data.dval); |
| 803 |
if (num / 4 >= denom) { |
| 804 |
ret->data.dval = fraction_four; |
| 805 |
} else { |
| 806 |
ret->data.dval = mp_double_make_fraction (mp, num, denom); |
| 807 |
} |
| 808 |
#if MPOST_DEBUG |
| 809 |
fprintf(stdout, "\n%f = velocity(%f,%f,%f,%f,%f)", mp_number_to_double(*ret), |
| 810 |
mp_number_to_double(st),mp_number_to_double(ct), |
| 811 |
mp_number_to_double(sf),mp_number_to_double(cf), |
| 812 |
mp_number_to_double(t)); |
| 813 |
#endif |
| 814 |
} |
| 815 |
|
| 816 |
|
| 817 |
@ The following somewhat different subroutine tests rigorously if $ab$ is |
| 818 |
greater than, equal to, or less than~$cd$, |
| 819 |
given integers $(a,b,c,d)$. In most cases a quick decision is reached. |
| 820 |
The result is $+1$, 0, or~$-1$ in the three respective cases. |
| 821 |
|
| 822 |
@c |
| 823 |
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) { |
| 824 |
/*integer q, r; */ /* temporary registers */ |
| 825 |
/*integer a, b, c, d;*/ |
| 826 |
(void)mp; |
| 827 |
|
| 828 |
mp_double_ab_vs_cd(mp,ret, a_orig, b_orig, c_orig, d_orig); |
| 829 |
if (1>0) |
| 830 |
return ; |
| 831 |
/* TODO: remove this code until the end */ |
| 832 |
/* a = a_orig.data.dval; */ |
| 833 |
/* b = b_orig.data.dval; */ |
| 834 |
/* c = c_orig.data.dval; */ |
| 835 |
/* d = d_orig.data.dval; */ |
| 836 |
/* <Reduce to the case that |a,c>=0|, |b,d>0| >; */ |
| 837 |
/* while (1) \{ */ |
| 838 |
/* q = a / d; */ |
| 839 |
/* r = c / b; */ |
| 840 |
/* if (q != r) \{ */ |
| 841 |
/* ret->data.dval = (q > r ? 1 : -1); */ |
| 842 |
/* goto RETURN; */ |
| 843 |
/* \} */ |
| 844 |
/* q = a % d; */ |
| 845 |
/* r = c % b; */ |
| 846 |
/* if (r == 0) \{ */ |
| 847 |
/* ret->data.dval = (q ? 1 : 0); */ |
| 848 |
/* goto RETURN; */ |
| 849 |
/* \} */ |
| 850 |
/* if (q == 0) \{ */ |
| 851 |
/* ret->data.dval = -1; */ |
| 852 |
/* goto RETURN; */ |
| 853 |
/* \} */ |
| 854 |
/* a = b; */ |
| 855 |
/* b = q; */ |
| 856 |
/* c = d; */ |
| 857 |
/* d = r; */ |
| 858 |
/* \} /\* now |a>d>0| and |c>b>0| *\/ */ |
| 859 |
/* RETURN: */ |
| 860 |
/* \#if MPOST_DEBUG */ |
| 861 |
/* fprintf(stdout, "\n%f = ab_vs_cd(%f,%f,%f,%f)", mp_number_to_double(*ret), */ |
| 862 |
/* mp_number_to_double(a_orig),mp_number_to_double(b_orig), */ |
| 863 |
/* mp_number_to_double(c_orig),mp_number_to_double(d_orig)); */ |
| 864 |
/* \#endif */ |
| 865 |
return; |
| 866 |
} |
| 867 |
|
| 868 |
|
| 869 |
/* <Reduce to the case that \|a...>= */ |
| 870 |
/* if (a < 0) \{ */ |
| 871 |
/* a = -a; */ |
| 872 |
/* b = -b; */ |
| 873 |
/* \} */ |
| 874 |
/* if (c < 0) \{ */ |
| 875 |
/* c = -c; */ |
| 876 |
/* d = -d; */ |
| 877 |
/* \} */ |
| 878 |
/* if (d <= 0) \{ */ |
| 879 |
/* if (b >= 0) \{ */ |
| 880 |
/* if ((a == 0 || b == 0) && (c == 0 || d == 0)) */ |
| 881 |
/* ret->data.dval = 0; */ |
| 882 |
/* else */ |
| 883 |
/* ret->data.dval = 1; */ |
| 884 |
/* goto RETURN; */ |
| 885 |
/* \} */ |
| 886 |
/* if (d == 0) \{ */ |
| 887 |
/* ret->data.dval = (a == 0 ? 0 : -1); */ |
| 888 |
/* goto RETURN; */ |
| 889 |
/* \} */ |
| 890 |
/* q = a; */ |
| 891 |
/* a = c; */ |
| 892 |
/* c = q; */ |
| 893 |
/* q = -b; */ |
| 894 |
/* b = -d; */ |
| 895 |
/* d = q; */ |
| 896 |
/* \} else if (b <= 0) \{ */ |
| 897 |
/* if (b < 0 && a > 0) \{ */ |
| 898 |
/* ret->data.dval = -1; */ |
| 899 |
/* return; */ |
| 900 |
/* \} */ |
| 901 |
/* ret->data.dval = (c == 0 ? 0 : -1); */ |
| 902 |
/* goto RETURN; */ |
| 903 |
/* \} */ |
| 904 |
|
| 905 |
@ Now here's a subroutine that's handy for all sorts of path computations: |
| 906 |
Given a quadratic polynomial $B(a,b,c;t)$, the |crossing_point| function |
| 907 |
returns the unique |fraction| value |t| between 0 and~1 at which |
| 908 |
$B(a,b,c;t)$ changes from positive to negative, or returns |
| 909 |
|t=fraction_one+1| if no such value exists. If |a<0| (so that $B(a,b,c;t)$ |
| 910 |
is already negative at |t=0|), |crossing_point| returns the value zero. |
| 911 |
|
| 912 |
The general bisection method is quite simple when $n=2$, hence |
| 913 |
|crossing_point| does not take much time. At each stage in the |
| 914 |
recursion we have a subinterval defined by |l| and~|j| such that |
| 915 |
$B(a,b,c;2^{-l}(j+t))=B(x_0,x_1,x_2;t)$, and we want to ``zero in'' on |
| 916 |
the subinterval where $x_0\G0$ and $\min(x_1,x_2)<0$. |
| 917 |
|
| 918 |
It is convenient for purposes of calculation to combine the values |
| 919 |
of |l| and~|j| in a single variable $d=2^l+j$, because the operation |
| 920 |
of bisection then corresponds simply to doubling $d$ and possibly |
| 921 |
adding~1. Furthermore it proves to be convenient to modify |
| 922 |
our previous conventions for bisection slightly, maintaining the |
| 923 |
variables $X_0=2^lx_0$, $X_1=2^l(x_0-x_1)$, and $X_2=2^l(x_1-x_2)$. |
| 924 |
With these variables the conditions $x_0\ge0$ and $\min(x_1,x_2)<0$ are |
| 925 |
equivalent to $\max(X_1,X_1+X_2)>X_0\ge0$. |
| 926 |
|
| 927 |
The following code maintains the invariant relations |
| 928 |
$0\L|x0|<\max(|x1|,|x1|+|x2|)$, |
| 929 |
$\vert|x1|\vert<2^{30}$, $\vert|x2|\vert<2^{30}$; |
| 930 |
it has been constructed in such a way that no arithmetic overflow |
| 931 |
will occur if the inputs satisfy |
| 932 |
$a<2^{30}$, $\vert a-b\vert<2^{30}$, and $\vert b-c\vert<2^{30}$. |
| 933 |
|
| 934 |
@d no_crossing { ret->data.dval = fraction_one + 1; goto RETURN; } |
| 935 |
@d one_crossing { ret->data.dval = fraction_one; goto RETURN; } |
| 936 |
@d zero_crossing { ret->data.dval = 0; goto RETURN; } |
| 937 |
|
| 938 |
@c |
| 939 |
static void mp_double_crossing_point (MP mp, mp_number *ret, mp_number aa, mp_number bb, mp_number cc) { |
| 940 |
(void)mp; |
| 941 |
double a,b,c; |
| 942 |
double d; /* recursive counter */ |
| 943 |
double x, xx, x0, x1, x2; /* temporary registers for bisection */ |
| 944 |
a = aa.data.dval; |
| 945 |
b = bb.data.dval; |
| 946 |
c = cc.data.dval; |
| 947 |
if (a < 0) |
| 948 |
zero_crossing; |
| 949 |
if (c >= 0) { |
| 950 |
if (b >= 0) { |
| 951 |
if (c > 0) { |
| 952 |
no_crossing; |
| 953 |
} else if ((a == 0) && (b == 0)) { |
| 954 |
no_crossing; |
| 955 |
} else { |
| 956 |
one_crossing; |
| 957 |
} |
| 958 |
} |
| 959 |
if (a == 0) |
| 960 |
zero_crossing; |
| 961 |
} else if (a == 0) { |
| 962 |
if (b <= 0) |
| 963 |
zero_crossing; |
| 964 |
} |
| 965 |
|
| 966 |
/* Use bisection to find the crossing point... */ |
| 967 |
d = epsilon; |
| 968 |
x0 = a; |
| 969 |
x1 = a - b; |
| 970 |
x2 = b - c; |
| 971 |
do { |
| 972 |
/* not sure why the error correction has to be >= 1E-12 */ |
| 973 |
x = (x1 + x2) / 2 + 1E-12; |
| 974 |
if (x1 - x0 > x0) { |
| 975 |
x2 = x; |
| 976 |
x0 += x0; |
| 977 |
d += d; |
| 978 |
} else { |
| 979 |
xx = x1 + x - x0; |
| 980 |
if (xx > x0) { |
| 981 |
x2 = x; |
| 982 |
x0 += x0; |
| 983 |
d += d; |
| 984 |
} else { |
| 985 |
x0 = x0 - xx; |
| 986 |
if (x <= x0) { |
| 987 |
if (x + x2 <= x0) |
| 988 |
no_crossing; |
| 989 |
} |
| 990 |
x1 = x; |
| 991 |
d = d + d + epsilon; |
| 992 |
} |
| 993 |
} |
| 994 |
} while (d < fraction_one); |
| 995 |
ret->data.dval = (d - fraction_one); |
| 996 |
RETURN: |
| 997 |
#if MPOST_DEBUG |
| 998 |
fprintf(stdout, "\n%f = crossing_point(%f,%f,%f)", mp_number_to_double(*ret), |
| 999 |
mp_number_to_double(aa),mp_number_to_double(bb),mp_number_to_double(cc)); |
| 1000 |
#endif |
| 1001 |
return; |
| 1002 |
} |
| 1003 |
|
| 1004 |
|
| 1005 |
@ We conclude this set of elementary routines with some simple rounding |
| 1006 |
and truncation operations. |
| 1007 |
|
| 1008 |
|
| 1009 |
@ |round_unscaled| rounds a |scaled| and converts it to |int| |
| 1010 |
@c |
| 1011 |
integer64 mp_round_unscaled(mp_number x_orig) { |
| 1012 |
integer64 x = (integer64)ROUND(x_orig.data.dval); |
| 1013 |
return x; |
| 1014 |
} |
| 1015 |
|
| 1016 |
@ |number_floor| floors a number |
| 1017 |
|
| 1018 |
@c |
| 1019 |
void mp_number_floor (mp_number *i) { |
| 1020 |
i->data.dval = floor(i->data.dval); |
| 1021 |
} |
| 1022 |
|
| 1023 |
@ |fraction_to_scaled| rounds a |fraction| and converts it to |scaled| |
| 1024 |
@c |
| 1025 |
void mp_double_fraction_to_round_scaled (mp_number *x_orig) { |
| 1026 |
double x = x_orig->data.dval; |
| 1027 |
x_orig->type = mp_scaled_type; |
| 1028 |
x_orig->data.dval = x/fraction_multiplier; |
| 1029 |
} |
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
@* Algebraic and transcendental functions. |
| 1034 |
\MP\ computes all of the necessary special functions from scratch, without |
| 1035 |
relying on |real| arithmetic or system subroutines for sines, cosines, etc. |
| 1036 |
|
| 1037 |
@ |
| 1038 |
|
| 1039 |
@c |
| 1040 |
void mp_double_square_rt (MP mp, mp_number *ret, mp_number x_orig) { /* return, x: scaled */ |
| 1041 |
double x; |
| 1042 |
x = x_orig.data.dval; |
| 1043 |
if (x <= 0) { |
| 1044 |
@<Handle square root of zero or negative argument@>; |
| 1045 |
} else { |
| 1046 |
ret->data.dval = sqrt(x); |
| 1047 |
} |
| 1048 |
} |
| 1049 |
|
| 1050 |
|
| 1051 |
@ @<Handle square root of zero...@>= |
| 1052 |
{ |
| 1053 |
if (x < 0) { |
| 1054 |
char msg[256]; |
| 1055 |
const char *hlp[] = { |
| 1056 |
"Since I don't take square roots of negative numbers,", |
| 1057 |
"I'm zeroing this one. Proceed, with fingers crossed.", |
| 1058 |
NULL }; |
| 1059 |
char *xstr = mp_double_number_tostring (mp, x_orig); |
| 1060 |
mp_snprintf(msg, 256, "Square root of %s has been replaced by 0", xstr); |
| 1061 |
free(xstr); |
| 1062 |
@.Square root...replaced by 0@>; |
| 1063 |
mp_error (mp, msg, hlp, true); |
| 1064 |
} |
| 1065 |
ret->data.dval = 0; |
| 1066 |
return; |
| 1067 |
} |
| 1068 |
|
| 1069 |
|
| 1070 |
@ Pythagorean addition $\psqrt{a^2+b^2}$ is implemented by a quick hack |
| 1071 |
|
| 1072 |
@c |
| 1073 |
void mp_double_pyth_add (MP mp, mp_number *ret, mp_number a_orig, mp_number b_orig) { |
| 1074 |
double a, b; /* a,b : scaled */ |
| 1075 |
a = fabs (a_orig.data.dval); |
| 1076 |
b = fabs (b_orig.data.dval); |
| 1077 |
errno = 0; |
| 1078 |
ret->data.dval = sqrt(a*a + b*b); |
| 1079 |
if (errno) { |
| 1080 |
mp->arith_error = true; |
| 1081 |
ret->data.dval = EL_GORDO; |
| 1082 |
} |
| 1083 |
} |
| 1084 |
|
| 1085 |
|
| 1086 |
@ Here is a similar algorithm for $\psqrt{a^2-b^2}$. Same quick hack, also. |
| 1087 |
|
| 1088 |
@c |
| 1089 |
void mp_double_pyth_sub (MP mp, mp_number *ret, mp_number a_orig, mp_number b_orig) { |
| 1090 |
double a, b; |
| 1091 |
a = fabs (a_orig.data.dval); |
| 1092 |
b = fabs (b_orig.data.dval); |
| 1093 |
if (a <= b) { |
| 1094 |
@<Handle erroneous |pyth_sub| and set |a:=0|@>; |
| 1095 |
} else { |
| 1096 |
a = sqrt(a*a - b*b); |
| 1097 |
} |
| 1098 |
ret->data.dval = a; |
| 1099 |
} |
| 1100 |
|
| 1101 |
|
| 1102 |
@ @<Handle erroneous |pyth_sub| and set |a:=0|@>= |
| 1103 |
{ |
| 1104 |
if (a < b) { |
| 1105 |
char msg[256]; |
| 1106 |
const char *hlp[] = { |
| 1107 |
"Since I don't take square roots of negative numbers,", |
| 1108 |
"I'm zeroing this one. Proceed, with fingers crossed.", |
| 1109 |
NULL }; |
| 1110 |
char *astr = mp_double_number_tostring (mp, a_orig); |
| 1111 |
char *bstr = mp_double_number_tostring (mp, b_orig); |
| 1112 |
mp_snprintf (msg, 256, "Pythagorean subtraction %s+-+%s has been replaced by 0", astr, bstr); |
| 1113 |
free(astr); |
| 1114 |
free(bstr); |
| 1115 |
@.Pythagorean...@>; |
| 1116 |
mp_error (mp, msg, hlp, true); |
| 1117 |
} |
| 1118 |
a = 0; |
| 1119 |
} |
| 1120 |
|
| 1121 |
|
| 1122 |
@ The subroutines for logarithm and exponential involve two tables. |
| 1123 |
The first is simple: |two_to_the[k]| equals $2^k$. |
| 1124 |
|
| 1125 |
@d two_to_the(A) (1<<(unsigned)(A)) |
| 1126 |
|
| 1127 |
@ Here is the routine that calculates $2^8$ times the natural logarithm |
| 1128 |
of a |scaled| quantity; it is an integer approximation to $2^{24}\ln(x/2^{16})$, |
| 1129 |
when |x| is a given positive integer. |
| 1130 |
|
| 1131 |
@c |
| 1132 |
void mp_double_m_log (MP mp, mp_number *ret, mp_number x_orig) { |
| 1133 |
if (x_orig.data.dval <= 0) { |
| 1134 |
@<Handle non-positive logarithm@>; |
| 1135 |
} else { |
| 1136 |
ret->data.dval = log (x_orig.data.dval)*256.0; |
| 1137 |
} |
| 1138 |
} |
| 1139 |
|
| 1140 |
@ @<Handle non-positive logarithm@>= |
| 1141 |
{ |
| 1142 |
char msg[256]; |
| 1143 |
const char *hlp[] = { |
| 1144 |
"Since I don't take logs of non-positive numbers,", |
| 1145 |
"I'm zeroing this one. Proceed, with fingers crossed.", |
| 1146 |
NULL }; |
| 1147 |
char *xstr = mp_double_number_tostring (mp, x_orig); |
| 1148 |
mp_snprintf (msg, 256, "Logarithm of %s has been replaced by 0", xstr); |
| 1149 |
free (xstr); |
| 1150 |
@.Logarithm...replaced by 0@>; |
| 1151 |
mp_error (mp, msg, hlp, true); |
| 1152 |
ret->data.dval = 0; |
| 1153 |
} |
| 1154 |
|
| 1155 |
|
| 1156 |
@ Conversely, the exponential routine calculates $\exp(x/2^8)$, |
| 1157 |
when |x| is |scaled|. |
| 1158 |
|
| 1159 |
@c |
| 1160 |
void mp_double_m_exp (MP mp, mp_number *ret, mp_number x_orig) { |
| 1161 |
errno = 0; |
| 1162 |
ret->data.dval = exp(x_orig.data.dval/256.0); |
| 1163 |
if (errno) { |
| 1164 |
if (x_orig.data.dval > 0) { |
| 1165 |
mp->arith_error = true; |
| 1166 |
ret->data.dval = EL_GORDO; |
| 1167 |
} else { |
| 1168 |
ret->data.dval = 0; |
| 1169 |
} |
| 1170 |
} |
| 1171 |
} |
| 1172 |
|
| 1173 |
|
| 1174 |
@ Given integers |x| and |y|, not both zero, the |n_arg| function |
| 1175 |
returns the |angle| whose tangent points in the direction $(x,y)$. |
| 1176 |
|
| 1177 |
@c |
| 1178 |
void mp_double_n_arg (MP mp, mp_number *ret, mp_number x_orig, mp_number y_orig) { |
| 1179 |
if (x_orig.data.dval == 0.0 && y_orig.data.dval == 0.0) { |
| 1180 |
@<Handle undefined arg@>; |
| 1181 |
} else { |
| 1182 |
ret->type = mp_angle_type; |
| 1183 |
/* Consistency with the other numbersystems */ |
| 1184 |
if (x_orig.data.dval == -0.0) |
| 1185 |
x_orig.data.dval = 0.0; |
| 1186 |
if (y_orig.data.dval == -0.0) |
| 1187 |
y_orig.data.dval = 0.0; |
| 1188 |
ret->data.dval = atan2 (y_orig.data.dval, x_orig.data.dval) * (180.0 / PI) * angle_multiplier; |
| 1189 |
if (ret->data.dval == -0.0) |
| 1190 |
ret->data.dval = 0.0; |
| 1191 |
#if MPOST_DEBUG |
| 1192 |
fprintf(stdout, "\nn_arg(%g,%g,%g)", mp_number_to_double(*ret), |
| 1193 |
mp_number_to_double(x_orig),mp_number_to_double(y_orig)); |
| 1194 |
#endif |
| 1195 |
} |
| 1196 |
} |
| 1197 |
|
| 1198 |
|
| 1199 |
@ @<Handle undefined arg@>= |
| 1200 |
{ |
| 1201 |
const char *hlp[] = { |
| 1202 |
"The `angle' between two identical points is undefined.", |
| 1203 |
"I'm zeroing this one. Proceed, with fingers crossed.", |
| 1204 |
NULL }; |
| 1205 |
mp_error (mp, "angle(0,0) is taken as zero", hlp, true); |
| 1206 |
@.angle(0,0)...zero@>; |
| 1207 |
ret->data.dval = 0; |
| 1208 |
} |
| 1209 |
|
| 1210 |
|
| 1211 |
@ Conversely, the |n_sin_cos| routine takes an |angle| and produces the sine |
| 1212 |
and cosine of that angle. The results of this routine are |
| 1213 |
stored in global integer variables |n_sin| and |n_cos|. |
| 1214 |
|
| 1215 |
@ Given an integer |z| that is $2^{20}$ times an angle $\theta$ in degrees, |
| 1216 |
the purpose of |n_sin_cos(z)| is to set |
| 1217 |
|x=@t$r\cos\theta$@>| and |y=@t$r\sin\theta$@>| (approximately), |
| 1218 |
for some rather large number~|r|. The maximum of |x| and |y| |
| 1219 |
will be between $2^{28}$ and $2^{30}$, so that there will be hardly |
| 1220 |
any loss of accuracy. Then |x| and~|y| are divided by~|r|. |
| 1221 |
|
| 1222 |
@d one_eighty_deg (180.0*angle_multiplier) |
| 1223 |
@d three_sixty_deg (360.0*angle_multiplier) |
| 1224 |
|
| 1225 |
@d odd(A) (abs(A)%2==1) |
| 1226 |
|
| 1227 |
@ Compute a multiple of the sine and cosine |
| 1228 |
|
| 1229 |
@c |
| 1230 |
void mp_double_sin_cos (MP mp, mp_number z_orig, mp_number *n_cos, mp_number *n_sin) { |
| 1231 |
(void)mp; |
| 1232 |
double rad; |
| 1233 |
rad = (z_orig.data.dval / angle_multiplier); /* still degrees */ |
| 1234 |
if ((rad == 90.0)||(rad == -270)){ |
| 1235 |
n_cos->data.dval = 0.0; |
| 1236 |
n_sin->data.dval = fraction_multiplier; |
| 1237 |
} else if ((rad == -90.0)||(rad == 270.0)) { |
| 1238 |
n_cos->data.dval = 0.0; |
| 1239 |
n_sin->data.dval = -fraction_multiplier; |
| 1240 |
} else if ((rad == 180.0) || (rad == -180.0)) { |
| 1241 |
n_cos->data.dval = -fraction_multiplier; |
| 1242 |
n_sin->data.dval = 0.0; |
| 1243 |
} else { |
| 1244 |
rad = rad * PI/180.0; |
| 1245 |
n_cos->data.dval = cos(rad) * fraction_multiplier; |
| 1246 |
n_sin->data.dval = sin(rad) * fraction_multiplier; |
| 1247 |
} |
| 1248 |
#if MPOST_DEBUG |
| 1249 |
fprintf(stdout, "\nsin_cos(%f,%f,%f)", mp_number_to_double(z_orig), |
| 1250 |
mp_number_to_double(*n_cos), mp_number_to_double(*n_sin)); |
| 1251 |
#endif |
| 1252 |
} |
| 1253 |
|
| 1254 |
@ This is the http://www-cs-faculty.stanford.edu/~uno/programs/rng.c |
| 1255 |
with small cosmetic modifications. |
| 1256 |
|
| 1257 |
@c |
| 1258 |
#define KK 100 /* the long lag */ |
| 1259 |
#define LL 37 /* the short lag */ |
| 1260 |
#define MM (1L<<30) /* the modulus */ |
| 1261 |
#define mod_diff(x,y) (((x)-(y))&(MM-1)) /* subtraction mod MM */ |
| 1262 |
/* */ |
| 1263 |
static long ran_x[KK]; /* the generator state */ |
| 1264 |
/* */ |
| 1265 |
static void ran_array(long aa[],int n) /* put n new random numbers in aa */ |
| 1266 |
/* long aa[] destination */ |
| 1267 |
/* int n array length (must be at least KK) */ |
| 1268 |
{ |
| 1269 |
register int i,j; |
| 1270 |
for (j=0;j<KK;j++) aa[j]=ran_x[j]; |
| 1271 |
for (;j<n;j++) aa[j]=mod_diff(aa[j-KK],aa[j-LL]); |
| 1272 |
for (i=0;i<LL;i++,j++) ran_x[i]=mod_diff(aa[j-KK],aa[j-LL]); |
| 1273 |
for (;i<KK;i++,j++) ran_x[i]=mod_diff(aa[j-KK],ran_x[i-LL]); |
| 1274 |
} |
| 1275 |
/* */ |
| 1276 |
/* the following routines are from exercise 3.6--15 */ |
| 1277 |
/* after calling |ran_start|, get new randoms by, e.g., |x=ran_arr_next()| */ |
| 1278 |
/* */ |
| 1279 |
#define QUALITY 1009 /* recommended quality level for high-res use */ |
| 1280 |
static long ran_arr_buf[QUALITY]; |
| 1281 |
static long ran_arr_dummy=-1, ran_arr_started=-1; |
| 1282 |
static long *ran_arr_ptr=&ran_arr_dummy; /* the next random number, or -1 */ |
| 1283 |
/* */ |
| 1284 |
#define TT 70 /* guaranteed separation between streams */ |
| 1285 |
#define is_odd(x) ((x)&1) /* units bit of x */ |
| 1286 |
/* */ |
| 1287 |
static void ran_start(long seed) /* do this before using |ran_array| */ |
| 1288 |
/* long seed selector for different streams */ |
| 1289 |
{ |
| 1290 |
register int t,j; |
| 1291 |
long x[KK+KK-1]; /* the preparation buffer */ |
| 1292 |
register long ss=(seed+2)&(MM-2); |
| 1293 |
for (j=0;j<KK;j++) { |
| 1294 |
x[j]=ss; /* bootstrap the buffer */ |
| 1295 |
ss<<=1; if (ss>=MM) ss-=MM-2; /* cyclic shift 29 bits */ |
| 1296 |
} |
| 1297 |
x[1]++; /* make x[1] (and only x[1]) odd */ |
| 1298 |
for (ss=seed&(MM-1),t=TT-1; t; ) { |
| 1299 |
for (j=KK-1;j>0;j--) x[j+j]=x[j], x[j+j-1]=0; /* "square" */ |
| 1300 |
for (j=KK+KK-2;j>=KK;j--) |
| 1301 |
x[j-(KK-LL)]=mod_diff(x[j-(KK-LL)],x[j]), |
| 1302 |
x[j-KK]=mod_diff(x[j-KK],x[j]); |
| 1303 |
if (is_odd(ss)) { /* "multiply by z" */ |
| 1304 |
for (j=KK;j>0;j--) x[j]=x[j-1]; |
| 1305 |
x[0]=x[KK]; /* shift the buffer cyclically */ |
| 1306 |
x[LL]=mod_diff(x[LL],x[KK]); |
| 1307 |
} |
| 1308 |
if (ss) ss>>=1; else t--; |
| 1309 |
} |
| 1310 |
for (j=0;j<LL;j++) ran_x[j+KK-LL]=x[j]; |
| 1311 |
for (;j<KK;j++) ran_x[j-LL]=x[j]; |
| 1312 |
for (j=0;j<10;j++) ran_array(x,KK+KK-1); /* warm things up */ |
| 1313 |
ran_arr_ptr=&ran_arr_started; |
| 1314 |
} |
| 1315 |
/* */ |
| 1316 |
#define ran_arr_next() (*ran_arr_ptr>=0? *ran_arr_ptr++: ran_arr_cycle()) |
| 1317 |
static long ran_arr_cycle(void) |
| 1318 |
{ |
| 1319 |
if (ran_arr_ptr==&ran_arr_dummy) |
| 1320 |
ran_start(314159L); /* the user forgot to initialize */ |
| 1321 |
ran_array(ran_arr_buf,QUALITY); |
| 1322 |
ran_arr_buf[KK]=-1; |
| 1323 |
ran_arr_ptr=ran_arr_buf+1; |
| 1324 |
return ran_arr_buf[0]; |
| 1325 |
} |
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
@ To initialize the |randoms| table, we call the following routine. |
| 1330 |
|
| 1331 |
@c |
| 1332 |
void mp_init_randoms (MP mp, int seed) { |
| 1333 |
int j, jj, k; /* more or less random integers */ |
| 1334 |
int i; /* index into |randoms| */ |
| 1335 |
j = abs (seed); |
| 1336 |
while (j >= fraction_one) { |
| 1337 |
j = j/2; |
| 1338 |
} |
| 1339 |
k = 1; |
| 1340 |
for (i = 0; i <= 54; i++) { |
| 1341 |
jj = k; |
| 1342 |
k = j - k; |
| 1343 |
j = jj; |
| 1344 |
if (k<0) |
| 1345 |
k += (int)fraction_one; |
| 1346 |
mp->randoms[(i * 21) % 55].data.dval = j; |
| 1347 |
} |
| 1348 |
mp_new_randoms (mp); |
| 1349 |
mp_new_randoms (mp); |
| 1350 |
mp_new_randoms (mp); /* ``warm up'' the array */ |
| 1351 |
|
| 1352 |
ran_start((long) seed); |
| 1353 |
|
| 1354 |
|
| 1355 |
} |
| 1356 |
|
| 1357 |
@ @c |
| 1358 |
static double modulus(double left, double right); |
| 1359 |
double modulus(double left, double right) { |
| 1360 |
double quota = left / right; |
| 1361 |
double frac,tmp; |
| 1362 |
frac = modf(quota,&tmp); |
| 1363 |
/* frac contains what's beyond the '.' */ |
| 1364 |
frac *= right; |
| 1365 |
return frac; |
| 1366 |
} |
| 1367 |
void mp_number_modulo (mp_number *a, mp_number b) { |
| 1368 |
a->data.dval = modulus (a->data.dval, b.data.dval); |
| 1369 |
} |
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
@ To consume a random integer for the uniform generator, the program below will say `|next_unif_random|'. |
| 1374 |
|
| 1375 |
@c |
| 1376 |
static void mp_next_unif_random (MP mp, mp_number *ret) { |
| 1377 |
double a; |
| 1378 |
unsigned long int op; |
| 1379 |
(void)mp; |
| 1380 |
op = (unsigned)ran_arr_next(); |
| 1381 |
a = (double)op/(MM*1.0); |
| 1382 |
ret->data.dval = a; |
| 1383 |
} |
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
@ To consume a random fraction, the program below will say `|next_random|'. |
| 1388 |
|
| 1389 |
@c |
| 1390 |
static void mp_next_random (MP mp, mp_number *ret) { |
| 1391 |
if ( mp->j_random==0 ) |
| 1392 |
mp_new_randoms(mp); |
| 1393 |
else |
| 1394 |
mp->j_random = mp->j_random-1; |
| 1395 |
mp_number_clone (ret, mp->randoms[mp->j_random]); |
| 1396 |
} |
| 1397 |
|
| 1398 |
|
| 1399 |
@ To produce a uniform random number in the range |0<=u<x| or |0>=u>x| |
| 1400 |
or |0=u=x|, given a |scaled| value~|x|, we proceed as shown here. |
| 1401 |
|
| 1402 |
Note that the call of |take_fraction| will produce the values 0 and~|x| |
| 1403 |
with about half the probability that it will produce any other particular |
| 1404 |
values between 0 and~|x|, because it rounds its answers. |
| 1405 |
|
| 1406 |
@c |
| 1407 |
static void mp_double_m_unif_rand (MP mp, mp_number *ret, mp_number x_orig) { |
| 1408 |
mp_number y; /* trial value */ |
| 1409 |
mp_number x, abs_x; |
| 1410 |
mp_number u; |
| 1411 |
new_fraction (y); |
| 1412 |
new_number (x); |
| 1413 |
new_number (abs_x); |
| 1414 |
new_number (u); |
| 1415 |
mp_number_clone (&x, x_orig); |
| 1416 |
mp_number_clone (&abs_x, x); |
| 1417 |
mp_double_abs (&abs_x); |
| 1418 |
mp_next_unif_random(mp, &u); |
| 1419 |
y.data.dval = abs_x.data.dval * u.data.dval; |
| 1420 |
free_number (u); |
| 1421 |
if (mp_number_equal(y, abs_x)) { |
| 1422 |
mp_number_clone (ret, ((math_data *)mp->math)->zero_t); |
| 1423 |
} else if (mp_number_greater(x, ((math_data *)mp->math)->zero_t)) { |
| 1424 |
mp_number_clone (ret, y); |
| 1425 |
} else { |
| 1426 |
mp_number_clone (ret, y); |
| 1427 |
mp_number_negate (ret); |
| 1428 |
} |
| 1429 |
free_number (abs_x); |
| 1430 |
free_number (x); |
| 1431 |
free_number (y); |
| 1432 |
} |
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
@ Finally, a normal deviate with mean zero and unit standard deviation |
| 1437 |
can readily be obtained with the ratio method (Algorithm 3.4.1R in |
| 1438 |
{\sl The Art of Computer Programming\/}). |
| 1439 |
|
| 1440 |
@c |
| 1441 |
static void mp_double_m_norm_rand (MP mp, mp_number *ret) { |
| 1442 |
mp_number ab_vs_cd; |
| 1443 |
mp_number abs_x; |
| 1444 |
mp_number u; |
| 1445 |
mp_number r; |
| 1446 |
mp_number la, xa; |
| 1447 |
new_number (ab_vs_cd); |
| 1448 |
new_number (la); |
| 1449 |
new_number (xa); |
| 1450 |
new_number (abs_x); |
| 1451 |
new_number (u); |
| 1452 |
new_number (r); |
| 1453 |
|
| 1454 |
do { |
| 1455 |
do { |
| 1456 |
mp_number v; |
| 1457 |
new_number (v); |
| 1458 |
mp_next_random(mp, &v); |
| 1459 |
mp_number_substract (&v, ((math_data *)mp->math)->fraction_half_t); |
| 1460 |
mp_double_number_take_fraction (mp,&xa, ((math_data *)mp->math)->sqrt_8_e_k, v); |
| 1461 |
free_number (v); |
| 1462 |
mp_next_random(mp, &u); |
| 1463 |
mp_number_clone (&abs_x, xa); |
| 1464 |
mp_double_abs (&abs_x); |
| 1465 |
} while (!mp_number_less(abs_x, u)); |
| 1466 |
mp_double_number_make_fraction (mp, &r, xa, u); |
| 1467 |
mp_number_clone (&xa, r); |
| 1468 |
mp_double_m_log (mp,&la, u); |
| 1469 |
mp_set_double_from_substraction(&la, ((math_data *)mp->math)->twelve_ln_2_k, la); |
| 1470 |
mp_double_ab_vs_cd (mp,&ab_vs_cd, ((math_data *)mp->math)->one_k, la, xa, xa); |
| 1471 |
} while (mp_number_less(ab_vs_cd,((math_data *)mp->math)->zero_t)); |
| 1472 |
mp_number_clone (ret, xa); |
| 1473 |
free_number (ab_vs_cd); |
| 1474 |
free_number (r); |
| 1475 |
free_number (abs_x); |
| 1476 |
free_number (la); |
| 1477 |
free_number (xa); |
| 1478 |
free_number (u); |
| 1479 |
} |
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
@ The following subroutine is used only in |norm_rand| and tests if $ab$ is |
| 1485 |
greater than, equal to, or less than~$cd$. |
| 1486 |
The result is $+1$, 0, or~$-1$ in the three respective cases. |
| 1487 |
|
| 1488 |
@c |
| 1489 |
void mp_double_ab_vs_cd (MP mp, mp_number *ret, mp_number a_orig, mp_number b_orig, mp_number c_orig, mp_number d_orig) { |
| 1490 |
double ab, cd; |
| 1491 |
(void)mp; |
| 1492 |
ret->data.dval = 0 ; |
| 1493 |
ab = a_orig.data.dval*b_orig.data.dval; |
| 1494 |
cd = c_orig.data.dval*d_orig.data.dval; |
| 1495 |
if (ab > cd ) |
| 1496 |
ret->data.dval = 1 ; |
| 1497 |
else if (ab < cd ) |
| 1498 |
ret->data.dval = -1 ; |
| 1499 |
return ; |
| 1500 |
} |
| 1501 |
|