tfloattostr.pp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. { %OPT=-Oonofastmath }
  2. { Test for FloatToStr and CurrToStr functions. }
  3. uses sysutils;
  4. const
  5. MaxCurrency : currency = 922337203685477.5807;
  6. MinCurrency : currency = -922337203685477.5807;
  7. var
  8. ErrCount: longint;
  9. procedure CheckVal(f: Extended);
  10. var
  11. s: string;
  12. f1: Extended;
  13. begin
  14. s := FloatToStr(f);
  15. f1 := StrToFloat(s);
  16. if (f<>f1) and (Abs(f-f1)/Abs(f) > 6e-15) then begin
  17. WriteLn('Error (Double):',Abs(f-f1)/Abs(f), ' Input:', f, ' Output:', s);
  18. Inc(ErrCount);
  19. end;
  20. f := Single(f);
  21. s := FloatToStr(Single(f));
  22. f1 := StrToFloat(s);
  23. if (f<>f1) and (Abs(f-f1)/Abs(f) > 6e-10) then begin
  24. WriteLn('Error (Single):',Abs(f-f1)/Abs(f), ' Input:', f, ' Output:', s);
  25. Inc(ErrCount);
  26. end;
  27. end;
  28. procedure Cycle(f: Extended);
  29. var
  30. i: Integer;
  31. begin
  32. for i := 1 to 50 do begin
  33. CheckVal(f);
  34. CheckVal(-f);
  35. f := f/10;
  36. end;
  37. end;
  38. procedure CycleInc(f, increment: Extended);
  39. var
  40. i: Integer;
  41. begin
  42. Cycle(f);
  43. for i := 0 to 30 do begin
  44. Cycle(f+increment);
  45. Cycle(f-increment);
  46. increment := increment/10;
  47. end;
  48. end;
  49. procedure CheckResult(const s, ref: string);
  50. begin
  51. if s <> ref then
  52. begin
  53. writeln('Got : ', s);
  54. writeln('Should be: ', ref);
  55. Inc(ErrCount);
  56. end;
  57. end;
  58. var
  59. e: extended;
  60. d: double;
  61. s: single;
  62. c: currency;
  63. i: Integer;
  64. tests: array [0..4] of Double = (123456789123456789., 1e20, 1.6e20, 5e20, 9e20);
  65. begin
  66. e:=1234567890123.4;
  67. d:=12345.12345;
  68. s:=12345.12;
  69. c:=12345.1234;
  70. CheckResult(FloatToStrF(e,ffExponent,15,1), '1'+DecimalSeparator+'23456789012340E+12');
  71. CheckResult(FloatToStrF(d,ffExponent,11,0), '1'+DecimalSeparator+'2345123450E+4');
  72. CheckResult(FloatToStrF(s,ffExponent,8,0), '1'+DecimalSeparator+'2345120E+4');
  73. CheckResult(FloatToStrF(s,ffExponent,8,7), '1'+DecimalSeparator+'2345120E+0004');
  74. CheckResult(FloatToStrF(e,ffExponent,8,3), '1'+DecimalSeparator+'2345679E+012');
  75. CheckResult(FloatToStrF(c,ffExponent,10,0), '1'+DecimalSeparator+'234512340E+4');
  76. CheckResult(FloatToStrF(c,ffExponent,11,2), '1'+DecimalSeparator+'2345123400E+04');
  77. CheckResult(FloatToStrF(c,ffExponent,10,4), '1'+DecimalSeparator+'234512340E+0004');
  78. CheckResult(FloatToStrF(-12345.12345,ffExponent,11,0), '-1'+DecimalSeparator+'2345123450E+4');
  79. CheckResult(FloatToStrF(-0.00000123,ffGeneral,15,0), '-1'+DecimalSeparator+'23E-6');
  80. CheckResult(FloatToStrF(-12345.12345,ffGeneral,7,0), '-12345'+DecimalSeparator+'12');
  81. CheckResult(CurrToStr(-12345.1234), '-12345'+DecimalSeparator+'1234');
  82. CheckResult(CurrToStr(MaxCurrency), '922337203685477'+DecimalSeparator+'5807');
  83. CheckResult(CurrToStr(MinCurrency), '-922337203685477'+DecimalSeparator+'5807');
  84. NegCurrFormat:=8;
  85. CheckResult(FloatToStrF(-12345.1234,ffCurrency,19,4), '-12' + ThousandSeparator + '345'+DecimalSeparator+'1234 ' + CurrencyString);
  86. CheckResult(FloatToStrF(MinCurrency,ffCurrency,19,4), '-922' + ThousandSeparator + '337' + ThousandSeparator + '203' + ThousandSeparator + '685' + ThousandSeparator + '477'+DecimalSeparator+'5807 ' + CurrencyString);
  87. for i := 0 to High(tests) do begin
  88. e := tests[i];
  89. CycleInc(e,1e20);
  90. CycleInc(e,9e20);
  91. CycleInc(e,e);
  92. CycleInc(e,e/2);
  93. CycleInc(e,e/3);
  94. end;
  95. if ErrCount > 0 then
  96. begin
  97. writeln('Test failed. Errors: ', ErrCount);
  98. Halt(1);
  99. end
  100. else
  101. writeln('Test completed.');
  102. end.