feisty meow concerns codebase  2.140
double_plus.h
Go to the documentation of this file.
1 #ifndef DOUBLE_PLUS_CLASS
2 #define DOUBLE_PLUS_CLASS
3 
4 /*****************************************************************************\
5 * *
6 * Name : double_plus (an extension for double floating point numbers) *
7 * Author : Chris Koeritz *
8 * *
9 *******************************************************************************
10 * Copyright (c) 1993-$now By Author. This program is free software; you can *
11 * redistribute it and/or modify it under the terms of the GNU General Public *
12 * License as published by the Free Software Foundation; either version 2 of *
13 * the License or (at your option) any later version. This is online at: *
14 * http://www.fsf.org/copyleft/gpl.html *
15 * Please send any updates to: fred@gruntose.com *
16 \*****************************************************************************/
17 
18 #include "math_ops.h"
19 
20 #include <basis/contracts.h>
21 #include <basis/enhance_cpp.h>
22 #include <basis/functions.h>
23 
24 #include <stdio.h>//temp
25 
27 
34 namespace mathematics {
35 
37 {
38 public:
39  #define DEFAULT_DELTA 0.0001
46  double_plus(double init = 0.0, double delta = DEFAULT_DELTA) : c_value(init), c_delta(delta) {}
47 
49  double_plus(const double_plus &to_copy) : c_value(to_copy.c_value), c_delta(to_copy.c_delta) {}
50 
51  virtual ~double_plus() {}
52 
53  DEFINE_CLASS_NAME("double_plus");
54 
57  { c_value = cp.c_value; c_delta = cp.c_delta; return *this; }
58 
59  double value() const { return truncate(); }
61  operator double () const { return truncate(); }
63 
64  double delta() const { return c_delta; }
66  void delta(double new_delta) { c_delta = new_delta; }
68 
69  double truncate() const { return math_ops::round_it(c_value / c_delta) * c_delta; }
71 
73  virtual bool equal_to(const basis::equalizable &f2) const {
74  const double_plus *cast = dynamic_cast<const double_plus *>(&f2);
75  if (!cast) return false;
76  return this->d_eq(*cast);
77  }
78 
80  virtual bool less_than(const basis::orderable &f2) const
81  {
82  const double_plus *cast = dynamic_cast<const double_plus *>(&f2);
83  if (!cast) return false;
84  return !this->d_eq(*cast) && (c_value < cast->c_value);
85  }
86 
87 private:
88  double c_value;
89  double c_delta;
90 
92  bool d_eq(const double_plus &to_compare) const {
93  double diff = basis::absolute_value(c_value - to_compare.value());
94  return diff < basis::absolute_value(c_delta);
95  }
96 };
97 
98 } //namespace.
99 
100 #endif
101 
Base class for object that can tell itself apart from other instances.
Definition: contracts.h:44
A base for objects that can be alphabetically (lexicographically) ordered.
Definition: contracts.h:57
void delta(double new_delta)
modifies the precision for equality comparisons.
Definition: double_plus.h:66
double truncate() const
returns a version of the number that is chopped off past the delta after rounding.
Definition: double_plus.h:69
virtual bool equal_to(const basis::equalizable &f2) const
returns true if this equals "f2" within the "delta" precision.
Definition: double_plus.h:73
DEFINE_CLASS_NAME("double_plus")
double_plus(const double_plus &to_copy)
initializes this from "to_copy".
Definition: double_plus.h:49
double_plus(double init=0.0, double delta=DEFAULT_DELTA)
initializes using "init" as the initial value and equality within "delta".
Definition: double_plus.h:46
virtual bool less_than(const basis::orderable &f2) const
Definition: double_plus.h:80
double delta() const
observes the precision for equality comparisons.
Definition: double_plus.h:64
double value() const
observes the value held in this.
Definition: double_plus.h:59
double_plus & operator=(const double_plus &cp)
standard assignment operator.
Definition: double_plus.h:56
static int round_it(float to_round)
returns the rounded integer value for "to_round".
Definition: math_ops.h:28
#define DEFAULT_DELTA
Definition: double_plus.h:39
type absolute_value(type a)
Returns a if a is non-negative, and returns -a otherwise.
Definition: functions.h:33
An extension to floating point primitives providing approximate equality.
Definition: averager.h:21