home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / math.swg / 0116_Fixed Point Math Unit.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-08-30  |  2.8 KB  |  96 lines

  1. (*
  2.  
  3.     This unit has been typed from the top of my head, so test
  4.     it first with the examples in my previous message to Eli.
  5.     As a matter of fact, this is a Unit that is based upon
  6.     my explanations to Eli, so it somewhat NEEDS it ;)
  7.  
  8.     But I tested it though, just before posting it ;)
  9.  
  10.     This unit may be published in the next SWAGs (at least I
  11.     think something like this belongs at least in the MISC SWAG
  12.     becos it doesn't have one yet (I think).)
  13.     but include the other mail too! (the previous one)
  14.  
  15.     If any questions, direct them to
  16.  
  17.     Gongo/Insecabilis
  18.        dsmits@zorro.ruca.ua.ac.be
  19.     or 2:292/8013.12 (fido)
  20. *)
  21.  
  22.     Unit FixP;
  23.  
  24.     {
  25.         (c) 1996 by Dimitri Smits aka Gongo/Insecabilis
  26.         Released to the public Domain on june 22 '96
  27.         You may use this in any production you want
  28.         just notice me of the result, always happy to
  29.         see such stuff ;) ... not needed to credit me,
  30.         just greet me (either in the documentation or
  31.         in the demo/game/softpackage ;)
  32.     }
  33.  
  34.     INTERFACE
  35.  
  36.     TYPE fp = LONGINT;
  37.         { 16.16 fixed point, signed
  38.             So all numbers -32768.0000 <= x < 32767.9999
  39.             should be sufficiently supported (although mul             and div
  40. might lose some precision, or give odd results            for large numbers
  41. (integer part)}
  42.     FUNCTION fp_add ( fp1,fp2: fp): fp;
  43.     { Not really needed, but when going to
  44.       overloading functions in C++ or so, this method is needed ;)
  45.      in other words, just here to make things complete }
  46.     { an fp3 := fp1 + fp2  is enough :) }
  47.  
  48.     FUNCTION fp_sub ( fp1,fp2: fp): fp;
  49.    { same as with fp_add, only + is - now }
  50.  
  51.     FUNCTION fp_mul ( fp1,fp2: fp): fp;
  52.     FUNCTION fp_div ( fp1,fp2: fp): fp;
  53.  
  54.     FUNCTION fp2float (fpt : fp) : REAL;
  55.     FUNCTION float2fp (fl : REAL) : fp;
  56.  
  57.     IMPLEMENTATION
  58.  
  59.     FUNCTION fp_add (fp1,fp2 : fp) : fp;
  60.         BEGIN
  61.            fp_add := fp1 + fp2;
  62.         END;
  63.  
  64.     FUNCTION fp_sub (fp1,fp2 : fp) : fp;
  65.         BEGIN
  66.            fp_sub := fp1 - fp2;
  67.         END;
  68.  
  69.     FUNCTION fp_mul (fp1,fp2 : fp) : fp;
  70.         BEGIN
  71.              IF abs(fp1) > abs(fp2) THEN
  72.                fp_mul := (fp1 SHR 8 * fp2) SHR 8
  73.              ELSE
  74.                fp_mul := (fp2 SHR 8 * fp1) SHR 8;
  75.             {16-bit precision needed, not 32 =)}
  76.         END;
  77.  
  78.     FUNCTION fp_div (fp1,fp2 :fp) : fp;
  79.         BEGIN
  80.             fp_div := (fp1 SHL 8) DIV (fp2 SHR 8);
  81.             { May lose some precision there}
  82.         END;
  83.  
  84.     FUNCTION fp2Float (fpt : fp) : REAL;
  85.         BEGIN
  86.             fp2Float := fpt / 65536;
  87.         END;
  88.  
  89.     FUNCTION float2fp (fl : REAL) : fp;
  90.         BEGIN
  91.             float2fp := ROUND (fl * 65536);
  92.         END;
  93.  
  94. BEGIN
  95. END.
  96.