drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
field/real.cc
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 <config.h>
22#include <drizzled/field/real.h>
23#include <drizzled/error.h>
24#include <drizzled/table.h>
25
26#include <math.h>
27
28#include <limits>
29
30using namespace std;
31
32namespace drizzled {
33
34extern const double log_10[309];
35
36/*
37 Floating-point numbers
38 */
39
40unsigned char *
41Field_real::pack(unsigned char *to, const unsigned char *from,
42 uint32_t max_length, bool low_byte_first)
43{
44 assert(max_length >= pack_length());
45#ifdef WORDS_BIGENDIAN
46 if (low_byte_first != getTable()->getShare()->db_low_byte_first)
47 {
48 const unsigned char *dptr= from + pack_length();
49 while (dptr-- > from)
50 *to++ = *dptr;
51 return(to);
52 }
53 else
54#endif
55 return(Field::pack(to, from, max_length, low_byte_first));
56}
57
58const unsigned char *
59Field_real::unpack(unsigned char *to, const unsigned char *from,
60 uint32_t param_data, bool low_byte_first)
61{
62#ifdef WORDS_BIGENDIAN
63 if (low_byte_first != getTable()->getShare()->db_low_byte_first)
64 {
65 const unsigned char *dptr= from + pack_length();
66 while (dptr-- > from)
67 *to++ = *dptr;
68 return(from + pack_length());
69 }
70 else
71#endif
72 return(Field::unpack(to, from, param_data, low_byte_first));
73}
74
75/*
76 If a field has fixed length, truncate the double argument pointed to by 'nr'
77 appropriately.
78 Also ensure that the argument is within [-max_value; max_value] range.
79*/
80
81int Field_real::truncate(double *nr, double max_value)
82{
83 int error= 1;
84 double res= *nr;
85
86 if (res == numeric_limits<double>::quiet_NaN())
87 {
88 res= 0;
89 set_null();
90 set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
91 goto end;
92 }
93
94 if (!not_fixed)
95 {
96 uint32_t order= field_length - dec;
97 uint32_t step= array_elements(log_10) - 1;
98 max_value= 1.0;
99 for (; order > step; order-= step)
100 max_value*= log_10[step];
101 max_value*= log_10[order];
102 max_value-= 1.0 / log_10[dec];
103
104 /* Check for infinity so we don't get NaN in calculations */
105 if (res != numeric_limits<double>::infinity())
106 {
107 double tmp= rint((res - floor(res)) * log_10[dec]) / log_10[dec];
108 res= floor(res) + tmp;
109 }
110 }
111
112 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 if (res > max_value)
118 {
119 res= max_value;
120 set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
121 }
122 else
123 error= 0;
124
125end:
126 *nr= res;
127 return error;
128}
129
130
132{
133 double dbl;
134 class_decimal2double(E_DEC_FATAL_ERROR, dm, &dbl);
135 return store(dbl);
136}
137
139{
140 ASSERT_COLUMN_MARKED_FOR_READ;
141
142 double2_class_decimal(E_DEC_FATAL_ERROR, val_real(), decimal_value);
143 return decimal_value;
144}
145
146} /* namespace drizzled */
int store_decimal(const type::Decimal *)
type::Decimal * val_decimal(type::Decimal *) const
virtual unsigned char * pack(unsigned char *to, const unsigned char *from, uint32_t max_length, bool low_byte_first)
Definition field/real.cc:41
virtual const unsigned char * unpack(unsigned char *to, const unsigned char *from, uint32_t param_data, bool low_byte_first)
Definition field/real.cc:59
bool set_warning(DRIZZLE_ERROR::enum_warning_level, drizzled::error_t code, int cuted_increment)
Definition field.cc:1217
virtual const unsigned char * unpack(unsigned char *to, const unsigned char *from, uint32_t param_data, bool low_byte_first)
Definition field.cc:968
virtual unsigned char * pack(unsigned char *to, const unsigned char *from, uint32_t max_length, bool low_byte_first)
Definition field.cc:955
virtual uint32_t pack_length() const
Definition field.cc:707
uint32_t field_length
Definition field.h:131
TODO: Rename this file - func.h is stupid.
STL namespace.