"Fossies" - the Fresh Open Source Software Archive

Member "mapm_4.9.5a/mapm_log.c" (21 Feb 2010, 6530 Bytes) of package /linux/misc/old/mapm-4.9.5a.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "mapm_log.c" see the Fossies "Dox" file reference documentation.

    1 
    2 /* 
    3  *  M_APM  -  mapm_log.c
    4  *
    5  *  Copyright (C) 1999 - 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_log.c,v 1.29 2007/12/03 01:44:19 mike Exp $
   23  *
   24  *      This file contains the LOG and LOG10 functions.
   25  *
   26  *      $Log: mapm_log.c,v $
   27  *      Revision 1.29  2007/12/03 01:44:19  mike
   28  *      Update license
   29  *
   30  *      Revision 1.28  2003/07/21 20:18:06  mike
   31  *      Modify error messages to be in a consistent format.
   32  *
   33  *      Revision 1.27  2003/06/02 17:22:46  mike
   34  *      put 'log_near_1' into it's own separate module
   35  *
   36  *      Revision 1.26  2003/05/12 17:42:46  mike
   37  *      only check for 'near 1' if exponent is 0 or 1
   38  *
   39  *      Revision 1.25  2003/05/04 21:08:25  mike
   40  *      *** empty log message ***
   41  *
   42  *      Revision 1.24  2003/05/01 21:58:34  mike
   43  *      remove math.h
   44  *
   45  *      Revision 1.23  2003/05/01 21:39:09  mike
   46  *      use 'abs' call
   47  *
   48  *      Revision 1.22  2003/05/01 19:44:57  mike
   49  *      optimize log_near_1 by calculating fewer digits
   50  *      on subsequent iterations
   51  *
   52  *      Revision 1.21  2003/03/31 22:00:56  mike
   53  *      call generic error handling function
   54  *
   55  *      Revision 1.20  2003/03/30 22:57:13  mike
   56  *      call a new iterative log function which is cubically convergent
   57  *
   58  *      Revision 1.19  2002/11/03 22:14:45  mike
   59  *      Updated function parameters to use the modern style
   60  *
   61  *      Revision 1.18  2001/07/16 19:21:16  mike
   62  *      add function M_free_all_log
   63  *
   64  *      Revision 1.17  2000/10/22 00:24:29  mike
   65  *      minor optimization
   66  *
   67  *      Revision 1.16  2000/10/21 16:22:50  mike
   68  *      use an improved log_near_1 algorithm
   69  *
   70  *      Revision 1.15  2000/10/20 16:49:33  mike
   71  *      update algorithm for basic log function and add new
   72  *      function when input is close to '1'
   73  *
   74  *      Revision 1.14  2000/09/23 19:48:21  mike
   75  *      change divide call to reciprocal
   76  *
   77  *      Revision 1.13  2000/07/11 18:58:35  mike
   78  *      do it right this time
   79  *
   80  *      Revision 1.12  2000/07/11 18:19:27  mike
   81  *      estimate a better initial precision
   82  *
   83  *      Revision 1.11  2000/05/19 16:14:15  mike
   84  *      update some comments
   85  *
   86  *      Revision 1.10  2000/05/17 23:47:35  mike
   87  *      recompute a local copy of log E base 10 on the fly
   88  *      if more precision is needed.
   89  *
   90  *      Revision 1.9  2000/03/27 21:44:12  mike
   91  *      determine how many iterations should be required at
   92  *      run time for log
   93  *
   94  *      Revision 1.8  1999/07/21 02:56:18  mike
   95  *      added some comments
   96  *
   97  *      Revision 1.7  1999/07/19 00:28:51  mike
   98  *      adjust local precision again
   99  *
  100  *      Revision 1.6  1999/07/19 00:10:34  mike
  101  *      adjust local precision during iterative loop
  102  *
  103  *      Revision 1.5  1999/07/18 23:15:54  mike
  104  *      change local precision dynamically and change
  105  *      tolerance to integers for faster iterative routine.
  106  *
  107  *      Revision 1.4  1999/06/19 21:08:32  mike
  108  *      changed local static variables to MAPM stack variables
  109  *
  110  *      Revision 1.3  1999/05/15 01:34:50  mike
  111  *      add check for number of decimal places
  112  *
  113  *      Revision 1.2  1999/05/10 21:42:32  mike
  114  *      added some comments
  115  *
  116  *      Revision 1.1  1999/05/10 20:56:31  mike
  117  *      Initial revision
  118  */
  119 
  120 #include "m_apm_lc.h"
  121 
  122 /****************************************************************************/
  123 /*
  124         Calls the LOG function. The formula used is :
  125 
  126         log10(x)  =  A * log(x) where A = log  (e)  [0.43429448190325...]
  127                                              10
  128 */
  129 void    m_apm_log10(M_APM rr, int places, M_APM aa)
  130 {
  131 int     dplaces;
  132 M_APM   tmp8, tmp9;
  133 
  134 tmp8 = M_get_stack_var();
  135 tmp9 = M_get_stack_var();
  136 
  137 dplaces = places + 4;
  138 M_check_log_places(dplaces + 45);
  139 
  140 m_apm_log(tmp9, dplaces, aa);
  141 m_apm_multiply(tmp8, tmp9, MM_lc_log10R);
  142 m_apm_round(rr, places, tmp8);
  143 M_restore_stack(2);                    /* restore the 2 locals we used here */
  144 }
  145 /****************************************************************************/
  146 void    m_apm_log(M_APM r, int places, M_APM a)
  147 {
  148 M_APM   tmp0, tmp1, tmp2;
  149 int mexp, dplaces;
  150 
  151 if (a->m_apm_sign <= 0)
  152   {
  153    M_apm_log_error_msg(M_APM_RETURN, "\'m_apm_log\', Negative argument");
  154    M_set_to_zero(r);
  155    return;
  156   }
  157 
  158 tmp0 = M_get_stack_var();
  159 tmp1 = M_get_stack_var();
  160 tmp2 = M_get_stack_var();
  161 
  162 dplaces = places + 8;
  163 
  164 /*
  165  *    if the input is real close to 1, use the series expansion
  166  *    to compute the log.
  167  *    
  168  *    0.9999 < a < 1.0001
  169  */
  170 
  171 mexp = a->m_apm_exponent;
  172 
  173 if (mexp == 0 || mexp == 1)
  174   {
  175    m_apm_subtract(tmp0, a, MM_One);
  176    
  177    if (tmp0->m_apm_sign == 0)    /* is input exactly 1 ?? */
  178      {                           /* if so, result is 0    */
  179       M_set_to_zero(r);
  180       M_restore_stack(3);   
  181       return;
  182      }
  183    
  184    if (tmp0->m_apm_exponent <= -4)
  185      {
  186       M_log_near_1(r, places, tmp0);
  187       M_restore_stack(3);   
  188       return;
  189      }
  190   }
  191 
  192 /* make sure our log(10) is accurate enough for this calculation */
  193 /* (and log(2) which is called from M_log_basic_iteration) */
  194 
  195 M_check_log_places(dplaces + 25);
  196 
  197 if (abs(mexp) <= 3)
  198   {
  199    M_log_basic_iteration(r, places, a);
  200   }
  201 else
  202   {
  203    /*
  204     *  use log (x * y) = log(x) + log(y)
  205     *
  206     *  here we use y = exponent of our base 10 number.
  207     *
  208     *  let 'C' = log(10) = 2.3025850929940....
  209     *
  210     *  then log(x * y) = log(x) + ( C * base_10_exponent )
  211     */
  212 
  213    m_apm_copy(tmp2, a);
  214    
  215    mexp = tmp2->m_apm_exponent - 2;
  216    tmp2->m_apm_exponent = 2;              /* force number between 10 & 100 */
  217    
  218    M_log_basic_iteration(tmp0, dplaces, tmp2);
  219    
  220    m_apm_set_long(tmp1, (long)mexp);
  221    m_apm_multiply(tmp2, tmp1, MM_lc_log10);
  222    m_apm_add(tmp1, tmp2, tmp0);
  223    
  224    m_apm_round(r, places, tmp1);
  225   }
  226 
  227 M_restore_stack(3);                    /* restore the 3 locals we used here */
  228 }
  229 /****************************************************************************/