hedgewars/uFloat.pas
changeset 8905 82b1400f4a08
parent 8838 aa2ffd427f6a
child 8915 36e1574e989d
equal deleted inserted replaced
8903:b35752efd5df 8905:82b1400f4a08
    82 function cstr(const z: hwFloat): shortstring; // Returns a shortstring representations of the hwFloat.
    82 function cstr(const z: hwFloat): shortstring; // Returns a shortstring representations of the hwFloat.
    83 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)
    83 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)
    84 function hwAbs(const t: hwFloat): hwFloat; inline; // Returns the value of t with positive sign.
    84 function hwAbs(const t: hwFloat): hwFloat; inline; // Returns the value of t with positive sign.
    85 function hwSqr(const t: hwFloat): hwFloat; inline; // Returns the square value of parameter t.
    85 function hwSqr(const t: hwFloat): hwFloat; inline; // Returns the square value of parameter t.
    86 function hwPow(const t: hwFloat; p: LongWord): hwFloat; inline; // Returns the power of the value
    86 function hwPow(const t: hwFloat; p: LongWord): hwFloat; inline; // Returns the power of the value
    87 function hwSqrt(const t: hwFloat): hwFloat; inline; // Returns the the positive square root of parameter t.
    87 function hwSqrt1(const t: hwFloat): hwFloat; inline; // Returns the the positive square root of parameter t.
       
    88 function hwSqrt(const x: hwFloat): hwFloat; inline; // Returns the the positive square root of parameter t.
    88 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.
    89 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.
    89 function DistanceI(const dx, dy: LongInt): hwFloat; // Same as above for integer parameters.
    90 function DistanceI(const dx, dy: LongInt): hwFloat; // Same as above for integer parameters.
    90 function AngleSin(const Angle: Longword): hwFloat;
    91 function AngleSin(const Angle: Longword): hwFloat;
    91 function AngleCos(const Angle: Longword): hwFloat;
    92 function AngleCos(const Angle: Longword): hwFloat;
    92 function vector2Angle(const x, y: hwFloat): LongInt;
    93 function vector2Angle(const x, y: hwFloat): LongInt;
   383     hwPow.QWordValue:= QWord(hwPow.Round) * t.Frac + QWord(hwPow.Frac) * t.Round + ((QWord(hwPow.Frac) * t.Frac) shr 32);
   384     hwPow.QWordValue:= QWord(hwPow.Round) * t.Frac + QWord(hwPow.Frac) * t.Round + ((QWord(hwPow.Frac) * t.Frac) shr 32);
   384     dec(p)
   385     dec(p)
   385     end
   386     end
   386 end;
   387 end;
   387 
   388 
   388 function hwSqrt(const t: hwFloat): hwFloat;
   389 function hwSqrt1(const t: hwFloat): hwFloat;
   389 const pwr = 8; // even value, feel free to adjust
   390 const pwr = 8; // even value, feel free to adjust
   390       rThreshold = 1 shl (pwr + 32);
   391       rThreshold = 1 shl (pwr + 32);
   391       lThreshold = 1 shl (pwr div 2 + 32);
   392       lThreshold = 1 shl (pwr div 2 + 32);
   392 var l, r: QWord;
   393 var l, r: QWord;
   393     c: hwFloat;
   394     c: hwFloat;
   394 begin
   395 begin
   395 hwSqrt.isNegative:= false;
   396 hwSqrt1.isNegative:= false;
   396 
   397 
   397 if t.Round = 0 then
   398 if t.Round = 0 then
   398     begin
   399     begin
   399     l:= t.QWordValue;
   400     l:= t.QWordValue;
   400     r:= $100000000
   401     r:= $100000000
   423         r:= c.QWordValue
   424         r:= c.QWordValue
   424     else
   425     else
   425         l:= c.QWordValue
   426         l:= c.QWordValue
   426 until r - l <= 1;
   427 until r - l <= 1;
   427 
   428 
   428 hwSqrt.QWordValue:= l
   429 hwSqrt1.QWordValue:= l
   429 end;
   430 end;
       
   431 
       
   432 function hwSqrt(const x: hwFloat): hwFloat;
       
   433 var r, t, s, q: QWord;
       
   434     i: integer;
       
   435 begin
       
   436 hwSqrt.isNegative:= false;
       
   437 
       
   438 t:= $4000000000000000;
       
   439 r:= 0;
       
   440 q:= x.QWordValue;
       
   441 
       
   442 for i:= 0 to 31 do
       
   443     begin
       
   444     s:= r + t;
       
   445     r:= r shr 1;
       
   446     if s <= q then
       
   447         begin
       
   448         dec(q, s);
       
   449         inc(r, t);
       
   450         end;
       
   451     t:= t shr 2;
       
   452     end;
       
   453 
       
   454 hwSqrt.QWordValue:= r shl 16
       
   455 end;
       
   456 
       
   457 
   430 
   458 
   431 function Distance(const dx, dy: hwFloat): hwFloat;
   459 function Distance(const dx, dy: hwFloat): hwFloat;
   432 begin
   460 begin
   433 Distance:= hwSqrt(hwSqr(dx) + hwSqr(dy))
   461 Distance:= hwSqrt(hwSqr(dx) + hwSqr(dy))
   434 end;
   462 end;