Tuesday, 16 February 2016

ColdFusion 2016: floor()

G'day:
OK, so there's a coupla other uninteresting features in ColdFusion 2016 which I'll look at. The first one is in response to a ticket John Whish raised a while back: Deprecate `int` in favour of `floor`.

SSIA, really, but not the grounds for doing so. The int() function doesn't really do what it's supposed to, as I wittered on about here: CFML: how should int() behave? int() should just return the integer part of a number (hint: it's in the name), but instead it performs a rounding exercise instead. This means with negative numbers, it gets the answer wrong.

Still: the horse has bolted and it's not like it can be fixed or dropped now. Speaking of fixing it, there is a function fix() which does what int() was supposed to do.

So what's this floor() function? It does exactly the same as int() currently does, but it's got a more sensible name when taken in conjunction with another already-existing ceiling(). The idea is that we abandon int() as being faulty, and promote floor() and ceiling() for the rounding down/up intergerising functions, and use fix() to just get the integer part. Basically: just forget about int() being a thing, and move on.

So this ain't much of an enhancement, it just does some language tidy-up.

Here, btw, is some code demonstrating what I'm on about:

<cfset numbers = [-3.6,-3.5,-3.4,-2.6,-2.5,-2.4,-0.5,0,0.4,0.5,0.6,1.5,1.6,2]>
<cfset handlers = [int,floor,ceiling,fix,round]>
<cfoutput>
<table border="1">
    <thead>
        <tr>
            <th>x</th>
            <cfloop array="#handlers#" item="handler">
                <th>#handler.getName()#</th>
            </cfloop>
        </tr>
    </thead>
    <tbody>
        <cfloop array="#numbers#" item="x">
            <tr>
                <td>#x#</td>
                <cfloop array="#handlers#" item="handler">
                    <td>#handler(x)#</td>
                </cfloop>
            </tr>
        </cfloop>
    </tbody>
</table>
</cfoutput>

Here's a rare instance of me using tags, eh? Well it's mostly output, and I needed a table to present it, so fair cop.

This outputs:

x int floor ceiling fix round
-3.6 -4 -4 -3 -3 -4
-3.5 -4 -4 -3 -3 -3
-3.4 -4 -4 -3 -3 -3
-2.6 -3 -3 -2 -2 -3
-2.5 -3 -3 -2 -2 -2
-2.4 -3 -3 -2 -2 -2
-0.5 -1 -1 0 0 0
0 0 0 0 0 0
0.4 0 0 1 0 0
0.5 0 0 1 0 1
0.6 0 0 1 0 1
1.5 1 1 2 1 2
1.6 1 1 2 1 2
2 2 2 2 2 2


All we see here is that int() (and now floor()) round to the next lowest integer, which might be a different integer from the integer part of the original number if the number is negative. ceiling() does the opposite: rounds to the next highest integer. fix() just returns the integer part of the number, and for good measure I've chucked round() in their too to show rounding to the nearest integer, which is yet another variation.

One thing Adobe have not done to finish this work is to mark int() as deprecated. That's kinda essential to this, as otherwise floor() is just adding clutter to the language, rather than being a step to tidy it up. I'll remind Adobe about this bit. Again.

So not very interesting, but a quick one to write up whilst waiting on a call.

Righto.

--
Adam