~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Stewart Smith
  • Date: 2009-06-16 03:02:59 UTC
  • mto: This revision was merged to the branch mainline in revision 1065.
  • Revision ID: stewart@flamingspork.com-20090616030259-tn2thqrajk6cappd
ER_NISAMCHK is unused, mark it as so. Thanks to Paul DuBois for researching this for MySQL.

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 <drizzled/server_includes.h>
 
21
#include <drizzled/function/math/round.h>
 
22
 
 
23
#include <limits>
 
24
 
 
25
using namespace std;
 
26
 
 
27
void Item_func_round::fix_length_and_dec()
 
28
{
 
29
  int      decimals_to_set;
 
30
  int64_t val1;
 
31
  bool     val1_unsigned;
 
32
 
 
33
  unsigned_flag= args[0]->unsigned_flag;
 
34
  if (!args[1]->const_item())
 
35
  {
 
36
    max_length= args[0]->max_length;
 
37
    decimals= args[0]->decimals;
 
38
    if (args[0]->result_type() == DECIMAL_RESULT)
 
39
    {
 
40
      max_length++;
 
41
      hybrid_type= DECIMAL_RESULT;
 
42
    }
 
43
    else
 
44
      hybrid_type= REAL_RESULT;
 
45
    return;
 
46
  }
 
47
 
 
48
  val1= args[1]->val_int();
 
49
  val1_unsigned= args[1]->unsigned_flag;
 
50
  if (val1 < 0)
 
51
    decimals_to_set= val1_unsigned ? INT_MAX : 0;
 
52
  else
 
53
    decimals_to_set= (val1 > INT_MAX) ? INT_MAX : (int) val1;
 
54
 
 
55
  if (args[0]->decimals == NOT_FIXED_DEC)
 
56
  {
 
57
    max_length= args[0]->max_length;
 
58
    decimals= cmin(decimals_to_set, NOT_FIXED_DEC);
 
59
    hybrid_type= REAL_RESULT;
 
60
    return;
 
61
  }
 
62
 
 
63
  switch (args[0]->result_type()) {
 
64
  case REAL_RESULT:
 
65
  case STRING_RESULT:
 
66
    hybrid_type= REAL_RESULT;
 
67
    decimals= cmin(decimals_to_set, NOT_FIXED_DEC);
 
68
    max_length= float_length(decimals);
 
69
    break;
 
70
  case INT_RESULT:
 
71
    if ((!decimals_to_set && truncate) || (args[0]->decimal_precision() < DECIMAL_LONGLONG_DIGITS))
 
72
    {
 
73
      int length_can_increase= test(!truncate && (val1 < 0) && !val1_unsigned);
 
74
      max_length= args[0]->max_length + length_can_increase;
 
75
      /* Here we can keep INT_RESULT */
 
76
      hybrid_type= INT_RESULT;
 
77
      decimals= 0;
 
78
      break;
 
79
    }
 
80
    /* fall through */
 
81
  case DECIMAL_RESULT:
 
82
  {
 
83
    hybrid_type= DECIMAL_RESULT;
 
84
    decimals_to_set= cmin(DECIMAL_MAX_SCALE, decimals_to_set);
 
85
    int decimals_delta= args[0]->decimals - decimals_to_set;
 
86
    int precision= args[0]->decimal_precision();
 
87
    int length_increase= ((decimals_delta <= 0) || truncate) ? 0:1;
 
88
 
 
89
    precision-= decimals_delta - length_increase;
 
90
    decimals= cmin(decimals_to_set, DECIMAL_MAX_SCALE);
 
91
    max_length= my_decimal_precision_to_length(precision, decimals,
 
92
                                               unsigned_flag);
 
93
    break;
 
94
  }
 
95
  default:
 
96
    assert(0); /* This result type isn't handled */
 
97
  }
 
98
}
 
99
 
 
100
double my_double_round(double value, int64_t dec, bool dec_unsigned,
 
101
                       bool truncate)
 
