~drizzle-trunk/drizzle/development

483.1.1 by Lee
latest code cleanup for Field_real
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
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
21
#include "config.h"
483.1.1 by Lee
latest code cleanup for Field_real
22
#include <drizzled/field/real.h>
550 by Monty Taylor
Moved error.h into just the files that need it.
23
#include <drizzled/error.h>
590.3.1 by Lee Bieber
changes to get build working on Solaris
24
#include <drizzled/table.h>
572.1.4 by Monty Taylor
Removed a bunch of unusued tests and defines from autoconf.
25
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
26
#include <math.h>
27
919.2.9 by Monty Taylor
Replaced C99 isinf() with C++ numeric_traits<>::infinity()
28
#include <limits>
29
919.2.10 by Monty Taylor
Remove C99 isfinite().
30
using namespace std;
483.1.1 by Lee
latest code cleanup for Field_real
31
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
32
namespace drizzled
33
{
34
1241.9.33 by Monty Taylor
Moved most of the global vars to set_var where they belong.
35
extern const double log_10[309];
36
483.1.1 by Lee
latest code cleanup for Field_real
37
/*
38
  Floating-point numbers
39
 */
40
41
unsigned char *
42
Field_real::pack(unsigned char *to, const unsigned char *from,
43
                 uint32_t max_length, bool low_byte_first)
44
{
45
  assert(max_length >= pack_length());
46
#ifdef WORDS_BIGENDIAN
1830.1.3 by Brian Aker
Fix for Solaris
47
  if (low_byte_first != getTable()->getShare()->db_low_byte_first)
483.1.1 by Lee
latest code cleanup for Field_real
48
  {
49
    const unsigned char *dptr= from + pack_length();
50
    while (dptr-- > from)
51
      *to++ = *dptr;
52
    return(to);
53
  }
54
  else
55
#endif
56
    return(Field::pack(to, from, max_length, low_byte_first));
57
}
58
59
const unsigned char *
60
Field_real::unpack(unsigned char *to, const unsigned char *from,
61
                   uint32_t param_data, bool low_byte_first)
62
{
63
#ifdef WORDS_BIGENDIAN
1830.1.3 by Brian Aker
Fix for Solaris
64
  if (low_byte_first != getTable()->getShare()->db_low_byte_first)
483.1.1 by Lee
latest code cleanup for Field_real
65
  {
66
    const unsigned char *dptr= from + pack_length();
67
    while (dptr-- > from)
68
      *to++ = *dptr;
69
    return(from + pack_length());
70
  }
71
  else
72
#endif
73
    return(Field::unpack(to, from, param_data, low_byte_first));
74
}
75
76
/*
77
  If a field has fixed length, truncate the double argument pointed to by 'nr'
78
  appropriately.
79
  Also ensure that the argument is within [-max_value; max_value] range.
80
*/
81
82
int Field_real::truncate(double *nr, double max_value)
83
{
84
  int error= 1;
85
  double res= *nr;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
86
919.2.11 by Monty Taylor
Removed C99 isnan() usage, which allows us to remove the util/math.{cc,h} workarounds. Yay for standards!
87
  if (res == numeric_limits<double>::quiet_NaN())
483.1.1 by Lee
latest code cleanup for Field_real
88
  {
89
    res= 0;
90
    set_null();
91
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
92
    goto end;
93
  }
94
95
  if (!not_fixed)
96
  {
97
    uint32_t order= field_length - dec;
98
    uint32_t step= array_elements(log_10) - 1;
99
    max_value= 1.0;
100
    for (; order > step; order-= step)
101
      max_value*= log_10[step];
102
    max_value*= log_10[order];
103
    max_value-= 1.0 / log_10[dec];
104
105
    /* Check for infinity so we don't get NaN in calculations */
919.2.9 by Monty Taylor
Replaced C99 isinf() with C++ numeric_traits<>::infinity()
106
    if (res != numeric_limits<double>::infinity())
483.1.1 by Lee
latest code cleanup for Field_real
107
    {
108
      double tmp= rint((res - floor(res)) * log_10[dec]) / log_10[dec];
109
      res= floor(res) + tmp;
110
    }
111
  }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
112
483.1.1 by Lee
latest code cleanup for Field_real
113
  if (res < -max_value)
114
  {
115
   res= -max_value;
116
   set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
117
  }
118
  else if (res > max_value)
119
  {
120
    res= max_value;
121
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
122
  }
123
  else
124
    error= 0;
125
126
end:
127
  *nr= res;
128
  return error;
129
}
130
131
132
int Field_real::store_decimal(const my_decimal *dm)
133
{
134
  double dbl;
135
  my_decimal2double(E_DEC_FATAL_ERROR, dm, &dbl);
136
  return store(dbl);
137
}
138
139
my_decimal *Field_real::val_decimal(my_decimal *decimal_value)
140
{
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
141
  ASSERT_COLUMN_MARKED_FOR_READ;
142
483.1.1 by Lee
latest code cleanup for Field_real
143
  double2my_decimal(E_DEC_FATAL_ERROR, val_real(), decimal_value);
144
  return decimal_value;
145
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
146
147
} /* namespace drizzled */