Browse Source

* fixed FloatToDecimal() for inf/nan (mantis #14143, thanks to
Sergei Gorelkin for the test program)

git-svn-id: trunk@13394 -

Jonas Maebe 16 years ago
parent
commit
55578e8226
3 changed files with 60 additions and 1 deletions
  1. 1 0
      .gitattributes
  2. 21 1
      rtl/objpas/sysutils/sysstr.inc
  3. 38 0
      tests/webtbs/tw14143.pp

+ 1 - 0
.gitattributes

@@ -9191,6 +9191,7 @@ tests/webtbs/tw1409.pp svneol=native#text/plain
 tests/webtbs/tw1412.pp svneol=native#text/plain
 tests/webtbs/tw14134.pp svneol=native#text/plain
 tests/webtbs/tw1414.pp svneol=native#text/plain
+tests/webtbs/tw14143.pp svneol=native#text/plain
 tests/webtbs/tw1416.pp svneol=native#text/plain
 tests/webtbs/tw1430.pp svneol=native#text/plain
 tests/webtbs/tw1433.pp svneol=native#text/plain

+ 21 - 1
rtl/objpas/sysutils/sysstr.inc

@@ -2306,6 +2306,7 @@ End;
 Procedure FloatToDecimal(Out Result: TFloatRec; const Value; ValueType: TFloatValue; Precision, Decimals : integer);
 var
   Buffer: String[254];  //Though str func returns only 25 chars, this might change in the future
+  InfNan: string[3];
   Error, N, L, Start, C: Integer;
   GotNonZeroBeforeDot, BeforeDot : boolean;
 begin
@@ -2329,7 +2330,26 @@ begin
     Inc(N);
   Result.Negative := (Buffer[N] = '-');
   if Result.Negative then
-    Inc(N);
+    Inc(N)
+  else if (Buffer[N] = '+') then
+    inc(N);
+  { special cases for Inf and Nan }
+  if (L>=N+2) then
+    begin
+      InfNan:=copy(Buffer,N,3);
+      if (InfNan='Inf') then
+        begin
+          Result.Digits[0]:=#0;
+          Result.Exponent:=32767;
+          exit
+        end;
+      if (InfNan='Nan') then
+        begin
+          Result.Digits[0]:=#0;
+          Result.Exponent:=-32768;
+          exit
+        end;
+    end;
   Start := N;  //Start of digits
   Result.Exponent := 0; BeforeDot := true;
   GotNonZeroBeforeDot := false;

+ 38 - 0
tests/webtbs/tw14143.pp

@@ -0,0 +1,38 @@
+program t5;
+{$ifdef fpc}{$mode objfpc}{$h+}{$endif}
+
+uses sysutils;
+
+var
+  frec: TFloatRec;
+  code: Integer;
+  
+const
+  posinf: Extended = 1.0/0.0;
+  neginf: Extended = -1.0/0.0;
+  nan: Extended = 0.0/0.0;
+
+begin
+  code := 0;
+  FloatToDecimal(frec, posinf, fvExtended, 15, 15);
+  if (frec.Exponent <> 32767) or frec.Negative or (frec.Digits[0] <> #0) then
+  begin
+    writeln('Positive infinity test failed');
+    code := code or 1;
+  end;
+  
+  FloatToDecimal(frec, neginf, fvExtended, 15, 15);
+  if (frec.Exponent <> 32767) or (not frec.Negative) or (frec.Digits[0] <> #0) then
+  begin
+    writeln('Negative infinity test failed');
+    code := code or 2;
+  end;
+  
+  FloatToDecimal(frec, nan, fvExtended, 15, 15);
+  if (frec.Exponent <> -32768) or (frec.Digits[0] <> #0) then
+  begin
+    writeln('NaN test failed');
+    code := code or 4;
+  end;
+  Halt(Code);
+end.