speedup log calculation by optimizing mulitplication by 1 + 10^-j
This commit is contained in:
parent
06ca0b2829
commit
277ae0acf3
@ -862,7 +862,6 @@ void ln_decn(void){
|
||||
exp_t initial_exp;
|
||||
#define B_j Tmp2Decn
|
||||
#define NUM_TIMES Tmp3Decn
|
||||
#define A_ARR_j Tmp4Decn
|
||||
|
||||
//check not negative or zero
|
||||
if (AccDecn.exponent < 0 || decn_is_zero(&AccDecn)){
|
||||
@ -878,9 +877,6 @@ void ln_decn(void){
|
||||
decn_to_str_complete(&AccDecn);
|
||||
printf("ln() accum scaled between 1,10: %s\n", Buf);
|
||||
#endif
|
||||
//get initial a_arr[0] = 2
|
||||
set_dec80_zero(&A_ARR_j);
|
||||
A_ARR_j.lsu[0] = 20;
|
||||
//get initial estimate (accum = 10 - A)
|
||||
copy_decn(&BDecn, &DECN_1);
|
||||
BDecn.exponent = 1; //BDecn = 10
|
||||
@ -902,12 +898,18 @@ void ln_decn(void){
|
||||
copy_decn(&AccDecn, &B_j); //accum = b_j
|
||||
k_j = 0;
|
||||
while (!(AccDecn.exponent < 0)){ //while >= 0 (!negative) TODO: should just be >
|
||||
uint8_t i;
|
||||
copy_decn(&B_j, &AccDecn); //b_j = accum
|
||||
//accum *= a_arr[i]
|
||||
copy_decn(&BDecn, &A_ARR_j);
|
||||
mult_decn();
|
||||
//accum *= a_arr[j]
|
||||
// since a_arr[j] is of the form 1 + 10^-j, just shift accum and add to self
|
||||
copy_decn(&BDecn, &AccDecn);
|
||||
for (i = 0; i < j; i++){
|
||||
shift_right(&BDecn);
|
||||
}
|
||||
add_decn();
|
||||
//accum -= 10
|
||||
if (AccDecn.lsu[0] >= 10 && get_exponent(&AccDecn) > 0){ //accum.exponent is 1 while needs subtracting
|
||||
// accum.exponent is 1 while needs subtracting
|
||||
if (AccDecn.lsu[0] >= 10 && get_exponent(&AccDecn) > 0){
|
||||
AccDecn.lsu[0] -= 10;
|
||||
} else {
|
||||
//set as negative to get out of while(), accum will get overwritten with b_j
|
||||
@ -932,21 +934,6 @@ void ln_decn(void){
|
||||
decn_to_str_complete(&B_j);
|
||||
printf(" %u: num_times: %u, %s\n", j, NUM_TIMES.lsu[j], Buf);
|
||||
#endif
|
||||
|
||||
//find next a_j (1 + 10^-j)
|
||||
if (j == 0) {
|
||||
//build value 1.1
|
||||
set_dec80_zero(&A_ARR_j);
|
||||
A_ARR_j.lsu[0] = 11;
|
||||
} else {
|
||||
//get next 1.000...1 value
|
||||
shift_right(&A_ARR_j);
|
||||
A_ARR_j.lsu[0] = 10;
|
||||
}
|
||||
#ifdef DEBUG_LOG
|
||||
decn_to_str_complete(&A_ARR_j);
|
||||
printf(" new a_j val: %s\n", Buf);
|
||||
#endif
|
||||
}
|
||||
|
||||
//build final value
|
||||
@ -1020,7 +1007,6 @@ void ln_decn(void){
|
||||
//try not to pollute namespace
|
||||
#undef B_j
|
||||
#undef NUM_TIMES
|
||||
#undef A_ARR_j
|
||||
#undef NUM_A_ARR
|
||||
}
|
||||
|
||||
|
@ -68,23 +68,14 @@ acc/b: 0.666666666666666672
|
||||
ln() accum scaled between 1,10: 1.55
|
||||
ln() initial accum: 8.45 (0)
|
||||
0: num_times: 2, 3.8
|
||||
new a_j val: 1.1
|
||||
1: num_times: 5, 0.14838
|
||||
new a_j val: 1.01
|
||||
2: num_times: 0, 1.4838
|
||||
new a_j val: 1.001
|
||||
3: num_times: 1, 4.852838
|
||||
new a_j val: 1.0001
|
||||
4: num_times: 4, 8.5417938638869182
|
||||
new a_j val: 1.00001
|
||||
5: num_times: 8, 5.4219722571346027
|
||||
new a_j val: 1.000001
|
||||
6: num_times: 5, 4.2198936704010812
|
||||
new a_j val: 1.0000001
|
||||
7: num_times: 4, 2.1989475835876253
|
||||
new a_j val: 1.00000001
|
||||
8: num_times: 2, 1.9894761756657718
|
||||
new a_j val: 1.000000001
|
||||
ln() remainder: 1.9894761756657718
|
||||
8: ln() remainder: 2.1989476075668969
|
||||
7: ln() remainder: 4.2198945607568002
|
||||
@ -104,23 +95,14 @@ ln(a): -1.8643301620628904
|
||||
ln() accum scaled between 1,10: 1.
|
||||
ln() initial accum: 9. (2)
|
||||
0: num_times: 3, 2.
|
||||
new a_j val: 1.1
|
||||
1: num_times: 2, 3.2
|
||||
new a_j val: 1.01
|
||||
2: num_times: 3, 2.668632
|
||||
new a_j val: 1.001
|
||||
3: num_times: 2, 6.72971932632
|
||||
new a_j val: 1.0001
|
||||
4: num_times: 6, 7.3225796749329481
|
||||
new a_j val: 1.00001
|
||||
5: num_times: 7, 3.2288226738783197
|
||||
new a_j val: 1.000001
|
||||
6: num_times: 3, 2.2882936035502779
|
||||
new a_j val: 1.0000001
|
||||
7: num_times: 2, 2.8829396120902148
|
||||
new a_j val: 1.00000001
|
||||
8: num_times: 2, 8.8293965974900732
|
||||
new a_j val: 1.000000001
|
||||
ln() remainder: 8.8293965974900732
|
||||
8: ln() remainder: 2.882939649749327
|
||||
7: ln() remainder: 2.2882938649749879
|
||||
@ -140,23 +122,14 @@ ln(a): 2.30258509299404576
|
||||
ln() accum scaled between 1,10: 1.1
|
||||
ln() initial accum: 8.9 (11)
|
||||
0: num_times: 3, 1.2
|
||||
new a_j val: 1.1
|
||||
1: num_times: 1, 3.2
|
||||
new a_j val: 1.01
|
||||
2: num_times: 3, 2.668632
|
||||
new a_j val: 1.001
|
||||
3: num_times: 2, 6.72971932632
|
||||
new a_j val: 1.0001
|
||||
4: num_times: 6, 7.3225796749329481
|
||||
new a_j val: 1.00001
|
||||
5: num_times: 7, 3.2288226738783197
|
||||
new a_j val: 1.000001
|
||||
6: num_times: 3, 2.2882936035502779
|
||||
new a_j val: 1.0000001
|
||||
7: num_times: 2, 2.8829396120902148
|
||||
new a_j val: 1.00000001
|
||||
8: num_times: 2, 8.8293965974900732
|
||||
new a_j val: 1.000000001
|
||||
ln() remainder: 8.8293965974900732
|
||||
8: ln() remainder: 2.882939649749327
|
||||
7: ln() remainder: 2.2882938649749879
|
||||
@ -176,23 +149,14 @@ ln(a): 23.1211611097447817
|
||||
ln() accum scaled between 1,10: 2.02
|
||||
ln() initial accum: 7.98 (-9)
|
||||
0: num_times: 2, 1.92
|
||||
new a_j val: 1.1
|
||||
1: num_times: 2, 2.232
|
||||
new a_j val: 1.01
|
||||
2: num_times: 2, 2.668632
|
||||
new a_j val: 1.001
|
||||
3: num_times: 2, 6.72971932632
|
||||
new a_j val: 1.0001
|
||||
4: num_times: 6, 7.3225796749329481
|
||||
new a_j val: 1.00001
|
||||
5: num_times: 7, 3.2288226738783197
|
||||
new a_j val: 1.000001
|
||||
6: num_times: 3, 2.2882936035502779
|
||||
new a_j val: 1.0000001
|
||||
7: num_times: 2, 2.8829396120902148
|
||||
new a_j val: 1.00000001
|
||||
8: num_times: 2, 8.8293965974900732
|
||||
new a_j val: 1.000000001
|
||||
ln() remainder: 8.8293965974900732
|
||||
8: ln() remainder: 2.882939649749327
|
||||
7: ln() remainder: 2.2882938649749879
|
||||
@ -212,23 +176,14 @@ ln(a): -22.3227534185273433
|
||||
ln() accum scaled between 1,10: 1.5
|
||||
ln() initial accum: 8.5 (1)
|
||||
0: num_times: 2, 4.
|
||||
new a_j val: 1.1
|
||||
1: num_times: 5, 3.3694
|
||||
new a_j val: 1.01
|
||||
2: num_times: 3, 4.413961894
|
||||
new a_j val: 1.001
|
||||
3: num_times: 4, 4.2564024200762553
|
||||
new a_j val: 1.0001
|
||||
4: num_times: 4, 2.5750519644445702
|
||||
new a_j val: 1.00001
|
||||
5: num_times: 2, 5.7509346574136428
|
||||
new a_j val: 1.000001
|
||||
6: num_times: 5, 7.5095341213443924
|
||||
new a_j val: 1.0000001
|
||||
7: num_times: 7, 5.0953727801950431
|
||||
new a_j val: 1.00000001
|
||||
8: num_times: 5, 0.953729349636862
|
||||
new a_j val: 1.000000001
|
||||
ln() remainder: 0.953729349636862
|
||||
8: ln() remainder: 5.0953729099644855
|
||||
7: ln() remainder: 7.509536940996642
|
||||
|
Loading…
Reference in New Issue
Block a user