GteBSPrecision.cpp
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.0 (2016/06/19)
7 
8 #include <GTEnginePCH.h>
10 #include <algorithm>
11 using namespace gte;
12 
13 
14 BSPrecision::BSPrecision(bool isFloat, bool forBSNumber)
15  :
16  mForBSNumber(forBSNumber)
17 {
18  // NOTE: It is not clear what the rationale is for the C++ Standard
19  // Library to set numeric_limits<float>::min_exponent to -125
20  // instead of -126; same issue with numeric_limits<double>::min_exponent
21  // set to -1021 instead of -1022. Similarly, it is not clear why
22  // numeric_limits<float>::max_exponent is set to 128 when the maximum
23  // finite 'float' has exponent 127. The exponent 128 is used in the
24  // 'float' infinity, but treating this as 2^{128} does not seem to be
25  // consistent with the IEEE 754-2008 standard. Same issue with
26  // numeric_limits<double>::max_exponent set to 1024 rather than 1023.
27 
28  if (isFloat)
29  {
30  mNumBits = std::numeric_limits<float>::digits; // 24
32  std::numeric_limits<float>::min_exponent - mNumBits; // -149
33  mMaxExponent = std::numeric_limits<float>::max_exponent - 1; // 127
34  }
35  else
36  {
37  mNumBits = std::numeric_limits<double>::digits; // 53
39  std::numeric_limits<double>::min_exponent - mNumBits; // -1074
40  mMaxExponent = std::numeric_limits<double>::max_exponent - 1; // 1023
41  }
42 }
43 
44 BSPrecision::BSPrecision(bool isFloat, int32_t maxExponent, bool forBSNumber)
45  :
46  mForBSNumber(forBSNumber)
47 {
48  if (isFloat)
49  {
50  mNumBits = std::numeric_limits<float>::digits; // 24
52  std::numeric_limits<float>::min_exponent - mNumBits; // -149
53  mMaxExponent = maxExponent;
54  }
55  else
56  {
57  mNumBits = std::numeric_limits<double>::digits; // 53
59  std::numeric_limits<double>::min_exponent - mNumBits; // -1074
60  mMaxExponent = maxExponent;
61  }
62 }
63 
64 BSPrecision::BSPrecision(int32_t numBits, int32_t minBiasedExponent,
65  int32_t maxExponent, bool forBSNumber)
66  :
67  mNumBits(numBits),
68  mMinBiasedExponent(minBiasedExponent),
69  mMaxExponent(maxExponent),
70  mForBSNumber(forBSNumber)
71 {
72 }
73 
74 int32_t BSPrecision::GetNumWords() const
75 {
76  return mNumBits / 32 + ((mNumBits % 32) > 0 ? 1 : 0);
77 }
78 
79 int32_t BSPrecision::GetNumBits() const
80 {
81  return mNumBits;
82 }
83 
85 {
86  return mMinBiasedExponent;
87 }
88 
90 {
91  return mMinBiasedExponent + mNumBits - 1;
92 }
93 
95 {
96  return mMaxExponent;
97 }
98 
100 {
101  // BSRational operator* involves a product of numerators and a product
102  // of denominators. In worst case, the precision requirements are the
103  // same as a BSNumber operator*, so testing of mForBSNumber is not
104  // needed.
105  int32_t numBits = mNumBits + precision.mNumBits;
106  int32_t maxExponent = mMaxExponent + precision.mMaxExponent + 1;
107  int32_t minBiasedExponent =
109  return BSPrecision(numBits, minBiasedExponent, maxExponent, mForBSNumber);
110 }
111 
113 {
114  if (mForBSNumber)
115  {
116  int32_t minBiasedExponent =
117  std::min(mMinBiasedExponent, precision.mMinBiasedExponent);
118  int32_t maxExponent =
119  std::max(mMaxExponent, precision.mMaxExponent) + 1;
120  int32_t numBits = maxExponent - minBiasedExponent;
121  return BSPrecision(numBits, minBiasedExponent, maxExponent,
122  mForBSNumber);
123  }
124  else
125  {
126  // n0/d0 + n1/d1 = (n0*d1 + n1*d0) / (d0*d1)
127  BSPrecision product = operator*(precision);
128  product.mForBSNumber = true;
129  BSPrecision sum = product + product;
130  sum.mForBSNumber = false;
131  BSPrecision division = sum / product;
132  division.mForBSNumber = false;
133  return division;
134  }
135 }
136 
138 {
139  return operator+(precision);
140 }
141 
143 {
144  if (mForBSNumber)
145  {
146  return precision;
147  }
148  else
149  {
150  // Division leads to multiplication of numerators and denominators.
151  return operator*(precision);
152  }
153 }
154 
156 {
157  if (mForBSNumber)
158  {
159  return precision;
160  }
161  else
162  {
163  // Comparison leads to multiplication of numerators and denominators.
164  return operator*(precision);
165  }
166 }
167 
169 {
170  if (mForBSNumber)
171  {
172  return precision;
173  }
174  else
175  {
176  // Comparison leads to multiplication of numerators and denominators.
177  return operator*(precision);
178  }
179 }
180 
182 {
183  if (mForBSNumber)
184  {
185  return precision;
186  }
187  else
188  {
189  // Comparison leads to multiplication of numerators and denominators.
190  return operator*(precision);
191  }
192 }
193 
195 {
196  if (mForBSNumber)
197  {
198  return precision;
199  }
200  else
201  {
202  // Comparison leads to multiplication of numerators and denominators.
203  return operator*(precision);
204  }
205 }
206 
208 {
209  if (mForBSNumber)
210  {
211  return precision;
212  }
213  else
214  {
215  // Comparison leads to multiplication of numerators and denominators.
216  return operator*(precision);
217  }
218 }
219 
221 {
222  if (mForBSNumber)
223  {
224  return precision;
225  }
226  else
227  {
228  // Comparison leads to multiplication of numerators and denominators.
229  return operator*(precision);
230  }
231 }
232 
BSPrecision operator/(BSPrecision const &precision)
BSPrecision operator<(BSPrecision const &precision)
BSPrecision operator*(BSPrecision const &precision)
BSPrecision operator<=(BSPrecision const &precision)
int32_t GetMaxExponent() const
BSPrecision operator+(BSPrecision const &precision)
BSPrecision operator-(BSPrecision const &precision)
BSPrecision operator==(BSPrecision const &precision)
int32_t GetNumBits() const
int32_t GetNumWords() const
GLenum GLint GLint * precision
Definition: glcorearb.h:1920
int32_t GetMinExponent() const
BSPrecision operator>(BSPrecision const &precision)
int32_t GetMinBiasedExponent() const
int32_t mMinBiasedExponent
BSPrecision(bool isFloat, bool forBSNumber)
BSPrecision operator!=(BSPrecision const &precision)
BSPrecision operator>=(BSPrecision const &precision)


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 03:59:59