View Issue Details

IDProjectCategoryView StatusLast Update
0025121FPCCompilerpublic2018-01-04 21:52
ReporterBenito van der Zander Assigned ToJonas Maebe  
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionfixed 
Product Version2.7.1 
Fixed in Version3.1.1 
Summary0025121: float precision loss during constant evaluation
DescriptionWhen constants are evaluated during compile time, it seems to ignore precision loss due to floating point conversions
Steps To Reproduce
All of these print false:

   writeln(single(144115188075855877) = single(144115188075855872));
   writeln(single(1 / 3.0) = single(0.33333333));
   writeln(single(92233720368547758) = single(92233720000000000));


All of these print true:

var
  s1, s2: single;
begin
   s1 := 144115188075855877;
   s2 := 144115188075855872;
   writeln(s1 = s2);
   s1 := 1 / 3.0;
   s2 := 0.33333333;
   writeln(s1 = s2);
   s1 := 92233720368547758;
   s2 := 92233720000000000;
   writeln(s1 = s2);
TagsNo tags attached.
Fixed in Revision37911
FPCOldBugId
FPCTarget
Attached Files

Activities

Jonas Maebe

2013-10-27 19:15

manager   ~0071000

Changing this also means that the result of e.g. (5.0/9.0) will be truncated to single by the compiler (because 5.0 is parsed as single and 9.0 is also parsed as the single, the result is single too). That's correct, but this should be delayed until after 2.8.0 is released because that can introduce easily overlooked bugs in the RTL and elsewhere. I'll attach a patch for the bug, which will be applied after fixes_2_8 is branched.

Jonas Maebe

2013-10-27 19:17

manager  

tw25121.patch (1,161 bytes)   
diff --git a/compiler/ncon.pas b/compiler/ncon.pas
index 9676e0a..7db9cc3 100644
--- a/compiler/ncon.pas
+++ b/compiler/ncon.pas
@@ -528,6 +528,21 @@ implementation
             internalerror(2008022401);
          inherited create(realconstn);
          typedef:=def;
+         case tfloatdef(def).floattype of
+           s32real:
+             v:=single(v);
+           s64real:
+             v:=double(v);
+           s80real,
+           sc80real,
+           s64comp,
+           s64currency:
+             v:=extended(v);
+           s128real:
+             internalerror(2013102701);
+           else
+             internalerror(2013102702);
+         end;
          value_real:=v;
          value_currency:=v;
          lab_real:=nil;
diff --git a/tests/webtbs/tw25121.pp b/tests/webtbs/tw25121.pp
new file mode 100644
index 0000000..59dee8d
--- /dev/null
+++ b/tests/webtbs/tw25121.pp
@@ -0,0 +1,8 @@
+begin
+  if not(single(144115188075855877) = single(144115188075855872)) then
+    halt(1);
+  if not(single(1 / 3.0) = single(0.33333333)) then
+    halt(2);
+  if not(single(92233720368547758) = single(92233720000000000)) then
+    halt(3);
+end.
tw25121.patch (1,161 bytes)   

Issue History

Date Modified Username Field Change
2013-09-30 22:55 Benito van der Zander New Issue
2013-10-27 19:15 Jonas Maebe Note Added: 0071000
2013-10-27 19:18 Jonas Maebe File Added: tw25121.patch
2015-09-28 16:40 Jonas Maebe Assigned To => Jonas Maebe
2015-09-28 16:40 Jonas Maebe Status new => assigned
2018-01-04 21:52 Jonas Maebe Fixed in Revision => 37911
2018-01-04 21:52 Jonas Maebe Status assigned => resolved
2018-01-04 21:52 Jonas Maebe Fixed in Version => 3.1.1
2018-01-04 21:52 Jonas Maebe Resolution open => fixed