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

Zeke xanadu860122 at gmail.com
Tue Apr 21 21:29:09 EDT 2009


Thanks for your reply, Wolfgang! But I don't think this makes sense. Yes, I
know rational numbers has precision. We can not use "==" in Java program to
decide equality. But I think when we use Drools, Drools should handle the
precision and make "==" works. We may use Drools in our Business product.
For example, say, user may set a rule like "SomeProduct.price == 99.9" which
is a common use case, right? The user may not know computer very well. He
will wonder why this rule wil not satisfy.
I think Drools can do thing in this way, given a precision scope. For
example, if the price > 99.989999999 and price < 99.900000001 then let price
== 99.9 true.

Thanks
- Zeke

2009/4/21 Wolfgang Laun <wolfgang.laun at gmail.com>

> 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
>>
>>
>
> _______________________________________________
> 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/20090422/11e789f7/attachment.html 


More information about the rules-users mailing list