~ubuntu-branches/debian/sid/pgadmin3/sid

« back to all changes in this revision

Viewing changes to pgadmin/pgscript/utilities/m_apm/mapm_add.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Gerfried Fuchs
  • Date: 2009-07-30 12:27:16 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20090730122716-fddbh42on721bbs2
Tags: 1.10.0-1
* New upstream release.
* Adjusted watch file to match release candidates.
* Updated to Standards-Version 3.8.2:
  - Moved to Section: database.
  - Add DEB_BUILD_OPTIONS support for parallel building.
  - Move from findstring to filter suggestion for DEB_BUILD_OPTIONS parsing.
* pgagent got split into its own separate source package by upstream.
* Exclude Docs.vcproj from installation.
* Move doc-base.enus from pgadmin3 to pgadmin3-data package, the files are
  in there too.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/* 
 
3
 *  M_APM  -  mapm_add.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
 *
 
23
 *      This file contains the functions that implement the sin (5x)
 
24
 *      and cos (4x) multiple angle relations
 
25
 *
 
26
 */
 
27
 
 
28
#include "pgAdmin3.h"
 
29
#include "pgscript/utilities/mapm-lib/m_apm_lc.h"
 
30
 
 
31
static  M_APM   M_work1 = NULL;
 
32
static  M_APM   M_work2 = NULL;
 
33
static  int     M_add_firsttime = TRUE;
 
34
 
 
35
/****************************************************************************/
 
36
void    M_free_all_add()
 
37
{
 
38
if (M_add_firsttime == FALSE)
 
39
  {
 
40
   m_apm_free(M_work1);
 
41
   m_apm_free(M_work2);
 
42
   M_add_firsttime = TRUE;
 
43
  }
 
44
}
 
45
/****************************************************************************/
 
46
void    m_apm_add(M_APM r, M_APM a, M_APM b)
 
