Dividing BigDecimal in rules can result in an ArithmeticException

Solution Verified - Updated -

Issue

  • Big decimals are passed into the Drools rule engine and rules will be run on those values. When the rule evaluates those values with a division it can return an exception when the division results in an infinitely recurring decimal (for example 1 / 3). Shouldn't engine in situations like this round the decimal such that an error is not thrown and the application can continue?

The example looks like following:

Data Model:

public static class BigDecimalWrapper {

        private BigDecimal first;
        private BigDecimal second;
}

Code which interacts with the engine:

BigDecimalWrapper wrapper = new BigDecimalWrapper();
wrapper.setFirst(new BigDecimal(1));
wrapper.setSecond(new BigDecimal(3));
kSession.insert(wrapper);
kSession.fireAllRules();

Rule:

rule "FindIt"
when
BigDecimalWrapper(first.divide(second).equals(null))
then
System.out.println("fired ");   
end

The code above produces:

Caused by: java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.

when 'divide' operation is executed.

Environment

  • Red Hat JBoss BRMS
    • 5
    • 6

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content