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
21
#ifdef USE_PRAGMA_IMPLEMENTATION
22
#pragma implementation // gcc: Class implementation
27
/****************************************************************************
28
double precision floating point numbers
29
****************************************************************************/
31
int Field_double::store(const char *from,uint len,CHARSET_INFO *cs)
35
double nr= my_strntod(cs,(char*) from, len, &end, &error);
36
if (error || (!len || (((uint) (end-from) != len) && table->in_use->count_cuted_fields)))
38
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
39
(error ? ER_WARN_DATA_OUT_OF_RANGE : WARN_DATA_TRUNCATED), 1);
42
Field_double::store(nr);
47
int Field_double::store(double nr)
49
int error= truncate(&nr, DBL_MAX);
51
#ifdef WORDS_BIGENDIAN
52
if (table->s->db_low_byte_first)
63
int Field_double::store(int64_t nr, bool unsigned_val)
65
return Field_double::store(unsigned_val ? uint64_t2double((uint64_t) nr) :
70
If a field has fixed length, truncate the double argument pointed to by 'nr'
72
Also ensure that the argument is within [-max_value; max_value] range.
75
int Field_real::truncate(double *nr, double max_value)
84
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
87
else if (unsigned_flag && res < 0)
90
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
96
uint order= field_length - dec;
97
uint step= array_elements(log_10) - 1;
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];
104
/* Check for infinity so we don't get NaN in calculations */
107
double tmp= rint((res - floor(res)) * log_10[dec]) / log_10[dec];
108
res= floor(res) + tmp;
112
if (res < -max_value)
115
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
117
else if (res > max_value)
120
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
131
int Field_real::store_decimal(const my_decimal *dm)
134
my_decimal2double(E_DEC_FATAL_ERROR, dm, &dbl);
138
double Field_double::val_real(void)
141
#ifdef WORDS_BIGENDIAN
142
if (table->s->db_low_byte_first)
152
int64_t Field_double::val_int(void)
156
#ifdef WORDS_BIGENDIAN
157
if (table->s->db_low_byte_first)
164
/* Check whether we fit into int64_t range */
165
if (j <= (double) INT64_MIN)
167
res= (int64_t) INT64_MIN;
170
if (j >= (double) (uint64_t) INT64_MAX)
172
res= (int64_t) INT64_MAX;
175
return (int64_t) rint(j);
179
char buf[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
180
String tmp(buf, sizeof(buf), &my_charset_latin1), *str;
181
str= val_str(&tmp, 0);
182
push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
183
ER_TRUNCATED_WRONG_VALUE,
184
ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
191
my_decimal *Field_real::val_decimal(my_decimal *decimal_value)
193
double2my_decimal(E_DEC_FATAL_ERROR, val_real(), decimal_value);
194
return decimal_value;
198
String *Field_double::val_str(String *val_buffer,
199
String *val_ptr __attribute__((unused)))
202
#ifdef WORDS_BIGENDIAN
203
if (table->s->db_low_byte_first)
211
uint to_length=max(field_length, DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE);
212
val_buffer->alloc(to_length);
213
char *to=(char*) val_buffer->ptr();
216
if (dec >= NOT_FIXED_DEC)
217
len= my_gcvt(nr, MY_GCVT_ARG_DOUBLE, to_length - 1, to, NULL);
219
len= my_fcvt(nr, dec, to, NULL);
221
val_buffer->length((uint) len);
223
prepend_zeros(val_buffer);
227
bool Field_double::send_binary(Protocol *protocol)
229
return protocol->store((double) Field_double::val_real(), dec, (String*) 0);
233
int Field_double::cmp(const uchar *a_ptr, const uchar *b_ptr)
236
#ifdef WORDS_BIGENDIAN
237
if (table->s->db_low_byte_first)
248
return (a < b) ? -1 : (a > b) ? 1 : 0;
252
#define DBL_EXP_DIG (sizeof(double)*8-DBL_MANT_DIG)
254
/* The following should work for IEEE */
256
void Field_double::sort_string(uchar *to,uint length __attribute__((unused)))
259
#ifdef WORDS_BIGENDIAN
260
if (table->s->db_low_byte_first)
267
change_double_for_sort(nr, to);
272
Save the field metadata for double fields.
274
Saves the pack length in the first byte of the field metadata array
275
at index of *metadata_ptr.
277
@param metadata_ptr First byte of field metadata
279
@returns number of bytes written to metadata_ptr
281
int Field_double::do_save_field_metadata(uchar *metadata_ptr)
283
*metadata_ptr= pack_length();
288
void Field_double::sql_type(String &res) const
290
CHARSET_INFO *cs=res.charset();
291
if (dec == NOT_FIXED_DEC)
293
res.set_ascii(STRING_WITH_LEN("double"));
297
res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
298
"double(%d,%d)",(int) field_length,dec));
300
add_zerofill_and_unsigned(res);