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

« back to all changes in this revision

Viewing changes to pgadmin/pgscript/utilities/m_apm/mapm_pow.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_pow.c
 
4
 *
 
5
 *  Copyright (C) 2000 - 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 POW function.
 
24
 *
 
25
 */
 
26
 
 
27
#include "pgAdmin3.h"
 
28
#include "pgscript/utilities/mapm-lib/m_apm_lc.h"
 
29
 
 
30
static  M_APM   M_last_xx_input;
 
31
static  M_APM   M_last_xx_log;
 
32
static  int     M_last_log_digits;
 
33
static  int     M_size_flag = 0;
 
34
 
 
35
/****************************************************************************/
 
36
void    M_free_all_pow()
 
37
{
 
38
if (M_size_flag != 0)
 
39
  {
 
40
   m_apm_free(M_last_xx_input);
 
41
   m_apm_free(M_last_xx_log);
 
42
   M_size_flag = 0;
 
43
  }
 
44
}
 
45
/****************************************************************************/
 
46
/*
 
47
        Calculate the POW function by calling EXP :
 
48
 
 
49
                  Y      A                 
 
50
                 X   =  e    where A = Y * log(X)
 
51
*/
 
52
void    m_apm_pow(M_APM rr, int places, M_APM xx, M_APM yy)
 
53
{
 
54
int     iflag, pflag;
 
55
char    sbuf[64];
 
56
M_APM   tmp8, tmp9;
 
57
 
 
58
/* if yy == 0, return 1 */
 
59
 
 
60
if (yy->m_apm_sign == 0)
 
61
  {
 
62
   m_apm_copy(rr, MM_One);
 
63
   return;
 
64
  }
 
65
 
 
66
/* if xx == 0, return 0 */
 
67
 
 
68
if (xx->m_apm_sign == 0)
 
69
  {
 
70
   M_set_to_zero(rr);
 
71
   return;
 
72
  }
 
73
 
 
74
if (M_size_flag == 0)       /* init locals on first call */
 
75
  {
 
76
   M_size_flag       = M_get_sizeof_int();
 
77
   M_last_log_digits = 0;
 
78
   M_last_xx_input   = m_apm_init();
 
79
   M_last_xx_log     = m_apm_init();
 
80
  }
 
81
 
 
82
/*
 
83
 *  if 'yy' is a small enough integer, call the more
 
84
 *  efficient _integer_pow function.
 
85
 */
 
86
 
 
87
if (m_apm_is_integer(yy))
 
88
  {
 
89
   iflag = FALSE;
 
90
 
 
91
   if (M_size_flag == 2)            /* 16 bit compilers */
 
92
     {
 
93
      if (yy->m_apm_exponent <= 4)
 
94
        iflag = TRUE;
 
95
     }
 
96
   else                             /* >= 32 bit compilers */
 
97
     {
 
98
      if (yy->m_apm_exponent <= 7)
 
99
        iflag = TRUE;
 
100
     }
 
101
 
 
102
   if (iflag)
 
103
     {
 
104
      m_apm_to_integer_string(sbuf, yy);
 
105
      m_apm_integer_pow(rr, places, xx, atoi(sbuf));
 
106
      return;
 
107
     }
 
108
  }
 
109
 
 
110
tmp8 = M_get_stack_var();
 
111
tmp9 = M_get_stack_var();
 
112
 
 
113
/*
 
114
 *    If parameter 'X' is the same this call as it 
 
115
 *    was the previous call, re-use the saved log 
 
116
 *    calculation from last time.
 
117
 */
 
118
 
 
119
pflag = FALSE;
 
120
 
 
121
if (M_last_log_digits >= places)
 
122
  {
 
123
   if (m_apm_compare(xx, M_last_xx_input) == 0)
 
124
     pflag = TRUE;
 
125
  }
 
126
 
 
127
if (pflag)
 
128
  {
 
129
   m_apm_round(tmp9, (places + 8), M_last_xx_log);
 
130
  }
 
131
else
 
132
  {
 
133
   m_apm_log(tmp9, (places + 8), xx);
 
134
 
 
135
   M_last_log_digits = places + 2;
 
136
 
 
137
   /* save the 'X' input value and the log calculation */
 
138
 
 
139
   m_apm_copy(M_last_xx_input, xx);
 
140
   m_apm_copy(M_last_xx_log, tmp9);
 
141
  }
 
142
 
 
143
m_apm_multiply(tmp8, tmp9, yy);
 
144
m_apm_exp(rr, places, tmp8);
 
145
M_restore_stack(2);                    /* restore the 2 locals we used here */
 
146
}
 
147
/****************************************************************************/