home *** CD-ROM | disk | FTP | other *** search
/ Teach Yourself Game Programming in 21 Days / TYGAMES_R.ISO / source / day_08 / fixdemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-18  |  3.2 KB  |  132 lines

  1.  
  2. /// I N C L U D E S ///////////////////////////////////////////////////////////
  3.  
  4. #include <math.h>
  5. #include <stdio.h>
  6.  
  7. // D E F I N E S /////////////////////////////////////////////////////////////
  8.  
  9. #define FP_SHIFT          8 // number of binary decimal digits
  10. #define FP_SHIFT_2N     256 // 2^FP_SHIFT, used during conversion of floats
  11.  
  12. // S T R U C T U R E S ///////////////////////////////////////////////////////
  13.  
  14. // define our new magical fixed point data type
  15.  
  16. typedef long fixed;
  17.  
  18. // F U N C T I O N S //////////////////////////////////////////////////////////
  19.  
  20. fixed Assign_Integer(long integer)
  21. {
  22. // this function assigns a integer to a fixed point type by shifting
  23.  
  24. return((fixed)integer << FP_SHIFT);
  25.  
  26. } // end Assign_Integer
  27.  
  28. ///////////////////////////////////////////////////////////////////////////////
  29.  
  30. fixed Assign_Float(float number)
  31. {
  32. // this function assigns a floating point number to a fixed point type
  33. // by multiplication since it makes no sense to shift a floating point data type
  34.  
  35. return((fixed)(number * FP_SHIFT_2N));
  36.  
  37. } // end Assign_Float
  38.  
  39. ///////////////////////////////////////////////////////////////////////////////
  40.  
  41. fixed Mul_Fixed(fixed f1,fixed f2)
  42. {
  43. // this function mulitplies two fixed point numbers and returns the result
  44. // notice how the final result is shifted back
  45.  
  46. return((f1*f2) >> FP_SHIFT);
  47.  
  48. } // end Mul_Fixed
  49.  
  50. ///////////////////////////////////////////////////////////////////////////////
  51.  
  52. fixed Div_Fixed(fixed f1,fixed f2)
  53. {
  54. // this function divvides two fixed point numbers and returns the result
  55. // notice how the divedend is pre-shifted before the division
  56.  
  57. return((f1<<FP_SHIFT)/f2);
  58.  
  59. } // end Div_Fixed
  60.  
  61. ///////////////////////////////////////////////////////////////////////////////
  62.  
  63. fixed Add_Fixed(fixed f1,fixed f2)
  64. {
  65.  
  66. // this function adds two fixed point numbers and returns the result
  67. // notice how no shifting is necessary
  68.  
  69. return(f1+f2);
  70.  
  71. } // end Add_Fixed
  72.  
  73. ///////////////////////////////////////////////////////////////////////////////
  74.  
  75. fixed Sub_Fixed(fixed f1,fixed f2)
  76. {
  77.  
  78. // this function subtracts two fixed point numbers and returns the result
  79. // notice how no shifting is necessary
  80.  
  81. return(f1-f2);
  82.  
  83. } // end Sub_Fixed
  84.  
  85. ///////////////////////////////////////////////////////////////////////////////
  86.  
  87. void Print_Fixed(fixed f1)
  88. {
  89. // this function prints out a fixed point number, it does this by
  90. // extracting the portion to the left of the imaginary decimal and
  91. // extracting the portion to the right of the imaginary decimal point
  92.  
  93. printf("%ld.%ld",f1 >> FP_SHIFT, 100*(unsigned long)(f1 & 0x00ff)/FP_SHIFT_2N);
  94.  
  95. } // end Print_Fixed
  96.  
  97.  
  98. //M A I N //////////////////////////////////////////////////////////////////////
  99.  
  100. void main(void)
  101. {
  102.  
  103. fixed f1,f2,f3; // defines some fixed point numbers
  104.  
  105. f1 = Assign_Float((float)15.00);
  106. f2 = Assign_Float((float)233.45);
  107.  
  108. printf("\nf1:=");
  109. Print_Fixed(f1);
  110.  
  111. printf("\nf2:=");
  112. Print_Fixed(f2);
  113.  
  114. printf("\nf1+f2:=");
  115. f3 = Add_Fixed(f1,f2);
  116. Print_Fixed(f3);
  117.  
  118. printf("\nf1-f2:=");
  119. f3 = Sub_Fixed(f1,f2);
  120. Print_Fixed(f3);
  121.  
  122. printf("\nf1*f2:=");
  123. f3 = Mul_Fixed(f1,f2);
  124. Print_Fixed(f3);
  125.  
  126. printf("\nf2/f1:=");
  127. f3 = Div_Fixed(f2,f1);
  128. Print_Fixed(f3);
  129.  
  130. } // end main
  131.  
  132.