~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

mergeĀ mainline

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
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
 
#include "drizzled/util/test.h"
30
 
 
31
 
namespace drizzled
32
 
{
33
 
 
34
 
extern const double log_10[309];
35
 
 
36
 
 
 
20
#include <drizzled/server_includes.h>
 
21
#include CSTDINT_H
 
22
#include <drizzled/function/math/round.h>
 
23
#include CMATH_H
 
24
#include <drizzled/util/math.h>
 
25
 
 
26
#if defined(CMATH_NAMESPACE)
 
27
using namespace CMATH_NAMESPACE;
 
28
#endif
37
29
using namespace std;
38
30
 
39
31
void Item_func_round::fix_length_and_dec()
67
59
  if (args[0]->decimals == NOT_FIXED_DEC)
68
60
  {
69
61
    max_length= args[0]->max_length;
70
 
    decimals= min(decimals_to_set, (int)NOT_FIXED_DEC);
 
62
    decimals= cmin(decimals_to_set, NOT_FIXED_DEC);
71
63
    hybrid_type= REAL_RESULT;
72
64
    return;
73
65
  }
76
68
  case REAL_RESULT:
77
69
  case STRING_RESULT:
78
70
    hybrid_type= REAL_RESULT;
79
 
    decimals= min(decimals_to_set, (int)NOT_FIXED_DEC);
 
71
    decimals= cmin(decimals_to_set, NOT_FIXED_DEC);
80
72
    max_length= float_length(decimals);
81
73
    break;
82
74
  case INT_RESULT:
93
85
  case DECIMAL_RESULT:
94
86
  {
95
87
    hybrid_type= DECIMAL_RESULT;
96
 
    decimals_to_set= min(DECIMAL_MAX_SCALE, decimals_to_set);
 
88
    decimals_to_set= cmin(DECIMAL_MAX_SCALE, decimals_to_set);
97
89
    int decimals_delta= args[0]->decimals - decimals_to_set;
98
90
    int precision= args[0]->decimal_precision();
99
91
    int length_increase= ((decimals_delta <= 0) || truncate) ? 0:1;
100
92
 
101
93
    precision-= decimals_delta - length_increase;
102
 
    decimals= min(decimals_to_set, DECIMAL_MAX_SCALE);
 
94
    decimals= cmin(decimals_to_set, DECIMAL_MAX_SCALE);
103
95
    max_length= my_decimal_precision_to_length(precision, decimals,
104
96
                                               unsigned_flag);
105
97
    break;
132
124
    See http://bugs.mysql.com/bug.php?id=42965
133
125
 
134
126
    This forces the compiler to store/load the value as 64bit and avoids
135
 
    an optimisation that *could* have the infinite check be done on the 80bit
 
127
    an optimisation that *could* have the isinf() be done on the 80bit
136
128
    representation.
137
129
   */
138
130
  if(sizeof(double) < sizeof(double_t))
141
133
    value_times_tmp= t;
142
134
  }
143
135
 
144
 
  double infinity= numeric_limits<double>::infinity();
145
 
  if (dec_negative && (tmp == infinity))
 
136
  if (dec_negative && isinf(tmp))
146
137
    tmp2= 0;
147
 
  else if (!dec_negative && (value_times_tmp == infinity))
 
138
  else if (!dec_negative && isinf(value_times_tmp))
148
139
    tmp2= value;
149
140
  else if (truncate)
150
141
  {
203
194
 
204
195
  if (truncate)
205
196
    value= (unsigned_flag) ?
206
 
      (int64_t)(((uint64_t) value / tmp) * tmp) : (value / tmp) * tmp;
 
197
      ((uint64_t) value / tmp) * tmp : (value / tmp) * tmp;
207
198
  else
208
199
    value= (unsigned_flag || value >= 0) ?
209
 
      (int64_t)(my_unsigned_round((uint64_t) value, tmp)) :
 
200
      my_unsigned_round((uint64_t) value, tmp) :
210
201
      -(int64_t) my_unsigned_round((uint64_t) -value, tmp);
211
202
  return value;
212
203
}
216
207
{
217
208
  my_decimal val, *value= args[0]->val_decimal(&val);
218
209
  int64_t dec= args[1]->val_int();
219
 
 
220
210
  if (dec >= 0 || args[1]->unsigned_flag)
221
 
    dec= min(dec, (int64_t) decimals);
 
211
    dec= cmin(dec, (int64_t) decimals);
222
212
  else if (dec < INT_MIN)
223
213
    dec= INT_MIN;
224
214
 
232
222
  return 0;
233
223
}
234
224
 
235
 
} /* namespace drizzled */