hedgewars/uFloat.pas
changeset 5122 9b0513507ba8
parent 4976 088d40d8aba2
child 5124 84267f79879b
equal deleted inserted replaced
5121:2d34ec60992c 5122:9b0513507ba8
    17  *)
    17  *)
    18 
    18 
    19 {$INCLUDE "options.inc"}
    19 {$INCLUDE "options.inc"}
    20 
    20 
    21 unit uFloat;
    21 unit uFloat;
       
    22 (*
       
    23  * This unit provides a custom data type, hwFloat.
       
    24  *
       
    25  * hwFloat represents a floating point number - the value and operations
       
    26  * of this numbers are independent from the hardware architecture
       
    27  * the game runs on.
       
    28  *
       
    29  * This is important for calculations that affect the course of the game
       
    30  * and would lead to different results if based on a hardware dependent
       
    31  * data type.
       
    32  *
       
    33  * Note: Not all comparisons are implemented.
       
    34  *
       
    35  * Note: Below you'll find a list of hwFloat constants:
       
    36  *       E.g. _1 is an hwFloat with value 1.0, and -_0_9 is -0.9
       
    37  *       Use and extend the list if needed, rather then using hwFloat() with
       
    38  *       integer constants.
       
    39  *)
    22 interface
    40 interface
    23 
    41 
    24 {$IFDEF FPC}
    42 {$IFDEF FPC}
    25 {$IFDEF ENDIAN_LITTLE}
    43 {$IFDEF ENDIAN_LITTLE}
    26 type hwFloat = record
    44 type hwFloat = record
    36                0: (Round, Frac: Longword);
    54                0: (Round, Frac: Longword);
    37                1: (QWordValue : QWord);
    55                1: (QWordValue : QWord);
    38                end;
    56                end;
    39 {$ENDIF}
    57 {$ENDIF}
    40 
    58 
       
    59 // Returns an hwFloat that represents the value of integer parameter i
    41 function int2hwFloat (const i: LongInt) : hwFloat; inline;
    60 function int2hwFloat (const i: LongInt) : hwFloat; inline;
       
    61 
       
    62 // The implemented operators
    42 
    63 
    43 operator + (const z1, z2: hwFloat) z : hwFloat; inline;
    64 operator + (const z1, z2: hwFloat) z : hwFloat; inline;
    44 operator - (const z1, z2: hwFloat) z : hwFloat; inline;
    65 operator - (const z1, z2: hwFloat) z : hwFloat; inline;
    45 operator - (const z1: hwFloat) z : hwFloat; inline;
    66 operator - (const z1: hwFloat) z : hwFloat; inline;
    46 
    67 
    50 operator / (const z1: hwFloat; const z2: LongInt) z : hwFloat; inline;
    71 operator / (const z1: hwFloat; const z2: LongInt) z : hwFloat; inline;
    51 
    72 
    52 operator < (const z1, z2: hwFloat) b : boolean; inline;
    73 operator < (const z1, z2: hwFloat) b : boolean; inline;
    53 operator > (const z1, z2: hwFloat) b : boolean; inline;
    74 operator > (const z1, z2: hwFloat) b : boolean; inline;
    54 
    75 
    55 function cstr(const z: hwFloat): shortstring;
    76 
    56 function hwRound(const t: hwFloat): LongInt; inline;
    77 // Various functions for hwFloat (some are inlined in the resulting code for better performance)
    57 function hwAbs(const t: hwFloat): hwFloat; inline;
    78 
    58 function hwSqr(const t: hwFloat): hwFloat; inline;
    79 function cstr(const z: hwFloat): shortstring; // Returns a shortstring representations of the hwFloat.
    59 function hwSqrt(const t: hwFloat): hwFloat; inline;
    80 function hwRound(const t: hwFloat): LongInt; inline; // Does NOT really round but returns the integer representation of the hwFloat without fractional digits. (-_0_9 -> -0, _1_5 -> _1)
    60 function Distance(const dx, dy: hwFloat): hwFloat;
    81 function hwAbs(const t: hwFloat): hwFloat; inline; // Returns the value of t with positive sign.
    61 function DistanceI(const dx, dy: LongInt): hwFloat;
    82 function hwSqr(const t: hwFloat): hwFloat; inline; // Returns the square value of parameter t.
       
    83 function hwSqrt(const t: hwFloat): hwFloat; inline; // Returns the the positive square root of parameter t.
       
    84 function Distance(const dx, dy: hwFloat): hwFloat; // Returns the distance between two points in 2-dimensional space, of which the parameters are the horizontal and vertical distance.
       
    85 function DistanceI(const dx, dy: LongInt): hwFloat; // Same as above for integer parameters.
    62 function AngleSin(const Angle: Longword): hwFloat;
    86 function AngleSin(const Angle: Longword): hwFloat;
    63 function AngleCos(const Angle: Longword): hwFloat;
    87 function AngleCos(const Angle: Longword): hwFloat;
    64 function SignAs(const num, signum: hwFloat): hwFloat; inline;
    88 function SignAs(const num, signum: hwFloat): hwFloat; inline; // Returns an hwFloat with the value of parameter num and the sign of signum.
    65 function hwSign(r: hwFloat): LongInt; inline;
    89 function hwSign(r: hwFloat): LongInt; inline; // Returns an integer with value 1 and sign of parameter r.
    66 
    90 
    67 {$IFDEF FPC}
    91 {$IFDEF FPC}
    68 {$J-}
    92 {$J-}
    69 {$ENDIF}
    93 {$ENDIF}
    70 {$WARNINGS OFF}
    94 {$WARNINGS OFF}
       
    95 
       
    96 
       
    97 // some hwFloat constants
       
    98 
    71 const  _1div1024: hwFloat = (isNegative: false; QWordValue:     4194304);
    99 const  _1div1024: hwFloat = (isNegative: false; QWordValue:     4194304);
    72       _1div10000: hwFloat = (isNegative: false; QWordValue:      429496);
   100       _1div10000: hwFloat = (isNegative: false; QWordValue:      429496);
    73       _1div50000: hwFloat = (isNegative: false; QWordValue:       85899);
   101       _1div50000: hwFloat = (isNegative: false; QWordValue:       85899);
    74      _1div100000: hwFloat = (isNegative: false; QWordValue:       42950);
   102      _1div100000: hwFloat = (isNegative: false; QWordValue:       42950);
    75           _1div3: hwFloat = (isNegative: false; QWordValue:  1431655766);
   103           _1div3: hwFloat = (isNegative: false; QWordValue:  1431655766);