Browse Source

* fixed StrToFloat for large constants

git-svn-id: trunk@1817 -
florian 19 years ago
parent
commit
446c24ed77
3 changed files with 46 additions and 17 deletions
  1. 1 0
      .gitattributes
  2. 27 17
      rtl/objpas/sysutils/sysstr.inc
  3. 18 0
      tests/webtbs/tw4520.pp

+ 1 - 0
.gitattributes

@@ -6375,6 +6375,7 @@ tests/webtbs/tw4487.pp -text svneol=unset#text/plain
 tests/webtbs/tw4489.pp -text svneol=unset#text/plain
 tests/webtbs/tw4489.pp -text svneol=unset#text/plain
 tests/webtbs/tw4496.pp svneol=native#text/plain
 tests/webtbs/tw4496.pp svneol=native#text/plain
 tests/webtbs/tw4519.pp -text svneol=unset#text/plain
 tests/webtbs/tw4519.pp -text svneol=unset#text/plain
+tests/webtbs/tw4520.pp -text svneol=unset#text/plain
 tests/webtbs/tw4529.pp -text svneol=unset#text/plain
 tests/webtbs/tw4529.pp -text svneol=unset#text/plain
 tests/webtbs/tw4537.pp svneol=native#text/plain
 tests/webtbs/tw4537.pp svneol=native#text/plain
 tests/webtbs/tw4540.pp -text svneol=unset#text/plain
 tests/webtbs/tw4540.pp -text svneol=unset#text/plain

+ 27 - 17
rtl/objpas/sysutils/sysstr.inc

@@ -1025,29 +1025,39 @@ Begin
           P := Pos('.', Result);
           P := Pos('.', Result);
           if P<>0 then
           if P<>0 then
             Result[P] := DecimalSeparator;
             Result[P] := DecimalSeparator;
-          TooLarge := P > Precision + 1;
+          TooLarge :=(P > Precision + 1) or (Pos('E', Result)<>0);
         End;
         End;
 
 
         If TooSmall Or TooLarge Then
         If TooSmall Or TooLarge Then
           begin
           begin
-          Result := FloatToStrF(Value, ffExponent, Precision, Digits);
-          // Strip unneeded zeroes.
-          P:=Pos('E',result)-1;
-          If P<>-1 then
-             While (P>1) and (Result[P]='0') do
-               begin
-               system.Delete(Result,P,1);
-               Dec(P);
-               end;
-          end
+            Result := FloatToStrF(Value, ffExponent, Precision, Digits);
+            // Strip unneeded zeroes.
+            P:=Pos('E',result)-1;
+            If P<>-1 then
+              begin
+                { delete superfluous +? }
+                if result[p+2]='+' then
+                  system.Delete(Result,P+2,1);
+                While (P>1) and (Result[P]='0') do
+                  begin
+                    system.Delete(Result,P,1);
+                    Dec(P);
+                  end;
+                If (P>0) and (Result[P]=DecimalSeparator) Then
+                  begin
+                    system.Delete(Result,P,1);
+                    Dec(P);
+                  end;
+              end;
+            end
         else if (P<>0) then // we have a decimalseparator
         else if (P<>0) then // we have a decimalseparator
           begin
           begin
-          P := Length(Result);
-          While (P>0) and (Result[P] = '0') Do
-            Dec(P);
-          If (P>0) and (Result[P]=DecimalSeparator) Then
-            Dec(P);
-          SetLength(Result, P);
+            P := Length(Result);
+            While (P>0) and (Result[P] = '0') Do
+              Dec(P);
+            If (P>0) and (Result[P]=DecimalSeparator) Then
+              Dec(P);
+            SetLength(Result, P);
           end;
           end;
       End;
       End;
 
 

+ 18 - 0
tests/webtbs/tw4520.pp

@@ -0,0 +1,18 @@
+{ Source provided for Free Pascal Bug Report 4520 }
+{ Submitted by "Martin Schreiber" on  2005-11-17 }
+{ e-mail:  }
+program project1;
+ { $mode objfpc}{$h+}
+uses
+ sysutils;
+var
+ rea1,rea2: real;
+ str1: string;
+begin
+ rea1:= 1e100;
+ str1:= floattostr(rea1);
+ if str1<>'1E100' then
+   halt(1);
+ writeln('1: ',rea1);
+ writeln('2: ',str1);
+end.