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