Object | +---DFixedPoint
The fixed point class implements a 'floating point number' in a long with a fixed point. The format is ..2^3+2^2+2^1+2^0+2^(-1)+2^(-2)+2^(-3)... For example if the point is set to 3 bits, the binary number 11011 equals to 3.375.
#include <stdio.h> #include "ofc/DFixedPoint.h" int main(int argc, char *argv[]) { DFixedPoint *fp1 = [DFixedPoint alloc]; DFixedPoint *fp2 = [DFixedPoint new ]; DFixedPoint *fp3 = [DFixedPoint new ]; DText *str; [fp1 init :75 :3]; // Init with value 75 and 3 bits in the point [fp2 point :8]; // Set the point for fixed point 2 [fp2 set :23]; // Set the value (using the current point) str = [fp1 toText]; // Convert to text printf("FixedPoint1 as double %f and string %s.\n", [fp1 toDouble], [str cstring]); [str free]; str = [fp2 toText]; printf("FixedPoint2 as double %f and string %s.\n", [fp2 toDouble], [str cstring]); [str free]; // Calculations [fp2 mul :fp1]; // FixedPoint2 = FixedPoint2 * FixedPoint1 [fp3 sub :fp2 :fp1]; // FixedPoint3 = FixedPoint2 - FixedPoint1 str = [fp3 toText]; printf("FixedPoint3 as double %f and string %s.\n", [fp3 toDouble], [str cstring]); [str free]; // Comparison if ([fp3 compare :fp1] == 0) printf("Fixedpoint3 is equal to fixedpoint1.\n"); else if ([fp3 compare :fp1] < 0) printf("Fixedpoint3 is smaller than fixedpoint1.\n"); else printf("Fixedpoint3 is greater than fixedpoint1.\n"); [fp1 free]; // Cleanup [fp2 free]; [fp3 free]; return 0; }