47
{
 
48
int     j, carry, sign, aexp, bexp, adigits, bdigits;
 
49
 
 
50
if (M_add_firsttime)
 
51
  {
 
52
   M_add_firsttime = FALSE;
 
53
   M_work1 = m_apm_init();
 
54
   M_work2 = m_apm_init();
 
55
  }
 
56
 
 
57
if (a->m_apm_sign == 0)
 
58
  {
 
59
   m_apm_copy(r,b);
 
60
   return;
 
61
  }
 
62
 
 
63
if (b->m_apm_sign == 0)
 
64
  {
 
65
   m_apm_copy(r,a);
 
66
   return;
 
67
  }
 
68
  
 
69
if (a->m_apm_sign == 1 && b->m_apm_sign == -1)
 
70
  {
 
71
   b->m_apm_sign = 1;
 
72
   m_apm_subtract(r,a,b);
 
73
   b->m_apm_sign = -1;
 
74
   return;
 
75
  }
 
76
 
 
77
if (a->m_apm_sign == -1 && b->m_apm_sign == 1)
 
78
  {
 
79
   a->m_apm_sign = 1;
 
80
   m_apm_subtract(r,b,a);
 
81
   a->m_apm_sign = -1;
 
82
   return;
 
83
  }
 
84
 
 
85
sign = a->m_apm_sign;         /* signs are the same, result will be same */
 
86
 
 
87
aexp = a->m_apm_exponent;
 
88
bexp = b->m_apm_exponent;
 
89
 
 
90
m_apm_copy(M_work1, a);
 
91
m_apm_copy(M_work2, b);
 
92
 
 
93
/*
 
94
 *  scale by at least 1 factor of 10 in case the MSB carrys
 
95
 */
 
96
 
 
97
if (aexp == bexp)
 
98
  {
 
99
   M_apm_scale(M_work1, 2);   /* shift 2 digits == 1 byte for efficiency */
 
100
   M_apm_scale(M_work2, 2);
 
101
  }
 
102
else
 
103
  {
 
104
   if (aexp > bexp)
 
105
     {
 
106
      M_apm_scale(M_work1, 2);
 
107
      M_apm_scale(M_work2, (aexp + 2 - bexp));
 
108
     }
 
109
   else            /*  aexp < bexp  */
 
110
     {
 
111
      M_apm_scale(M_work2, 2);
 
112
      M_apm_scale(M_work1, (bexp + 2 - aexp));
 
113
     }
 
114
  }
 
115
 
 
116
adigits = M_work1->m_apm_datalength;
 
117
bdigits = M_work2->m_apm_datalength;
 
118
 
 
119
if (adigits >= bdigits)
 
120
  {
 
121
   m_apm_copy(r, M_work1);
 
122
   j = (bdigits + 1) >> 1;
 
123
   carry = 0;
 
124
 
 
125
   while (TRUE)
 
126
     {
 
127
      j--;
 
128
      r->m_apm_data[j] += carry + M_work2->m_apm_data[j];
 
129
 
 
130
      if (r->m_apm_data[j] >= 100)
 
131
        {
 
132
         r->m_apm_data[j] -= 100;
 
133
         carry = 1;
 
134
        }
 
135
      else
 
136
         carry = 0;
 
137
 
 
138
      if (j == 0) 
 
139
        break;
 
140
     }
 
141
  }
 
142
else
 
143
  {
 
144
   m_apm_copy(r, M_work2);
 
145
   j = (adigits + 1) >> 1;
 
146
   carry = 0;
 
147
 
 
148
   while (TRUE)
 
149
     {
 
150
      j--;
 
151
      r->m_apm_data[j] += carry + M_work1->m_apm_data[j];
 
152
 
 
153
      if (r->m_apm_data[j] >= 100)
 
154
        {
 
155
         r->m_apm_data[j] -= 100;
 
156
         carry = 1;
 
157
        }
 
158
      else
 
159
         carry = 0;
 
160
 
 
161
      if (j == 0) 
 
162
        break;
 
163
     }
 
164
  }
 
165
 
 
166
r->m_apm_sign = sign;
 
167
 
 
168
M_apm_normalize(r);
 
169
}
 
170
/****************************************************************************/
 
171
void    m_apm_subtract(M_APM r, M_APM a, M_APM b)
 
