Floating Point Arithmetic Inaccuracy in Lucee

Recently, We were facing arithmetic issue with Lucee. We are using Lucee 4.5 in production enviroment. First, let me elaborate the issue. The code was very simple (see below example). We were doing subtraction operation having fixed floating value. we were expecting result with fixed 2 decimal point but it gave 12 floating value whereas we were giving input 2 decimal point. We were not facing output issue each time. it behaves randomly. While checking with ACF(Adobe ColdFusion) it works well in each case.

See below example code:

Error when loading gists from https://gist.github.com/.
<cfset x = 21945.16 />
<cfset y = 17256.13 />
<cfset z = x – y />
<cfoutput>#z#</cfoutput>

Above code was giving output "4689.029999999999" whereas we were expecting "4689.03". We wonder that how could be possible as we were inputing 2 decimal point whereas getting more than 2 decimal point in output. Here we can set 2 decimal point output using NumberFormat function but we wonder about different behaviour than ColdFusion.

After checking with ACF, we were planning to submit a bug report about same. First we tried to find out submitted bug for same issue in Lucee server bug report page. Yes, we got entry about same issue and also find resolution.

In case of precise result, we should need to use PrecisionEvaluate() function. After using this, we got expected output. 

Error when loading gists from https://gist.github.com/.
<cfset x = 21945.16 />
<cfset y = 17256.13 />
<cfset z = PrecisionEvaluate(x – y) />
<cfoutput>#z#</cfoutput>

O/P: 4689.03

This function will give you right result in case of Lucce and ACF.