~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/real.cc

Merge of 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 <drizzled/table.h>
 
25
 
 
26
#include <limits>
 
27
 
 
28
using namespace std;
 
29
 
 
30
/*
 
31
  Floating-point numbers
 
32
 */
 
33
 
 
34
unsigned char *
 
35
Field_real::pack(unsigned char *to, const unsigned char *from,
 
36
                 uint32_t max_length, bool low_byte_first)
 
37
{
 
38
  assert(max_length >= pack_length());
 
39
#ifdef WORDS_BIGENDIAN
 
40
  if (low_byte_first != table->s->db_low_byte_first)
 
41
  {
 
42
    const unsigned char *dptr= from + pack_length();
 
43
    while (dptr-- > from)
 
44
      *to++ = *dptr;
 
45
    return(to);
 
46
  }
 
47
  else
 
48
#endif
 
49
    return(Field::pack(to, from, max_length, low_byte_first));
 
50
}
 
51
 
 
52
const unsigned char *
 
53
Field_real::unpack(unsigned char *to, const unsigned char *from,
 
54
                   uint32_t param_data, bool low_byte_first)
 
55
{
 
56
#ifdef WORDS_BIGENDIAN
 
57
  if (low_byte_first != table->s->db_low_byte_first)
 
58
  {
 
59
    const unsigned char *dptr= from + pack_length();
 
60
    while (dptr-- > from)
 
61
      *to++ = *dptr;
 
62
    return(from + pack_length());
 
63
  }
 
64
  else
 
65
#endif
 
66
    return(Field::unpack(to, from, param_data, low_byte_first));
 
67
}
 
68
 
 
69
/*
 
70
  If a field has fixed length, truncate the double argument pointed to by 'nr'
 
71
  appropriately.
 
72
  Also ensure that the argument is within [-max_value; max_value] range.
 
73
*/
 
74
 
 
75
int Field_real::truncate(double *nr, double max_value)
 
76
{
 
77
  int error= 1;
 
78
  double res= *nr;
 
79
 
 
80
  if (res == numeric_limits<double>::quiet_NaN())
 
81
  {
 
82
    res= 0;
 
83
    set_null();
 
84
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
85
    goto end;
 
86
  }
 
87
 
 
88
  if (!not_fixed)
 
89
  {
 
90
    uint32_t order= field_length - dec;
 
91
    uint32_t step= array_elements(log_10) - 1;
 
92
    max_value= 1.0;
 
93
    for (; order > step; order-= step)
 
94
      max_value*= log_10[step];
 
95
    max_value*= log_10[order];
 
96
    max_value-= 1.0 / log_10[dec];
 
97
 
 
98
    /* Check for infinity so we don't get NaN in calculations */
 
99
    if (res != numeric_limits<double>::infinity())
 
100
    {
 
101
      double tmp= rint((res - floor(res)) * log_10[dec]) / log_10[dec];
 
102
      res= floor(res) + tmp;
 
103
    }
 
104
  }
 
105
 
 
106
  if (res < -max_value)
 
107
  {
 
108
   res= -max_value;
 
109
   set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
110
  }
 
111
  else if (res > max_value)
 
112
  {
 
113
    res= max_value;
 
114
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
115
  }
 
116
  else
 
117
    error= 0;
 
118
 
 
119
end:
 
120
  *nr= res;
 
121
  return error;
 
122
}
 
123
 
 
124
 
 
125
int Field_real::store_decimal(const my_decimal *dm)
 
126
{
 
127
  double dbl;
 
128
  my_decimal2double(E_DEC_FATAL_ERROR, dm, &dbl);
 
129
  return store(dbl);
 
130
}
 
131
 
 
132
my_decimal *Field_real::val_decimal(my_decimal *decimal_value)
 
133
{
 
134
  ASSERT_COLUMN_MARKED_FOR_READ;
 
135
 
 
136
  double2my_decimal(E_DEC_FATAL_ERROR, val_real(), decimal_value);
 
137
  return decimal_value;
 
138
}