Janilabo

Janilabo | LFrac() / HFrac()

Oct 11th, 2012
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.47 KB | None | 0 0
  1. const            
  2.   TEST_VALUE = 12345.6789;
  3.  
  4. function LFrac(e: Extended): Extended;
  5. var
  6.   a, d: Extended;
  7.   i: Integer;
  8.   s: string;  
  9. begin
  10.   d := DecRet(e);
  11.   if (d <> 0) then
  12.   begin      
  13.     a := Abs(d);
  14.     if (a < 0.1) then
  15.       a := (a + 0.1);
  16.     s := FloatToStr(a);
  17.     try
  18.       while (('0.' + IntToStr(Round(a * Pow(10, i))) <> s) and (i < 15)) do
  19.         Inc(i);
  20.     except
  21.     end;  
  22.     if (d < 0) then  
  23.     begin
  24.       Result := StrToFloat('0.' + StringOfChar('9', i));
  25.       Result := (Result * -1);
  26.     end else
  27.       Result := StrToFloat('0.' + StringOfChar('0', (i - 1)) + '1');
  28.   end;                                                  
  29. end;
  30.  
  31. function HFrac(e: Extended): Extended;
  32. var
  33.   a, d: Extended;
  34.   i: Integer;
  35.   s: string;
  36. begin
  37.   d := DecRet(e);
  38.   if (d <> 0) then  
  39.   begin      
  40.     a := Abs(d);
  41.     if (a < 0.1) then
  42.       a := (a + 0.1);
  43.     s := FloatToStr(a);
  44.     try
  45.       while (('0.' + IntToStr(Round(a * Pow(10, i))) <> s) and (i < 15)) do
  46.         Inc(i);
  47.     except
  48.     end;  
  49.     if (d < 0) then
  50.     begin
  51.       Result := StrToFloat('0.' + StringOfChar('0', (i - 1)) + '1');
  52.       Result := (Result * -1);
  53.     end else
  54.       Result := StrToFloat('0.' + StringOfChar('9', i));
  55.   end;              
  56. end;
  57.  
  58. begin      
  59.   ClearDebug;                                              
  60.   WriteLn('LFrac: ' + FloatToStr(LFrac(TEST_VALUE)));
  61.   WriteLn('HFrac: ' + FloatToStr(HFrac(TEST_VALUE)));  
  62. end.
Advertisement
Add Comment
Please, Sign In to add comment