5 * Copyright (C) 2000 - 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_rcp.c,v 1.7 2007/12/03 01:46:46 mike Exp $
24 * This file contains the fast division and reciprocal functions
26 * $Log: mapm_rcp.c,v $
27 * Revision 1.7 2007/12/03 01:46:46 mike
30 * Revision 1.6 2003/07/21 20:20:17 mike
31 * Modify error messages to be in a consistent format.
33 * Revision 1.5 2003/05/01 21:58:40 mike
36 * Revision 1.4 2003/03/31 22:15:49 mike
37 * call generic error handling function
39 * Revision 1.3 2002/11/03 21:32:09 mike
40 * Updated function parameters to use the modern style
42 * Revision 1.2 2000/09/26 16:27:48 mike
45 * Revision 1.1 2000/09/26 16:16:00 mike
51 /****************************************************************************/
52 void m_apm_divide(M_APM rr
, int places
, M_APM aa
, M_APM bb
)
55 int sn
, nexp
, dplaces
;
57 sn
= aa
->m_apm_sign
* bb
->m_apm_sign
;
59 if (sn
== 0) /* one number is zero, result is zero */
61 if (bb
->m_apm_sign
== 0)
63 M_apm_log_error_msg(M_APM_RETURN
, "\'m_apm_divide\', Divide by 0");
71 * Use the original 'Knuth' method for smaller divides. On the
72 * author's system, this was the *approx* break even point before
73 * the reciprocal method used below became faster.
78 M_apm_sdivide(rr
, places
, aa
, bb
);
82 /* mimic the decimal place behavior of the original divide */
84 nexp
= aa
->m_apm_exponent
- bb
->m_apm_exponent
;
87 dplaces
= nexp
+ places
;
91 tmp0
= M_get_stack_var();
92 tmp1
= M_get_stack_var();
94 m_apm_reciprocal(tmp0
, (dplaces
+ 8), bb
);
95 m_apm_multiply(tmp1
, tmp0
, aa
);
96 m_apm_round(rr
, dplaces
, tmp1
);
100 /****************************************************************************/
101 void m_apm_reciprocal(M_APM rr
, int places
, M_APM aa
)
103 M_APM last_x
, guess
, tmpN
, tmp1
, tmp2
;
105 int ii
, bflag
, dplaces
, nexp
, tolerance
;
107 if (aa
->m_apm_sign
== 0)
109 M_apm_log_error_msg(M_APM_RETURN
, "\'m_apm_reciprocal\', Input = 0");
115 last_x
= M_get_stack_var();
116 guess
= M_get_stack_var();
117 tmpN
= M_get_stack_var();
118 tmp1
= M_get_stack_var();
119 tmp2
= M_get_stack_var();
121 m_apm_absolute_value(tmpN
, aa
);
124 normalize the input number (make the exponent 0) so
125 the 'guess' below will not over/under flow on large
129 nexp
= aa
->m_apm_exponent
;
130 tmpN
->m_apm_exponent
-= nexp
;
132 m_apm_to_string(sbuf
, 15, tmpN
);
133 m_apm_set_double(guess
, (1.0 / atof(sbuf
)));
135 tolerance
= places
+ 4;
136 dplaces
= places
+ 16;
139 m_apm_negate(last_x
, MM_Ten
);
141 /* Use the following iteration to calculate the reciprocal :
144 X = X * [ 2 - N * X ]
152 m_apm_multiply(tmp1
, tmpN
, guess
);
153 m_apm_subtract(tmp2
, MM_Two
, tmp1
);
154 m_apm_multiply(tmp1
, tmp2
, guess
);
159 m_apm_round(guess
, dplaces
, tmp1
);
161 /* force at least 2 iterations so 'last_x' has valid data */
165 m_apm_subtract(tmp2
, guess
, last_x
);
167 if (tmp2
->m_apm_sign
== 0)
171 * if we are within a factor of 4 on the error term,
172 * we will be accurate enough after the *next* iteration
176 if ((-4 * tmp2
->m_apm_exponent
) > tolerance
)
180 m_apm_copy(last_x
, guess
);
184 m_apm_round(rr
, places
, tmp1
);
185 rr
->m_apm_exponent
-= nexp
;
186 rr
->m_apm_sign
= aa
->m_apm_sign
;
189 /****************************************************************************/