~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/real.cc

MergedĀ fromĀ lee.

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