HaikuDepot: notify work status from main window
[haiku.git] / src / libs / mapm / mapm_lg3.c
blob6b83da5bfc3c643a3c59a8315c249f9f1c38d0ff
2 /*
3 * M_APM - mapm_lg3.c
5 * Copyright (C) 2003 - 2007 Michael C. Ring
7 * Permission to use, copy, and distribute this software and its
8 * documentation for any purpose with or without fee is hereby granted,
9 * provided that the above copyright notice appear in all copies and
10 * that both that copyright notice and this permission notice appear
11 * in supporting documentation.
13 * Permission to modify the software is granted. Permission to distribute
14 * the modified code is granted. Modifications are to be distributed by
15 * using the file 'license.txt' as a template to modify the file header.
16 * 'license.txt' is available in the official MAPM distribution.
18 * This software is provided "as is" without express or implied warranty.
22 * $Id: mapm_lg3.c,v 1.7 2007/12/03 01:42:59 mike Exp $
24 * This file contains the function to compute log(2), log(10),
25 * and 1/log(10) to the desired precision using an AGM algorithm.
27 * $Log: mapm_lg3.c,v $
28 * Revision 1.7 2007/12/03 01:42:59 mike
29 * Update license
31 * Revision 1.6 2003/12/09 01:25:06 mike
32 * actually compute the first term of the AGM iteration instead
33 * of assuming the inputs a=1 and b=10^-N.
35 * Revision 1.5 2003/12/04 03:19:16 mike
36 * rearrange logic in AGM to be more straight-forward
38 * Revision 1.4 2003/05/01 22:04:37 mike
39 * rearrange some code
41 * Revision 1.3 2003/05/01 21:58:31 mike
42 * remove math.h
44 * Revision 1.2 2003/03/30 22:14:58 mike
45 * add comments
47 * Revision 1.1 2003/03/30 21:18:04 mike
48 * Initial revision
51 #include "m_apm_lc.h"
54 * using the 'R' function (defined below) for 'N' decimal places :
57 * -N -N
58 * log(2) = R(1, 0.5 * 10 ) - R(1, 10 )
61 * -N -N
62 * log(10) = R(1, 0.1 * 10 ) - R(1, 10 )
65 * In general:
67 * -N -N
68 * log(x) = R(1, 10 / x) - R(1, 10 )
71 * I found this on a web site which went into considerable detail
72 * on the history of log(2). This formula is algebraically identical
73 * to the formula specified in J. Borwein and P. Borwein's book
74 * "PI and the AGM". (reference algorithm 7.2)
77 /****************************************************************************/
79 * check if our local copy of log(2) & log(10) is precise
80 * enough for our purpose. if not, calculate them so it's
81 * as precise as desired, accurate to at least 'places'.
83 void M_check_log_places(int places)
85 M_APM tmp6, tmp7, tmp8, tmp9;
86 int dplaces;
88 dplaces = places + 4;
90 if (dplaces > MM_lc_log_digits)
92 MM_lc_log_digits = dplaces + 4;
94 tmp6 = M_get_stack_var();
95 tmp7 = M_get_stack_var();
96 tmp8 = M_get_stack_var();
97 tmp9 = M_get_stack_var();
99 dplaces += 6 + (int)log10((double)places);
101 m_apm_copy(tmp7, MM_One);
102 tmp7->m_apm_exponent = -places;
104 M_log_AGM_R_func(tmp8, dplaces, MM_One, tmp7);
106 m_apm_multiply(tmp6, tmp7, MM_0_5);
108 M_log_AGM_R_func(tmp9, dplaces, MM_One, tmp6);
110 m_apm_subtract(MM_lc_log2, tmp9, tmp8); /* log(2) */
112 tmp7->m_apm_exponent -= 1; /* divide by 10 */
114 M_log_AGM_R_func(tmp9, dplaces, MM_One, tmp7);
116 m_apm_subtract(MM_lc_log10, tmp9, tmp8); /* log(10) */
117 m_apm_reciprocal(MM_lc_log10R, dplaces, MM_lc_log10); /* 1 / log(10) */
119 M_restore_stack(4);
122 /****************************************************************************/
125 * define a notation for a function 'R' :
130 * R (a0, b0) = ------------------------------
132 * ----
133 * \
134 * \ n-1 2 2
135 * 1 - | 2 * (a - b )
136 * / n n
138 * ----
139 * n >= 0
142 * where a, b are the classic AGM iteration :
145 * a = 0.5 * (a + b )
146 * n+1 n n
149 * b = sqrt(a * b )
150 * n+1 n n
154 * define a variable 'c' for more efficient computation :
156 * 2 2 2
157 * c = 0.5 * (a - b ) , c = a - b
158 * n+1 n n n n n
162 /****************************************************************************/
163 void M_log_AGM_R_func(M_APM rr, int places, M_APM aa, M_APM bb)
165 M_APM tmp1, tmp2, tmp3, tmp4, tmpC2, sum, pow_2, tmpA0, tmpB0;
166 int tolerance, dplaces;
168 tmpA0 = M_get_stack_var();
169 tmpB0 = M_get_stack_var();
170 tmpC2 = M_get_stack_var();
171 tmp1 = M_get_stack_var();
172 tmp2 = M_get_stack_var();
173 tmp3 = M_get_stack_var();
174 tmp4 = M_get_stack_var();
175 sum = M_get_stack_var();
176 pow_2 = M_get_stack_var();
178 tolerance = places + 8;
179 dplaces = places + 16;
181 m_apm_copy(tmpA0, aa);
182 m_apm_copy(tmpB0, bb);
183 m_apm_copy(pow_2, MM_0_5);
185 m_apm_multiply(tmp1, aa, aa); /* 0.5 * [ a ^ 2 - b ^ 2 ] */
186 m_apm_multiply(tmp2, bb, bb);
187 m_apm_subtract(tmp3, tmp1, tmp2);
188 m_apm_multiply(sum, MM_0_5, tmp3);
190 while (TRUE)
192 m_apm_subtract(tmp1, tmpA0, tmpB0); /* C n+1 = 0.5 * [ An - Bn ] */
193 m_apm_multiply(tmp4, MM_0_5, tmp1); /* C n+1 */
194 m_apm_multiply(tmpC2, tmp4, tmp4); /* C n+1 ^ 2 */
196 /* do the AGM */
198 m_apm_add(tmp1, tmpA0, tmpB0);
199 m_apm_multiply(tmp3, MM_0_5, tmp1);
201 m_apm_multiply(tmp2, tmpA0, tmpB0);
202 m_apm_sqrt(tmpB0, dplaces, tmp2);
204 m_apm_round(tmpA0, dplaces, tmp3);
206 /* end AGM */
208 m_apm_multiply(tmp2, MM_Two, pow_2);
209 m_apm_copy(pow_2, tmp2);
211 m_apm_multiply(tmp1, tmpC2, pow_2);
212 m_apm_add(tmp3, sum, tmp1);
214 if ((tmp1->m_apm_sign == 0) ||
215 ((-2 * tmp1->m_apm_exponent) > tolerance))
216 break;
218 m_apm_round(sum, dplaces, tmp3);
221 m_apm_subtract(tmp4, MM_One, tmp3);
222 m_apm_reciprocal(rr, places, tmp4);
224 M_restore_stack(9);
226 /****************************************************************************/