102
{
 
103
  double tmp;
 
104
  bool dec_negative= (dec < 0) && !dec_unsigned;
 
105
  uint64_t abs_dec= dec_negative ? -dec : dec;
 
106
  /*
 
107
    tmp2 is here to avoid return the value with 80 bit precision
 
108
    This will fix that the test round(0.1,1) = round(0.1,1) is true
 
109
  */
 
110
  double tmp2;
 
111
 
 
112
  tmp=(abs_dec < array_elements(log_10) ?
 
113
       log_10[abs_dec] : pow(10.0,(double) abs_dec));
 
114
 
 
115
  double value_times_tmp= value * tmp;
 
116
 
 
117
  /*
 
118
    NOTE: This is a workaround for a gcc 4.3 bug on Intel x86 32bit
 
119
    See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39228
 
120
    See http://bugs.mysql.com/bug.php?id=42965
 
121
 
 
122
    This forces the compiler to store/load the value as 64bit and avoids
 
123
    an optimisation that *could* have the infinite check be done on the 80bit
 
124
    representation.
 
125
   */
 
126
  if(sizeof(double) < sizeof(double_t))
 
127
  {
 
128
    volatile double t= value_times_tmp;
 
129
    value_times_tmp= t;
 
130
  }
 
131
 
 
132
  double infinity= numeric_limits<double>::infinity();
 
133
  if (dec_negative && (tmp == infinity))
 
134
    tmp2= 0;
 
135
  else if (!dec_negative && (value_times_tmp == infinity))
 
136
    tmp2= value;
 
137
  else if (truncate)
 
138
  {
 
139
    if (value >= 0)
 
140
      tmp2= dec < 0 ? floor(value/tmp)*tmp : floor(value*tmp)/tmp;
 
141
    else
 
142
      tmp2= dec < 0 ? ceil(value/tmp)*tmp : ceil(value*tmp)/tmp;
 
143
  }
 
144
  else
 
145
    tmp2=dec < 0 ? rint(value/tmp)*tmp : rint(value*tmp)/tmp;
 
146
  return tmp2;
 
147
}
 
148
 
 
149
 
 
150
double Item_func_round::real_op()
 
151
{
 
152
  double value= args[0]->val_real();
 
153
 
 
154
  if (!(null_value= args[0]->null_value || args[1]->null_value))
 
155
    return my_double_round(value, args[1]->val_int(), args[1]->unsigned_flag,
 
156
                           truncate);
 
157
 
 
158
  return 0.0;
 
159
}
 
160
 
 
161
/*
 
162
  Rounds a given value to a power of 10 specified as the 'to' argument,
 
163
  avoiding overflows when the value is close to the uint64_t range boundary.
 
164
*/
 
165
 
 
166
static inline uint64_t my_unsigned_round(uint64_t value, uint64_t to)
 
167
{
 
168
  uint64_t tmp= value / to * to;
 
169
  return (value - tmp < (to >> 1)) ? tmp : tmp + to;
 
170
}
 
171
 
 
172
 
 
173
int64_t Item_func_round::int_op()
 
174
{
 
175
  int64_t value= args[0]->val_int();
 
176
  int64_t dec= args[1]->val_int();
 
177
  decimals= 0;
 
178
  uint64_t abs_dec;
 
179
  if ((null_value= args[0]->null_value || args[1]->null_value))
 
180
    return 0;
 
181
  if ((dec >= 0) || args[1]->unsigned_flag)
 
182
    return value; // integer have not digits after point
 
183
 
 
184
  abs_dec= -dec;
 
185
  int64_t tmp;
 
186
 
 
187
  if(abs_dec >= array_elements(log_10_int))
 
188
    return 0;
 
189
 
 
190
  tmp= log_10_int[abs_dec];
 
191
 
 
192
  if (truncate)
 
193
    value= (unsigned_flag) ?
 
194
      ((uint64_t) value / tmp) * tmp : (value / tmp) * tmp;
 
195
  else
 
196
    value= (unsigned_flag || value >= 0) ?
 
197
      my_unsigned_round((uint64_t) value, tmp) :
 
198
      -(int64_t) my_unsigned_round((uint64_t) -value, tmp);
 
199
  return value;
 
200
}
 
201
 
 
202
 
 
203
my_decimal *Item_func_round::decimal_op(my_decimal *decimal_value)
 
204
{
 
205
  my_decimal val, *value= args[0]->val_decimal(&val);
 
206
  int64_t dec= args[1]->val_int();
 
207
  if (dec >= 0 || args[1]->unsigned_flag)
 
208
    dec= cmin(dec, (int64_t) decimals);
 
209
  else if (dec < INT_MIN)
 
210
    dec= INT_MIN;
 
211
 
 
212
  if (!(null_value= (args[0]->null_value || args[1]->null_value ||
 
213
                     my_decimal_round(E_DEC_FATAL_ERROR, value, (int) dec,
 
214
                                      truncate, decimal_value) > 1)))
 
215
  {
 
216
    decimal_value->frac= decimals;
 
217
    return decimal_value;
 
218
  }
 
219
  return 0;
 
220
}
 
221