Jen,
This quirk is due to the difference between exact integer math and
approximate floating point math. Some imprecision is introduced when CF
converts the decimal number that you see to a binary floating point number
that the computer performs the math function on. Some decimal numbers cannot
be exactly represented by binary floating point numbers, so floating point
math often has unexpected results. If you find this idea confusing, think of
the more familiar base-10 number of 1/3. It looks exact in that
representation, but presented another way that number is actually
0.3333333333...(repeating). Infinitely repeating numbers also exist when
using base-2 binary instead of base-10, but with a different set of
fractions that cannot be represented without a loss of precision, such as
0.1. Computers cannot handle infinitely repeating numbers, so some rounding
and loss of precision occurs. You should never see the precision problem
with common integer math like incrementing loop counters.

This floating point precision behavior impacts most programming languages,
not just ColdFusion and Java. In strongly-typed platforms, such as .NET and
SQL Server, you can tell the complier that a number is a decimal and that
you want math performed on that number to be precise fixed-point math, not
imprecise floating point math. I'm not sure of any good way to pull this off
in ColdFusion. Possibly something with the javacast function would work, but
that would be messy code. I just use a rounding function.

The question of why does ColdFusion use imprecise floating point math
instead of exact fixed-point math is one I don't have the answer to without
researching it.

It is annoying to have pass all math involving decimals through a rounding
function, and it is easy to forget to do this. Does anyone know of a good
way to fix this problem on a site-wide basis?

It would be nice if ColdFusion was smart enough to know that addition and
subtraction involving base-10 numbers with only one decimal point cannot
result in a number with greater than one decimal point of precision.

#3.2 - 3.2 + 8 - 8# = 0
#3.2 + 8 - 3.2 - 8# = -8.881784197E-016
#0.6/0.2# = 3
#0.6/0.2 - 3# = -4.4408920985E-016

-Mike Chabot

On Thu, Feb 3, 2011 at 3:56 PM, Jen McVicker <snarkmeis...@gmail.com> wrote:

>
> OK, a coworker sent this over to me and I am puzzled:
>
>
>
> <cfset number1 = evaluate(12.5 * 1.1)>
>
> <cfset number2 = 12.5 * 1.1>
>
> <cfset number3 = 13.75>
>
> <cfset testEval = number3 - number1>
>
> <cfset testNoEval = number3 - number2>
>
>
>
> <cfoutput>
>
> number1 (12.5 * 1.1): <strong>#number1#</strong><br>
>
> number2 (#number2#): <strong>#number2#</strong><br>
>
> number3 (#number3#): <strong>#number3#</strong><br>
>
> #number3# - #number1#: <strong>#testEval#</strong><BR>
>
> #number3# - #number2#: <strong>#testNoEval#</strong><BR>
>
> <cfif number3 eq number1>Number3 equals number1<BR>
>
> <cfelse>Number3 does NOT equal number1<BR></cfif>
>
> <cfif number3 eq number2>Number3 equals number2<BR>
>
> <cfelse>Number3 does NOT equal number2<BR></cfif>
>
> </cfoutput>
>
>
>
> Results in:
>
>
> number1 (evaluate(12.5 * 1.1)): 13.75
> number2 (12.5 * 1.1): 13.75
> number3 (13.75): 13.75
> 13.75 - 13.75: 0
> 13.75 - 13.75: -1.7763568394E-015
> Number3 equals number1
> Number3 does NOT equal number2
>
>
>
> Obviously number2 is set to a reference of the expression rather than the
> actual value that is returned.  But since the variable outputs as 13.75,
> why
> does it make a difference?  Can someone explain it to me in small words so
> that I will understand?  ;-)
>
>
>
> Jen Perkins McVicker
> Adobe Certified ColdFusion Developer
> Email: jen.mcvic...@gmail.com
>
> Linked In: http://www.linkedin.com/in/jmcvicker
>
>
>
>
>
> 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Order the Adobe Coldfusion Anthology now!
http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion
Archive: 
http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:341857
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm

Reply via email to