1
2/*
3 *  M_APM  -  mapm_lg4.c
4 *
5 *  Copyright (C) 2003 - 2007   Michael C. Ring
6 *
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.
12 *
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.
17 *
18 *  This software is provided "as is" without express or implied warranty.
19 */
20
21/*
22 *      $Id: mapm_lg4.c,v 1.3 2007/12/03 01:43:32 mike Exp $
23 *
24 *      This file contains the LOG_NEAR_1 function.
25 *
26 *      $Log: mapm_lg4.c,v $
27 *      Revision 1.3  2007/12/03 01:43:32  mike
28 *      Update license
29 *
30 *      Revision 1.2  2003/06/02 18:08:45  mike
31 *      tweak decimal places and add comments
32 *
33 *      Revision 1.1  2003/06/02 17:27:26  mike
34 *      Initial revision
35 */
36
37#include "m_apm_lc.h"
38
39/****************************************************************************/
40/*
41	calculate log (1 + x) with the following series:
42
43              x
44	y = -----      ( |y| < 1 )
45	    x + 2
46
47
48            [ 1 + y ]                 y^3     y^5     y^7
49	log [-------]  =  2 * [ y  +  ---  +  ---  +  ---  ... ]
50            [ 1 - y ]                  3       5       7
51
52*/
53void	M_log_near_1(M_APM rr, int places, M_APM xx)
54{
55M_APM   tmp0, tmp1, tmp2, tmpS, term;
56int	tolerance, dplaces, local_precision;
57long    m1;
58
59tmp0 = M_get_stack_var();
60tmp1 = M_get_stack_var();
61tmp2 = M_get_stack_var();
62tmpS = M_get_stack_var();
63term = M_get_stack_var();
64
65tolerance = xx->m_apm_exponent - (places + 6);
66dplaces   = (places + 12) - xx->m_apm_exponent;
67
68m_apm_add(tmp0, xx, MM_Two);
69m_apm_divide(tmpS, (dplaces + 6), xx, tmp0);
70
71m_apm_copy(term, tmpS);
72m_apm_multiply(tmp0, tmpS, tmpS);
73m_apm_round(tmp2, (dplaces + 6), tmp0);
74
75m1 = 3L;
76
77while (TRUE)
78  {
79   m_apm_multiply(tmp0, term, tmp2);
80
81   if ((tmp0->m_apm_exponent < tolerance) || (tmp0->m_apm_sign == 0))
82     break;
83
84   local_precision = dplaces + tmp0->m_apm_exponent;
85
86   if (local_precision < 20)
87     local_precision = 20;
88
89   m_apm_set_long(tmp1, m1);
90   m_apm_round(term, local_precision, tmp0);
91   m_apm_divide(tmp0, local_precision, term, tmp1);
92   m_apm_add(tmp1, tmpS, tmp0);
93   m_apm_copy(tmpS, tmp1);
94   m1 += 2;
95  }
96
97m_apm_multiply(tmp0, MM_Two, tmpS);
98m_apm_round(rr, places, tmp0);
99
100M_restore_stack(5);                    /* restore the 5 locals we used here */
101}
102/****************************************************************************/
103