"fixed" Rational::ToDouble().
authorDavid Gow <david@ingeniumdigital.com>
Thu, 7 Aug 2014 14:30:37 +0000 (22:30 +0800)
committerDavid Gow <david@ingeniumdigital.com>
Thu, 7 Aug 2014 14:30:37 +0000 (22:30 +0800)
Previously if the numerator or denominator was > the range of a double,
Rational.ToDouble() would return NaN. We now check for this case, and
discard precision accordingly.

src/rational.h

index bbd4c30..b019505 100644 (file)
@@ -10,6 +10,8 @@
 #include <cassert>
 #include "arbint.h"
 #include "gmpint.h"
+#include <climits>
+#include <values.h>
 
 namespace IPDF
 {
@@ -171,7 +173,13 @@ struct Rational
 
        double ToDouble() const 
        {
-               return (double)P/(double)Q;
+               T num = P, denom = Q;
+               while (Tabs(num) > T(DBL_MAX))
+               {
+                       num /= T(16);
+                       denom /= T(16);
+               }
+               return ((double)(num))/((double)(denom));
        }
        bool CheckAccuracy(double d, const char * msg, double threshold = 1e-3) const
        {

UCC git Repository :: git.ucc.asn.au