172
{
 
173
int     itmp, j, flag, icompare, sign, aexp, bexp, 
 
174
        borrow, adigits, bdigits;
 
175
 
 
176
if (M_add_firsttime)
 
177
  {
 
178
   M_add_firsttime = FALSE;
 
179
   M_work1 = m_apm_init();
 
180
   M_work2 = m_apm_init();
 
181
  }
 
182
 
 
183
if (b->m_apm_sign == 0)
 
184
  {
 
185
   m_apm_copy(r,a);
 
186
   return;
 
187
  }
 
188
  
 
189
if (a->m_apm_sign == 0)
 
190
  {
 
191
   m_apm_copy(r,b);
 
192
   r->m_apm_sign = -(r->m_apm_sign);
 
193
   return;
 
194
  }
 
195
 
 
196
if (a->m_apm_sign == 1 && b->m_apm_sign == -1)
 
197
  {
 
198
   b->m_apm_sign = 1;
 
199
   m_apm_add(r,a,b);
 
200
   b->m_apm_sign = -1;
 
201
   return;
 
202
  }
 
203
 
 
204
if (a->m_apm_sign == -1 && b->m_apm_sign == 1)
 
205
  {
 
206
   b->m_apm_sign = -1;
 
207
   m_apm_add(r,a,b);
 
208
   b->m_apm_sign = 1;
 
209
   return;
 
210
  }
 
211
 
 
212
/* now, the signs are the same  */
 
213
/* make a positive working copy */
 
214
 
 
215
m_apm_absolute_value(M_work1, a);
 
216
m_apm_absolute_value(M_work2, b);
 
217
 
 
218
/* are they the same??  if so, the result is zero */
 
219
 
 
220
if ((icompare = m_apm_compare(M_work1, M_work2)) == 0)
 
221
  {
 
222
   M_set_to_zero(r);
 
223
   return;
 
224
  }
 
225
 
 
226
if (icompare == 1)             /*  |a| > |b|  (do A-B)  */
 
227
  {
 
228
   flag = TRUE;
 
229
   sign = a->m_apm_sign;     
 
230
  }
 
231
else                           /*  |b| > |a|  (do B-A)  */
 
232
  {
 
233
   flag = FALSE;
 
234
   sign = -(a->m_apm_sign);     
 
235
  }
 
236
 
 
237
aexp = M_work1->m_apm_exponent;
 
238
bexp = M_work2->m_apm_exponent;
 
239
 
 
240
if (aexp > bexp)
 
241
  M_apm_scale(M_work2, (aexp - bexp));
 
242
 
 
243
if (aexp < bexp)
 
244
  M_apm_scale(M_work1, (bexp - aexp));
 
245
 
 
246
adigits = M_work1->m_apm_datalength;
 
247
bdigits = M_work2->m_apm_datalength;
 
248
 
 
249
if (adigits > bdigits)
 
250
  M_apm_pad(M_work2, adigits);
 
251
 
 
252
if (adigits < bdigits)
 
253
  M_apm_pad(M_work1, bdigits);
 
254
 
 
255
if (flag)               /* perform A-B,  M_work1 - M_work2 */
 
256
  {
 
257
   m_apm_copy(r, M_work1);
 
258
   j = (r->m_apm_datalength + 1) >> 1;
 
259
   borrow = 0;
 
260
 
 
261
   while (TRUE)
 
262
     {
 
263
      j--;
 
264
      itmp = (int)r->m_apm_data[j] - ((int)M_work2->m_apm_data[j] + borrow);
 
265
 
 
266
      if (itmp >= 0)
 
267
        {
 
268
         r->m_apm_data[j] = (UCHAR)itmp;
 
269
         borrow = 0;
 
270
        }
 
271
      else
 
272
        {
 
273
         r->m_apm_data[j] = (UCHAR)(100 + itmp);
 
274
         borrow = 1;
 
275
        }
 
276
 
 
277
      if (j == 0) 
 
278
        break;
 
279
     }
 
280
  }
 
281
else            /* perform B-A,  M_work2 - M_work1 */
 
282
  {
 
283
   m_apm_copy(r, M_work2);
 
284
   j = (r->m_apm_datalength + 1) >> 1;
 
285
   borrow = 0;
 
286
 
 
287
   while (TRUE)
 
288
     {
 
289
      j--;
 
290
      itmp = (int)r->m_apm_data[j] - ((int)M_work1->m_apm_data[j] + borrow);
 
291
 
 
292
      if (itmp >= 0)
 
293
        {
 
294
         r->m_apm_data[j] = (UCHAR)itmp;
 
295
         borrow = 0;
 
296
        }
 
297
      else
 
298
        {
 
299
         r->m_apm_data[j] = (UCHAR)(100 + itmp);
 
300
         borrow = 1;
 
301
        }
 
302
 
 
303
      if (j == 0) 
 
304
        break;
 
305
     }
 
306
  }
 
307
   
 
308
r->m_apm_sign = sign;
 
309
 
 
310
M_apm_normalize(r);
 
311
}
 
312
/****************************************************************************/
 
313
 
 
314
#ifdef __cplusplus
 
315
 
 
316
MAPM operator+(const MAPM &a,const MAPM &b)
 
317
        {MAPM ret;m_apm_add(ret.val(),a.cval(),b.cval());return ret;}
 
318
MAPM operator-(const MAPM &a,const MAPM &b)
 
319
        {MAPM ret;m_apm_subtract(ret.val(),a.cval(),b.cval());return ret;}
 
320
 
 
321
#endif