~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/math/round.cc

Renamed more stuff to drizzle.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; version 2 of the License.
9
 
 *
10
 
 *  This program is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with this program; if not, write to the Free Software
17
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
 */
19
 
 
20
 
#include "config.h"
21
 
 
22
 
#include <math.h>
23
 
#include <limits.h>
24
 
 
25
 
#include <limits>
26
 
#include <algorithm>
27
 
 
28
 
#include "drizzled/function/math/round.h"
29
 
 
30
 
namespace drizzled
31
 
{
32
 
 
33
 
extern const double log_10[309];
34
 
 
35
 
 
36
 
using namespace std;
37
 
 
38
 
void Item_func_round::fix_length_and_dec()
39
 
{
40
 
  int      decimals_to_set;
41
 
  int64_t val1;
42
 
  bool     val1_unsigned;
43
 
 
44
 
  unsigned_flag= args[0]->unsigned_flag;
45
 
  if (!args[1]->const_item())
46
 
  {
47
 
    max_length= args[0]->max_length;
48
 
    decimals= args[0]->decimals;
49
 
    if (args[0]->result_type() == DECIMAL_RESULT)
50
 
    {
51
 
      max_length++;
52
 
      hybrid_type= DECIMAL_RESULT;
53
 
    }
54
 
    else
55
 
      hybrid_type= REAL_RESULT;
56
 
    return;
57
 
  }
58
 
 
59
 
  val1= args[1]->val_int();
60
 
  val1_unsigned= args[1]->unsigned_flag;
61
 
  if (val1 < 0)
62
 
    decimals_to_set= val1_unsigned ? INT_MAX : 0;
63
 
  else
64
 
    decimals_to_set= (val1 > INT_MAX) ? INT_MAX : (int) val1;
65
 
 
66
 
  if (args[0]->decimals == NOT_FIXED_DEC)
67
 
  {
68
 
    max_length= args[0]->max_length;
69
 
    decimals= min(decimals_to_set, (int)NOT_FIXED_DEC);
70
 
    hybrid_type= REAL_RESULT;
71
 
    return;
72
 
  }
73
 
 
74
 
  switch (args[0]->result_type()) {
75
 
  case REAL_RESULT:
76
 
  case STRING_RESULT:
77
 
    hybrid_type= REAL_RESULT;
78
 
    decimals= min(decimals_to_set, (int)NOT_FIXED_DEC);
79
 
    max_length= float_length(decimals);
80
 
    break;
81
 
  case INT_RESULT:
82
 
    if ((!decimals_to_set && truncate) || (args[0]->decimal_precision() < DECIMAL_LONGLONG_DIGITS))
83
 
    {
84
 
      int length_can_increase= test(!truncate && (val1 < 0) && !val1_unsigned);
85
 
      max_length= args[0]->max_length + length_can_increase;
86
 
      /* Here we can keep INT_RESULT */
87
 
      hybrid_type= INT_RESULT;
88
 
      decimals= 0;
89
 
      break;
90
 
    }
91
 
    /* fall through */
92
 
  case DECIMAL_RESULT:
93
 
  {
94
 
    hybrid_type= DECIMAL_RESULT;
95
 
    decimals_to_set= min(DECIMAL_MAX_SCALE, decimals_to_set);
96
 
    int decimals_delta= args[0]->decimals - decimals_to_set;
97
 
    int precision= args[0]->decimal_precision();
98
 
    int length_increase= ((decimals_delta <= 0) || truncate) ? 0:1;
99
 
 
100
 
    precision-= decimals_delta - length_increase;
101
 
    decimals= min(decimals_to_set, DECIMAL_MAX_SCALE);
102
 
    max_length= my_decimal_precision_to_length(precision, decimals,
103
 
                                               unsigned_flag);
104
 
    break;
105
 
  }
106
 
  default:
107
 
    assert(0); /* This result type isn't handled */
108
 
  }
109
 
}
110
 
 
111
 
double my_double_round(double value, int64_t dec, bool dec_unsigned,
112
 
                       bool truncate)
113
 
{
114
 
  double tmp;
115
 
  bool dec_negative= (dec < 0) && !dec_unsigned;
116
 
  uint64_t abs_dec= dec_negative ? -dec : dec;
117
 
  /*
118
 
    tmp2 is here to avoid return the value with 80 bit precision
119
 
    This will fix that the test round(0.1,1) = round(0.1,1) is true
120
 
  */
121
 
  double tmp2;
122
 
 
123
 
  tmp=(abs_dec < array_elements(log_10) ?
124
 
       log_10[abs_dec] : pow(10.0,(double) abs_dec));
125
 
 
126
 
  double value_times_tmp= value * tmp;
127
 
 
128
 
  /*
129
 
    NOTE: This is a workaround for a gcc 4.3 bug on Intel x86 32bit
130
 
    See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39228
131
 
    See http://bugs.mysql.com/bug.php?id=42965
132
 
 
133
 
    This forces the compiler to store/load the value as 64bit and avoids
134
 
    an optimisation that *could* have the infinite check be done on the 80bit
135
 
    representation.
136
 
   */
137
 
  if(sizeof(double) < sizeof(double_t))
138
 
  {
139
 
    volatile double t= value_times_tmp;
140
 
    value_times_tmp= t;
141
 
  }
142
 
 
143
 
  double infinity= numeric_limits<double>::infinity();
144
 
  if (dec_negative && (tmp == infinity))
145
 
    tmp2= 0;
146
 
  else if (!dec_negative && (value_times_tmp == infinity))
147
 
    tmp2= value;
148
 
  else if (truncate)
149
 
  {
150
 
    if (value >= 0)
151
 
      tmp2= dec < 0 ? floor(value/tmp)*tmp : floor(value*tmp)/tmp;
152
 
    else
153
 
      tmp2= dec < 0 ? ceil(value/tmp)*tmp : ceil(value*tmp)/tmp;
154
 
  }
155
 
  else
156
 
    tmp2=dec < 0 ? rint(value/tmp)*tmp : rint(value*tmp)/tmp;
157
 
  return tmp2;
158
 
}
159
 
 
160
 
 
161
 
double Item_func_round::real_op()
162
 
{
163
 
  double value= args[0]->val_real();
164
 
 
165
 
  if (!(null_value= args[0]->null_value || args[1]->null_value))
166
 
    return my_double_round(value, args[1]->val_int(), args[1]->unsigned_flag,
167
 
                           truncate);
168
 
 
169
 
  return 0.0;
170
 
}
171
 
 
172
 
/*
173
 
  Rounds a given value to a power of 10 specified as the 'to' argument,
174
 
  avoiding overflows when the value is close to the uint64_t range boundary.
175
 
*/
176
 
 
177
 
static inline uint64_t my_unsigned_round(uint64_t value, uint64_t to)
178
 
{
179
 
  uint64_t tmp= value / to * to;
180
 
  return (value - tmp < (to >> 1)) ? tmp : tmp + to;
181
 
}
182
 
 
183
 
 
184
 
int64_t Item_func_round::int_op()
185
 
{
186
 
  int64_t value= args[0]->val_int();
187
 
  int64_t dec= args[1]->val_int();
188
 
  decimals= 0;
189
 
  uint64_t abs_dec;
190
 
  if ((null_value= args[0]->null_value || args[1]->null_value))
191
 
    return 0;
192
 
  if ((dec >= 0) || args[1]->unsigned_flag)
193
 
    return value; // integer have not digits after point
194
 
 
195
 
  abs_dec= -dec;
196
 
  int64_t tmp;
197
 
 
198
 
  if(abs_dec >= array_elements(log_10_int))
199
 
    return 0;
200
 
 
201
 
  tmp= log_10_int[abs_dec];
202
 
 
203
 
  if (truncate)
204
 
    value= (unsigned_flag) ?
205
 
      ((uint64_t) value / tmp) * tmp : (value / tmp) * tmp;
206
 
  else
207
 
    value= (unsigned_flag || value >= 0) ?
208
 
      my_unsigned_round((uint64_t) value, tmp) :
209
 
      -(int64_t) my_unsigned_round((uint64_t) -value, tmp);
210
 
  return value;
211
 
}
212
 
 
213
 
 
214
 
my_decimal *Item_func_round::decimal_op(my_decimal *decimal_value)
215
 
{
216
 
  my_decimal val, *value= args[0]->val_decimal(&val);
217
 
  int64_t dec= args[1]->val_int();
218
 
 
219
 
  if (dec >= 0 || args[1]->unsigned_flag)
220
 
    dec= min(dec, (int64_t) decimals);
221
 
  else if (dec < INT_MIN)
222
 
    dec= INT_MIN;
223
 
 
224
 
  if (!(null_value= (args[0]->null_value || args[1]->null_value ||
225
 
                     my_decimal_round(E_DEC_FATAL_ERROR, value, (int) dec,
226
 
                                      truncate, decimal_value) > 1)))
227
 
  {
228
 
    decimal_value->frac= decimals;
229
 
    return decimal_value;
230
 
  }
231
 
  return 0;
232
 
}
233
 
 
234
 
} /* namespace drizzled */