Previous topic  Top  Next topic  Print this Topic
 

Important Note for Floating Point Operation

 

The <add> built-in delivers non-whole-numbered results.

Description:

"?- 1.1[_add(?X)->16.1]."        delivers X = 15.000000000000002, but

"?- 1.1[_add(?X)->15.1]."        delivers X = 14.0.

The problem with this is that it is not OntoBroker-specific but related to floating point operations in general. The error can simply be reproduced by this java code:

      public static void main(String[] args) {

              double op1 = 1.1;

              double op2 = 15.1;

              double result = op2 - op1;

              System.out.println(op2+" - "+op1+" = "+result);

              op2 = 16.1;

              result = op2 - op1;

              System.out.println(op2+" - "+op1+" = "+result);

The output will be:

15.1 - 1.1 = 14.0

16.1 - 1.1 = 15.000000000000002

The point is that you are forced to round/format your result if you use floating point numbers. You should use the round/rounddigit built-ins to round the result of the add-operation to the desired precision.