Drizzled Public API Documentation

floor.cc

00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 #include <config.h>
00021 #include <math.h>
00022 #include "floor.h"
00023 
00024 #include <drizzled/type/decimal.h>
00025 
00026 namespace drizzled
00027 {
00028 
00029 int64_t Item_func_floor::int_op()
00030 {
00031   int64_t result;
00032   switch (args[0]->result_type()) {
00033   case INT_RESULT:
00034     result= args[0]->val_int();
00035     null_value= args[0]->null_value;
00036     break;
00037   case DECIMAL_RESULT:
00038   {
00039     type::Decimal dec_buf, *dec;
00040     if ((dec= Item_func_floor::decimal_op(&dec_buf)))
00041       dec->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
00042     else
00043       result= 0;
00044     break;
00045   }
00046   default:
00047     result= (int64_t)Item_func_floor::real_op();
00048   };
00049   return result;
00050 }
00051 
00052 double Item_func_floor::real_op()
00053 {
00054   /*
00055     the volatile's for BUG #3051 to calm optimizer down (because of gcc's
00056     bug)
00057   */
00058   volatile double value= args[0]->val_real();
00059   null_value= args[0]->null_value;
00060   return floor(value);
00061 }
00062 
00063 
00064 type::Decimal *Item_func_floor::decimal_op(type::Decimal *decimal_value)
00065 {
00066   type::Decimal val, *value= args[0]->val_decimal(&val);
00067   if (!(null_value= (args[0]->null_value ||
00068                      class_decimal_floor(E_DEC_FATAL_ERROR, value,
00069                                       decimal_value) > 1)))
00070     return decimal_value;
00071   return 0;
00072 }
00073 
00074 } /* namespace drizzled */