int main(void) { fixedpt x, y; int n; /* addition */ x = 5; y = 9; printf("int x + y = %d + %d = %d\n", x, y, x + y); printf("fixedpt x + y = %d + %d = %d\n", int_to_fixedpt(x), int_to_fixedpt(y), fixedpt_add(int_to_fixedpt(x), int_to_fixedpt(y))); printf("fixedpt x + y = %d + %d = intzero %d\n", int_to_fixedpt(x), int_to_fixedpt(y), fixedpt_to_int_zero(fixedpt_add(int_to_fixedpt(x), int_to_fixedpt(y)))); printf("fixedpt x + y = %d + %d = intnearest %d\n", int_to_fixedpt(x), int_to_fixedpt(y), fixedpt_to_int_nearest(fixedpt_add(int_to_fixedpt(x), int_to_fixedpt(y)))); /* subtraction */ printf("\nint x - y = %d - %d = %d\n", x, y, x - y); printf("fixedpt x - y = %d - %d = %d\n", int_to_fixedpt(x), int_to_fixedpt(y), fixedpt_sub(int_to_fixedpt(x), int_to_fixedpt(y))); printf("fixedpt x - y = %d - %d = intzero %d\n", int_to_fixedpt(x), int_to_fixedpt(y), fixedpt_to_int_zero(fixedpt_sub(int_to_fixedpt(x), int_to_fixedpt(y)))); printf("fixedpt x - y = %d - %d = intnearest %d\n", int_to_fixedpt(x), int_to_fixedpt(y), fixedpt_to_int_nearest(fixedpt_sub(int_to_fixedpt(x), int_to_fixedpt(y)))); /* multiplication */ printf("\nint x * y = %d * %d = %d\n", x, y, x * y); printf("fixedpt x * y = %d * %d = %d\n", int_to_fixedpt(x), int_to_fixedpt(y), fixedpt_mul(int_to_fixedpt(x), int_to_fixedpt(y))); printf("fixedpt x * y = %d * %d = intzero %d\n", int_to_fixedpt(x), int_to_fixedpt(y), fixedpt_to_int_zero(fixedpt_mul(int_to_fixedpt(x), int_to_fixedpt(y)))); printf("fixedpt x * y = %d * %d = intnearest %d\n", int_to_fixedpt(x), int_to_fixedpt(y), fixedpt_to_int_nearest(fixedpt_mul(int_to_fixedpt(x), int_to_fixedpt(y)))); /* division */ printf("\nint x / y = %d / %d = %d\n", x, y, x / y); printf("fixedpt x / y = %d / %d = %d\n", int_to_fixedpt(x), int_to_fixedpt(y), fixedpt_div(int_to_fixedpt(x), int_to_fixedpt(y))); printf("fixedpt x / y = %d / %d = intzero %d\n", int_to_fixedpt(x), int_to_fixedpt(y), fixedpt_to_int_zero(fixedpt_div(int_to_fixedpt(x), int_to_fixedpt(y)))); printf("fixedpt x / y = %d / %d = intnearest %d\n", int_to_fixedpt(x), int_to_fixedpt(y), fixedpt_to_int_nearest(fixedpt_div(int_to_fixedpt(x), int_to_fixedpt(y)))); return 0; }
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; }
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; }