as2js: /home/snapwebsites/snapcpp/contrib/as2js/include/as2js/float64.h Source File

float64.h
Go to the documentation of this file.
1 #ifndef AS2JS_FLOAT64_H
2 #define AS2JS_FLOAT64_H
3 /* include/as2js/float64.h
4 
5 Copyright (c) 2005-2019 Made to Order Software Corp. All Rights Reserved
6 
8 
9 Permission is hereby granted, free of charge, to any
10 person obtaining a copy of this software and
11 associated documentation files (the "Software"), to
12 deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify,
14 merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom
16 the Software is furnished to do so, subject to the
17 following conditions:
18 
19 The above copyright notice and this permission notice
20 shall be included in all copies or substantial
21 portions of the Software.
22 
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
24 ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
25 LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
26 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
27 EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
29 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 ARISING FROM, OUT OF OR IN CONNECTION WITH THE
31 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 SOFTWARE.
33 
34 */
35 
36 #include "as2js/compare.h"
37 
38 #include <limits>
39 #include <cmath>
40 
41 
42 namespace as2js
43 {
44 
45 class Float64
46 {
47 public:
48  typedef double float64_type;
49 
51  // : f_float(0.0) auto-init
52  {
53  }
54 
55  Float64(float64_type const rhs)
56  {
57  f_float = rhs;
58  }
59 
60  Float64(Float64 const& rhs)
61  {
62  f_float = rhs.f_float;
63  }
64 
66  {
67  f_float = rhs.f_float;
68  return *this;
69  }
70 
71  float64_type get() const
72  {
73  return f_float;
74  }
75 
76  void set(float64_type const new_float)
77  {
78  f_float = new_float;
79  }
80 
81  void set_NaN()
82  {
83  f_float = std::numeric_limits<Float64::float64_type>::quiet_NaN();
84  }
85 
86  void set_infinity()
87  {
88  f_float = std::numeric_limits<Float64::float64_type>::infinity();
89  }
90 
91  bool is_NaN() const
92  {
93  return std::isnan(f_float);
94  }
95 
96  bool is_infinity() const
97  {
98  return std::isinf(f_float);
99  }
100 
101  bool is_positive_infinity() const
102  {
103  return std::isinf(f_float) && !std::signbit(f_float);
104  }
105 
106  bool is_negative_infinity() const
107  {
108  return std::isinf(f_float) && std::signbit(f_float);
109  }
110 
112  {
113  // if infinity, return -1 or +1
114  // if not infinity, return 0
115  return std::isinf(f_float)
116  ? (std::signbit(f_float) ? -1 : 1)
117  : 0;
118  }
119 
120  compare_t compare(Float64 const& rhs) const
121  {
122  // if we got a NaN, it's not ordered
123  if(is_NaN() || rhs.is_NaN())
124  {
126  }
127 
128  // comparing two floats properly handles infinity
129  // (at least in g++ on Intel processors)
130 #pragma GCC diagnostic push
131 #pragma GCC diagnostic ignored "-Wfloat-equal"
135 #pragma GCC diagnostic pop
136  }
137 
138  static float64_type default_epsilon()
139  {
140  return 0.00001;
141  }
142 
143  bool nearly_equal(Float64 const& rhs, float64_type epsilon = default_epsilon())
144  {
145 #pragma GCC diagnostic push
146 #pragma GCC diagnostic ignored "-Wfloat-equal"
147  // already equal?
148  if(f_float == rhs.f_float)
149  {
150  return true;
151  }
152 
153  float64_type const diff = fabs(f_float - rhs.f_float);
154  if(f_float == 0.0
155  || rhs.f_float == 0.0
156  || diff < std::numeric_limits<float64_type>::min())
157  {
158  return diff < (epsilon * std::numeric_limits<float64_type>::min());
159  }
160 #pragma GCC diagnostic pop
161 
162  return diff / (fabs(f_float) + fabs(rhs.f_float)) < epsilon;
163  }
164 
165 
166 private:
167  float64_type f_float = 0.0;
168 };
169 
170 
171 }
172 // namespace as2js
173 #endif
174 // #ifndef AS2JS_FLOAT64_H
175 
176 // vim: ts=4 sw=4 et
float64_type f_float
Definition: float64.h:167
double float64_type
Definition: float64.h:48
Float64(float64_type const rhs)
Definition: float64.h:55
void set_NaN()
Definition: float64.h:81
static float64_type default_epsilon()
Definition: float64.h:138
Float64(Float64 const &rhs)
Definition: float64.h:60
bool nearly_equal(Float64 const &rhs, float64_type epsilon=default_epsilon())
Definition: float64.h:143
bool is_infinity() const
Definition: float64.h:96
bool is_positive_infinity() const
Definition: float64.h:101
int classified_infinity() const
Definition: float64.h:111
compare_t compare(Float64 const &rhs) const
Definition: float64.h:120
bool is_NaN() const
Definition: float64.h:91
void set_infinity()
Definition: float64.h:86
Float64 & operator=(Float64 const &rhs)
Definition: float64.h:65
bool is_negative_infinity() const
Definition: float64.h:106
The AlexScript to JavaScript namespace.
Definition: compiler.cpp:37
compare_t
Definition: compare.h:41

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.

Syndicate content

Snap! Websites
An Open Source CMS System in C++

Contact Us Directly