A fixed point library from Trenki http://www.trenki.net/content/view/17/1/ Slightly modified so that the fixed point data structure can automatically cast itself to float or int when used in an expression, and added the functionality to cast between precisions.

Files at this revision

API Documentation at this revision

Comitter:
madcowswe
Date:
Sun Nov 27 05:10:45 2011 +0000
Parent:
0:ca79fb4a8165
Commit message:
Made it possible to cast to double

Changed in this revision

fixed_class.h Show annotated file Show diff for this revision Revisions of this file
diff -r ca79fb4a8165 -r ac99b6f474a0 fixed_class.h
--- a/fixed_class.h	Sun Nov 27 00:28:31 2011 +0000
+++ b/fixed_class.h	Sun Nov 27 05:10:45 2011 +0000
@@ -80,10 +80,11 @@
     fixed_point operator / (int32_t r) const { fixed_point x = *this; x /= r; return x;}
     operator int()         const { return intValue>>p; }
     operator float()       const { return fix2float<p>(intValue); }
+    operator double()       const { return (double)intValue / (1 << p); }
     
     //Added casting and constructing from another precision
     template <int q>
-	fixed_point(const fixed_point<q> &r) : intValue(q <= p ? r.intValue << (p - q) : r.intValue >> (q - p)) {}
+    fixed_point(const fixed_point<q> &r) : intValue(q <= p ? r.intValue << (p - q) : r.intValue >> (q - p)) {}
 };
 
 // Specializations for use with plain integers