1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
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.
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.
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
28
#include "drizzled/function/math/round.h"
33
extern const double log_10[309];
38
void Item_func_round::fix_length_and_dec()
44
unsigned_flag= args[0]->unsigned_flag;
45
if (!args[1]->const_item())
47
max_length= args[0]->max_length;
48
decimals= args[0]->decimals;
49
if (args[0]->result_type() == DECIMAL_RESULT)
52
hybrid_type= DECIMAL_RESULT;
55
hybrid_type= REAL_RESULT;
59
val1= args[1]->val_int();
60
val1_unsigned= args[1]->unsigned_flag;
62
decimals_to_set= val1_unsigned ? INT_MAX : 0;
64
decimals_to_set= (val1 > INT_MAX) ? INT_MAX : (int) val1;
66
if (args[0]->decimals == NOT_FIXED_DEC)
68
max_length= args[0]->max_length;
69
decimals= min(decimals_to_set, (int)NOT_FIXED_DEC);
70
hybrid_type= REAL_RESULT;
74
switch (args[0]->result_type()) {
77
hybrid_type= REAL_RESULT;
78
decimals= min(decimals_to_set, (int)NOT_FIXED_DEC);
79
max_length= float_length(decimals);
82
if ((!decimals_to_set && truncate) || (args[0]->decimal_precision() < DECIMAL_LONGLONG_DIGITS))
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;
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;
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,
107
assert(0); /* This result type isn't handled */
111
double my_double_round(double value, int64_t dec, bool dec_unsigned,
115
bool dec_negative= (dec < 0) && !dec_unsigned;
116
uint64_t abs_dec= dec_negative ? -dec : dec;
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
123
tmp=(abs_dec < array_elements(log_10) ?
124
log_10[abs_dec] : pow(10.0,(double) abs_dec));
126
double value_times_tmp= value * tmp;
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
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
137
if(sizeof(double) < sizeof(double_t))
139
volatile double t= value_times_tmp;
143
double infinity= numeric_limits<double>::infinity();
144
if (dec_negative && (tmp == infinity))
146
else if (!dec_negative && (value_times_tmp == infinity))
151
tmp2= dec < 0 ? floor(value/tmp)*tmp : floor(value*tmp)/tmp;
153
tmp2= dec < 0 ? ceil(value/tmp)*tmp : ceil(value*tmp)/tmp;
156
tmp2=dec < 0 ? rint(value/tmp)*tmp : rint(value*tmp)/tmp;
161
double Item_func_round::real_op()
163
double value= args[0]->val_real();
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,
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.
177
static inline uint64_t my_unsigned_round(uint64_t value, uint64_t to)
179
uint64_t tmp= value / to * to;
180
return (value - tmp < (to >> 1)) ? tmp : tmp + to;
184
int64_t Item_func_round::int_op()
186
int64_t value= args[0]->val_int();
187
int64_t dec= args[1]->val_int();
190
if ((null_value= args[0]->null_value || args[1]->null_value))
192
if ((dec >= 0) || args[1]->unsigned_flag)
193
return value; // integer have not digits after point
198
if(abs_dec >= array_elements(log_10_int))
201
tmp= log_10_int[abs_dec];
204
value= (unsigned_flag) ?
205
((uint64_t) value / tmp) * tmp : (value / tmp) * tmp;
207
value= (unsigned_flag || value >= 0) ?
208
my_unsigned_round((uint64_t) value, tmp) :
209
-(int64_t) my_unsigned_round((uint64_t) -value, tmp);
214
my_decimal *Item_func_round::decimal_op(my_decimal *decimal_value)
216
my_decimal val, *value= args[0]->val_decimal(&val);
217
int64_t dec= args[1]->val_int();
219
if (dec >= 0 || args[1]->unsigned_flag)
220
dec= min(dec, (int64_t) decimals);
221
else if (dec < INT_MIN)
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)))
228
decimal_value->frac= decimals;
229
return decimal_value;
234
} /* namespace drizzled */