fixedpt asset_path_fixed_simplified ( fixedpt s0, fixedpt mu, fixedpt sigma, fixedpt t1, int n, int seed ){ int i; fixedpt dt, stepnum, p; fixedpt gaussR1[OMP_ACCEL], gaussR2[OMP_ACCEL]; // stepnum = fixedpt_rconst(n); stepnum = n << FIXEDPT_FBITS; // ??? janders dt = fixedpt_div(t1, stepnum); fixedpt constA = fixedpt_mul(fixedpt_sub(mu, fixedpt_mul(sigma, sigma)), dt); fixedpt constB = fixedpt_mul(sigma, fixedpt_sqrt ( dt )); int s = seed; p = s0; int tid; for ( i = 1; i <= n; i++ ) { tid = omp_get_thread_num(); if (i & 1) { // iteration is odd, generate two random Gaussian numbers (the Box-Muller transform gens 2 numbers) s = get_two_normal_fixed(s, gaussR1, gaussR2); } p = fixedpt_mul(p, fixedpt_exp (fixedpt_add(constA, fixedpt_mul(constB, i & 1 ? gaussR1[tid] : gaussR2[tid])))); // if (tid == 0) { // printf("i = %d, seed = %d, gaussR1 = %d, gaussR1 = %d, p = %d\n", i, s, gaussR1[tid], gaussR2[tid], p); // } // fixedpt_print(p); } return p; }
fixedpt asset_path_fixed_simplified ( fixedpt s0, fixedpt mu, fixedpt sigma, fixedpt t1, int n, int *seed ){ int i; fixedpt dt, stepnum, p; fixedpt gaussR1 = 0, gaussR2 = 0; // stepnum = fixedpt_rconst(n); stepnum = n << FIXEDPT_FBITS; // ??? janders dt = fixedpt_div(t1, stepnum); fixedpt constA = fixedpt_mul(fixedpt_sub(mu, fixedpt_mul(sigma, sigma)), dt); fixedpt constB = fixedpt_mul(sigma, fixedpt_sqrt ( dt )); p = s0; for ( i = 1; i <= n; i++ ) { if (i & 1) // iteration is odd, generate two random Gaussian numbers (the Box-Muller transform gens 2 numbers) get_two_normal_fixed_LUT(seed, &gaussR1, &gaussR2); p = fixedpt_mul(p, fixedpt_exp (fixedpt_add(constA, fixedpt_mul(constB, i & 1 ? gaussR1 : gaussR2)))); // fixedpt_print(p); } return p; }
int main() { fixedpt A, B, C; printf("fixedptc library version: %s\n", FIXEDPT_VCSID); printf("Using %d-bit precision, %d.%d format\n\n", FIXEDPT_BITS, FIXEDPT_WBITS, FIXEDPT_FBITS); printf("The most precise number: "); fixedpt_print(1); printf("The biggest number: "); fixedpt_print(0x7fffff00); printf("Here are some example numbers:\n"); printf("Random number: "); fixedpt_print(fixedpt_rconst(143.125)); printf("PI: "); fixedpt_print(FIXEDPT_PI); printf("e: "); fixedpt_print(FIXEDPT_E); puts(""); A = fixedpt_rconst(2.5); B = fixedpt_fromint(3); fixedpt_print(A); puts("+"); fixedpt_print(B); C = fixedpt_add(A, B); puts("="); fixedpt_print(C); puts(""); fixedpt_print(A); puts("*"); fixedpt_print(B); puts("="); C = fixedpt_mul(A, B); fixedpt_print(C); puts(""); A = fixedpt_rconst(1); B = fixedpt_rconst(4); C = fixedpt_div(A, B); fixedpt_print(A); puts("/"); fixedpt_print(B); puts("="); fixedpt_print(C); printf("exp(1)="); fixedpt_print(fixedpt_exp(FIXEDPT_ONE)); puts(""); puts("sqrt(pi)="); fixedpt_print(fixedpt_sqrt(FIXEDPT_PI)); puts(""); puts("sqrt(25)="); fixedpt_print(fixedpt_sqrt(fixedpt_rconst(25))); puts(""); puts("sin(pi/2)="); fixedpt_print(fixedpt_sin(FIXEDPT_HALF_PI)); puts(""); puts("sin(3.5*pi)="); fixedpt_print(fixedpt_sin(fixedpt_mul(fixedpt_rconst(3.5), FIXEDPT_PI))); puts(""); puts("4^3.5="); fixedpt_print(fixedpt_pow(fixedpt_rconst(4), fixedpt_rconst(3.5))); puts(""); puts("4^0.5="); fixedpt_print(fixedpt_pow(fixedpt_rconst(4), fixedpt_rconst(0.5))); return (0); }