[rules-users] "==" operator does not work on float and double type

Wolfgang Laun wolfgang.laun at gmail.com
Tue Apr 21 04:38:59 EDT 2009


The long answer to your question can be found in
http://en.wikipedia.org/wiki/Floating_point.

The short answer is that machine numbers for floats and doubles are
approximations to almost all rational numbers, or, if you prefer, fractions.
This is because most numbers have infinite binary expansions (such as 1/3 in
decimal).

Even when you use "99.9" in one place (a Java program), there's no guarantee
that the double produced by the Java compiler is equal to the double
resulting from the same literal via Drools' compiler chain (in a DRL file).
Conversion between double and float is guaranteed to generate inequality for
such numbers.

For your tests you might try a fraction that can be represented accurately,
e.g., 0.5 or 0.125.

-W


2009/4/21 Zeke <xanadu860122 at gmail.com>

> Hi, All:
>   My drl file is like below:
>   package org.drools.examples;
>
>   import org.drools.examples.HelloWorldExample.Message;
>
>   rule "check deep int"
>       dialect "java"
>       when
>           m:Message( sec.fir.d == "99.9" )
>       then
>           System.out.println( "===check deep double ok===" );
>   end
>
>   I find that "==" operator does not work well on double and float type.
> This condition can not be satisfied. If I change the condition to sec.fir.d
> < "100" or sec.fir.d > "99.8", it works well. Is it a bug?
>
>   I attach my test code. And I add below jars into classpath:
>   antlr-runtime-3.0.jar
>   core-3.2.3.v_686_R32x.jar
>   drools-compiler-4.0.7.jar
>   drools-core-4.0.7.jar
>   drools-decisiontables-4.0.7.jar
>   drools-jsr94-4.0.7.jar
>   janino-2.5.10.jar
>   jsr94-1.1.jar
>   mvel-1.3.12-java1.5.0.jar
>
>   Can anyone kindly help me?
>
> Thanks!
> - Zeke
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20090421/c086d6e9/attachment.html 


More information about the rules-users mailing list