Monday, May 17, 2010

Compare big integers in Coldfusion

Encountered a strange result in coldfusion when i was comparing big integers in coldfusion. In short you should never use the EQ operator when checking if two big integers are the same. This is because coldfusion almost always sees integers as java.lang.Double. So when comparing something like

<cfdump var="#1111111111111111112 eq 1111111111111111113#">

The result will be YES, instead of No. The solution for this strange behavior is to use the Compare function:

<cfdump var="#compare(1111111111111111112,1111111111111111113)#">

If the two big integers are the same this function will return 0 if they are not the same the function will return -1 or 1. In this case it will return -1.

More info on this topic can be found at Beware ColdFusion Floating Point Integers a blog by Barney Boisvert.

No comments: