~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/functions/round.cc

  • Committer: Monty Taylor
  • Date: 2008-10-13 09:29:43 UTC
  • mfrom: (509 drizzle)
  • mto: (509.1.4 codestyle)
  • mto: This revision was merged to the branch mainline in revision 511.
  • Revision ID: monty@inaugust.com-20081013092943-rwvx4a6d85b5l2dh
MergedĀ inĀ trunk.

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