~drizzle-trunk/drizzle/development

492.3.9 by Lee
code clean move Item_func_abs, Item_func_int_exp, Item_func_ln, Item_func_log to functions directory
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2008 Sun Microsystems
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; version 2 of the License.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 */
19
20
#include <drizzled/server_includes.h>
21
#include CSTDINT_H
22
#include <drizzled/functions/log.h>
572.1.4 by Monty Taylor
Removed a bunch of unusued tests and defines from autoconf.
23
#include CMATH_H
24
25
#if defined(CMATH_NAMESPACE)
26
using namespace CMATH_NAMESPACE;
27
#endif
492.3.9 by Lee
code clean move Item_func_abs, Item_func_int_exp, Item_func_ln, Item_func_log to functions directory
28
29
/**
30
  Extended but so slower LOG function.
31
32
  We have to check if all values are > zero and first one is not one
33
  as these are the cases then result is not a number.
34
*/
35
double Item_func_log::val_real()
36
{
37
  assert(fixed == 1);
38
  double value= args[0]->val_real();
39
  if ((null_value= args[0]->null_value))
40
    return 0.0;
41
  if (value <= 0.0)
42
  {
43
    signal_divide_by_null();
44
    return 0.0;
45
  }
46
  if (arg_count == 2)
47
  {
48
    double value2= args[1]->val_real();
49
    if ((null_value= args[1]->null_value))
50
      return 0.0;
51
    if (value2 <= 0.0 || value == 1.0)
52
    {
53
      signal_divide_by_null();
54
      return 0.0;
55
    }
56
    return log(value2) / log(value);
57
  }
58
  return log(value);
59
}
60
61
double Item_func_log2::val_real()
62
{
63
  assert(fixed == 1);
64
  double value= args[0]->val_real();
65
66
  if ((null_value=args[0]->null_value))
67
    return 0.0;
68
  if (value <= 0.0)
69
  {
70
    signal_divide_by_null();
71
    return 0.0;
72
  }
73
  return log(value) / M_LN2;
74
}
75
76
double Item_func_log10::val_real()
77
{
78
  assert(fixed == 1);
79
  double value= args[0]->val_real();
80
  if ((null_value= args[0]->null_value))
81
    return 0.0;
82
  if (value <= 0.0)
83
  {
84
    signal_divide_by_null();
85
    return 0.0;
86
  }
87
  return log10(value);
88
}
89