1
/* - mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 MySQL
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.
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.
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
22
#include <drizzled/server_includes.h>
23
#include <drizzled/field/real.h>
26
Floating-point numbers
30
Field_real::pack(unsigned char *to, const unsigned char *from,
31
uint32_t max_length, bool low_byte_first)
33
assert(max_length >= pack_length());
34
#ifdef WORDS_BIGENDIAN
35
if (low_byte_first != table->s->db_low_byte_first)
37
const unsigned char *dptr= from + pack_length();
44
return(Field::pack(to, from, max_length, low_byte_first));
48
Field_real::unpack(unsigned char *to, const unsigned char *from,
49
uint32_t param_data, bool low_byte_first)
51
#ifdef WORDS_BIGENDIAN
52
if (low_byte_first != table->s->db_low_byte_first)
54
const unsigned char *dptr= from + pack_length();
57
return(from + pack_length());
61
return(Field::unpack(to, from, param_data, low_byte_first));
65
If a field has fixed length, truncate the double argument pointed to by 'nr'
67
Also ensure that the argument is within [-max_value; max_value] range.
70
int Field_real::truncate(double *nr, double max_value)
79
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
85
uint32_t order= field_length - dec;
86
uint32_t step= array_elements(log_10) - 1;
88
for (; order > step; order-= step)
89
max_value*= log_10[step];
90
max_value*= log_10[order];
91
max_value-= 1.0 / log_10[dec];
93
/* Check for infinity so we don't get NaN in calculations */
94
if (!(std::isinf(res)))
96
double tmp= rint((res - floor(res)) * log_10[dec]) / log_10[dec];
97
res= floor(res) + tmp;
101
if (res < -max_value)
104
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
106
else if (res > max_value)
109
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
120
int Field_real::store_decimal(const my_decimal *dm)
123
my_decimal2double(E_DEC_FATAL_ERROR, dm, &dbl);
127
my_decimal *Field_real::val_decimal(my_decimal *decimal_value)
129
double2my_decimal(E_DEC_FATAL_ERROR, val_real(), decimal_value);
130
return decimal_value;