~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/real.cc

Merged from jay.

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 MySQL
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; either version 2 of the License, or
9
 
 *  (at your option) any later version.
10
 
 *
11
 
 *  This program is distributed in the hope that it will be useful,
12
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 *  GNU General Public License for more details.
15
 
 *
16
 
 *  You should have received a copy of the GNU General Public License
17
 
 *  along with this program; if not, write to the Free Software
18
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 
 */
20
 
 
21
 
#include <drizzled/server_includes.h>
22
 
#include <drizzled/field/real.h>
23
 
#include <drizzled/error.h>
24
 
#include CMATH_H
25
 
#include <drizzled/util/math.h>
26
 
 
27
 
#if defined(CMATH_NAMESPACE)
28
 
using namespace CMATH_NAMESPACE;
29
 
#endif
30
 
 
31
 
/*
32
 
  Floating-point numbers
33
 
 */
34
 
 
35
 
unsigned char *
36
 
Field_real::pack(unsigned char *to, const unsigned char *from,
37
 
                 uint32_t max_length, bool low_byte_first)
38
 
{
39
 
  assert(max_length >= pack_length());
40
 
#ifdef WORDS_BIGENDIAN
41
 
  if (low_byte_first != table->s->db_low_byte_first)
42
 
  {
43
 
    const unsigned char *dptr= from + pack_length();
44
 
    while (dptr-- > from)
45
 
      *to++ = *dptr;
46
 
    return(to);
47
 
  }
48
 
  else
49
 
#endif
50
 
    return(Field::pack(to, from, max_length, low_byte_first));
51
 
}
52
 
 
53
 
const unsigned char *
54
 
Field_real::unpack(unsigned char *to, const unsigned char *from,
55
 
                   uint32_t param_data, bool low_byte_first)
56
 
{
57
 
#ifdef WORDS_BIGENDIAN
58
 
  if (low_byte_first != table->s->db_low_byte_first)
59
 
  {
60
 
    const unsigned char *dptr= from + pack_length();
61
 
    while (dptr-- > from)
62
 
      *to++ = *dptr;
63
 
    return(from + pack_length());
64
 
  }
65
 
  else
66
 
#endif
67
 
    return(Field::unpack(to, from, param_data, low_byte_first));
68
 
}
69
 
 
70
 
/*
71
 
  If a field has fixed length, truncate the double argument pointed to by 'nr'
72
 
  appropriately.
73
 
  Also ensure that the argument is within [-max_value; max_value] range.
74
 
*/
75
 
 
76
 
int Field_real::truncate(double *nr, double max_value)
77
 
{
78
 
  int error= 1;
79
 
  double res= *nr;
80
 
  
81
 
  if (isnan(res))
82
 
  {
83
 
    res= 0;
84
 
    set_null();
85
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
86
 
    goto end;
87
 
  }
88
 
 
89
 
  if (!not_fixed)
90
 
  {
91
 
    uint32_t order= field_length - dec;
92
 
    uint32_t step= array_elements(log_10) - 1;
93
 
    max_value= 1.0;
94
 
    for (; order > step; order-= step)
95
 
      max_value*= log_10[step];
96
 
    max_value*= log_10[order];
97
 
    max_value-= 1.0 / log_10[dec];
98
 
 
99
 
    /* Check for infinity so we don't get NaN in calculations */
100
 
    if (!(isinf(res)))
101
 
    {
102
 
      double tmp= rint((res - floor(res)) * log_10[dec]) / log_10[dec];
103
 
      res= floor(res) + tmp;
104
 
    }
105
 
  }
106
 
  
107
 
  if (res < -max_value)
108
 
  {
109
 
   res= -max_value;
110
 
   set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
111
 
  }
112
 
  else if (res > max_value)
113
 
  {
114
 
    res= max_value;
115
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
116
 
  }
117
 
  else
118
 
    error= 0;
119
 
 
120
 
end:
121
 
  *nr= res;
122
 
  return error;
123
 
}
124
 
 
125
 
 
126
 
int Field_real::store_decimal(const my_decimal *dm)
127
 
{
128
 
  double dbl;
129
 
  my_decimal2double(E_DEC_FATAL_ERROR, dm, &dbl);
130
 
  return store(dbl);
131
 
}
132
 
 
133
 
my_decimal *Field_real::val_decimal(my_decimal *decimal_value)
134
 
{
135
 
  double2my_decimal(E_DEC_FATAL_ERROR, val_real(), decimal_value);
136
 
  return decimal_value;
137
 
}