4

Why does coldfusion 8 evaluate 47.0000 * 15.40 eq 723.8 as false?

<cfset test = false />
<cfset a = 47.0000 />
<cfset b = 15.40 />
<cfset c = 723.8 />

<cfif (a * b) eq c>
  <cfset test = true />
</cfif>

<cfdump "#test#">

Test is output as false.

Jason M
  • 510
  • 4
  • 11

1 Answers1

15

You can use PrecisionEvaluate() to have CF use BigDecimals to do the math.

<cfset test = false />
<cfset a = 47.0000 />
<cfset b = 15.40 />
<cfset c = 723.8 />

<cfif PrecisionEvaluate(a * b) eq c>
  <cfset test = true />
</cfif>

<cfdump var="#test#" abort="true">

This results in the expected answer of true.

Leigh
  • 28,765
  • 10
  • 55
  • 103
Busches
  • 1,964
  • 16
  • 19
  • 1
    Interesting. a * b returns 723.8000000000001, but PrecisionEvaluate(a * b) returns 723.8000000000000 – Jason M Jan 30 '13 at 15:29
  • 3
    @JasonM - Because the former returns a `java.lang.Double`. The latter returns uses (and returns) a `java.math.BigDecimal` which is more precise. Do a search on [ColdFusion floating point..](http://stackoverflow.com/search?q=[coldfusion]+floating+point+) there are tons of threads on this. – Leigh Jan 30 '13 at 15:53
  • If you're still stuck with CFMX7 or older, you can instead use createObject("java", "java.math.BigDecimal") to make use of the Java class. – leokhorn Jul 01 '14 at 08:56