main.cpp
1 #include "Fixed.hpp" 2 #include <iostream> 3 4 // Helper to print raw fixed-point internals 5 static void printRaw(const Fixed &obj, const char *name) { 6 int raw = obj.getRawBits(); 7 unsigned int uraw = static_cast<unsigned int>(raw); 8 int fb = Fixed::getFractionalBits(); 9 int frac_raw = raw & ((1 << fb) - 1); 10 std::cout << name << " raw: " << raw << " (hex 0x" << std::hex << uraw 11 << std::dec << ")"; 12 std::cout << ", fractional raw: " << frac_raw << " (binary "; 13 for (int i = fb - 1; i >= 0; --i) 14 std::cout << ((frac_raw >> i) & 1); 15 std::cout << ") -> " << static_cast<float>(frac_raw) / (1 << fb) << std::endl 16 << std::endl; 17 } 18 19 int main() { 20 std::cout << "========================================" << std::endl; 21 std::cout << " Fixed Class Comprehensive Demo" << std::endl; 22 std::cout << "========================================" << std::endl 23 << std::endl; 24 25 // Constructors 26 Fixed defaultObj; 27 Fixed fromInt(10); 28 Fixed fromFloat(42.42f); 29 Fixed copyOfInt = fromInt; 30 31 std::cout << "Constructors:" << std::endl; 32 std::cout << " defaultObj : " << defaultObj << " (default)" 33 << std::endl; 34 printRaw(defaultObj, "defaultObj"); 35 36 std::cout << " fromInt (10) : " << fromInt << std::endl; 37 printRaw(fromInt, "fromInt"); 38 39 std::cout << " fromFloat (42.42) : " << fromFloat << std::endl; 40 printRaw(fromFloat, "fromFloat"); 41 42 std::cout << " copyOfInt (copy) : " << copyOfInt << std::endl; 43 printRaw(copyOfInt, "copyOfInt"); 44 45 // Assignment 46 defaultObj = fromFloat; 47 std::cout << "After assignment defaultObj = fromFloat: " << defaultObj 48 << std::endl; 49 printRaw(defaultObj, "defaultObj (after assignment)"); 50 51 // Arithmetic 52 std::cout << std::endl << "Arithmetic operations:" << std::endl; 53 Fixed sum = fromInt + fromFloat; 54 std::cout << " 10 + 42.42 = " << sum << std::endl; 55 printRaw(sum, "10 + 42.42"); 56 57 Fixed diff = fromFloat - fromInt; 58 std::cout << " 42.42 - 10 = " << diff << std::endl; 59 printRaw(diff, "42.42 - 10"); 60 61 Fixed prod = fromInt * fromFloat; 62 std::cout << " 10 * 42.42 = " << prod << std::endl; 63 printRaw(prod, "10 * 42.42"); 64 65 Fixed half(0.5f); 66 Fixed scaled = fromFloat * half; 67 std::cout << " 42.42 * 0.5 = " << scaled << std::endl; 68 printRaw(scaled, "42.42 * 0.5"); 69 70 Fixed quot = fromFloat / fromInt; 71 std::cout << " 42.42 / 10 = " << quot << std::endl; 72 printRaw(quot, "42.42 / 10"); 73 74 // toFloat and raw accessors 75 std::cout << std::endl << "Conversions & accessors:" << std::endl; 76 std::cout << " fromFloat.toFloat(): " << fromFloat.toFloat() << std::endl; 77 std::cout << " getRawBits(fromFloat): " << fromFloat.getRawBits() 78 << std::endl; 79 std::cout << " getFractionalBits(): " << Fixed::getFractionalBits() 80 << std::endl; 81 82 // Comparisons 83 std::cout << std::endl << "Comparisons:" << std::endl; 84 std::cout << " fromInt < fromFloat : " << (fromInt < fromFloat) << std::endl; 85 std::cout << " fromInt <= copyOfInt: " << (fromInt <= copyOfInt) << std::endl; 86 std::cout << " fromInt == copyOfInt: " << (fromInt == copyOfInt) << std::endl; 87 std::cout << " fromFloat != fromInt : " << (fromFloat != fromInt) 88 << std::endl; 89 90 // Increment / Decrement 91 std::cout << std::endl << "Increment / Decrement:" << std::endl; 92 Fixed incDemo(1.0f); 93 std::cout << " start: " << incDemo << std::endl; 94 std::cout << " prefix ++: " << ++incDemo << std::endl; 95 std::cout << " after prefix: " << incDemo << std::endl; 96 std::cout << " postfix ++: " << incDemo++ << std::endl; 97 std::cout << " after postfix: " << incDemo << std::endl; 98 std::cout << " prefix --: " << --incDemo << std::endl; 99 std::cout << " postfix --: " << incDemo-- << std::endl; 100 std::cout << " final: " << incDemo << std::endl; 101 printRaw(incDemo, "incDemo (final)"); 102 103 // min / max 104 std::cout << std::endl << "Min / Max helpers:" << std::endl; 105 Fixed &rmin = Fixed::min(fromInt, fromFloat); 106 const Fixed &cmin = Fixed::min(fromInt, fromFloat); 107 Fixed &rmax = Fixed::max(fromInt, fromFloat); 108 const Fixed &cmax = Fixed::max(fromInt, fromFloat); 109 std::cout << " min (non-const ref) : " << rmin << std::endl; 110 std::cout << " min (const ref) : " << cmin << std::endl; 111 std::cout << " max (non-const ref) : " << rmax << std::endl; 112 std::cout << " max (const ref) : " << cmax << std::endl; 113 114 std::cout << std::endl 115 << "========================================" << std::endl; 116 std::cout << " End of Demo" << std::endl; 117 std::cout << "========================================" << std::endl; 118 119 return 0; 120 }