On Thursday, March 13, 2003, at 02:03 AM, Nathan Bubna wrote:
if people are relying on #set( $foo = 5 / 2 ) to make "$foo" == '2',
then
chances are they're doing not-very-proper-designer-ish things to begin
with.
even if it's legit stuff, it means that they should be savy enough to
repair
their templates when upgrading to 1.5 (assuming we properly warn of
this).
What would be the way to repair that? Either VTL incorporates an
integer division operator, or a double-to-integer conversion operator;
otherwise integer division would be lost without a chance to recover it.
#set( $foo = 5 / 2 ) $foo.intValue() :-)
or there's the classic Use-A-Tool (tm) response...
Fair enough, I didn't think of that.
or even better... they could move the suspect math (integer division) out
of their view layer!
Maybe I'm just not cool with penalizing integers in favor of floats because of my "philosophical background" or whatever. I regard integers as more "fundamental" than floats, but I'm prepared to cope with that.
i think the most intuitive behavior is simply to return a whole number when
the result of an operation (+,-,*, /, or %) is a whole number or a floating
point when the operation doesn't result in a whole.
I fear a slew of beginner questions by baffled users that won't grasp the do-what-I-mean behavior, but also I'm prepared to stand corrected if this pans out all right. I'm still of the opinion that no matter how hard you try, programs will never develop telepathic abilities, but maybe I'm wrong.
Let me put forth a proposal:
- Numeric constants retain their characteristic type, that is: 5 is an Integer, 5.2 is a Double.
- Addition, subtraction and multiplication of two Integers result in an Integer. Any other combination results in a Double. Division is *always* performed in floating-point (and returns a Double), even between two Integers.
- Using a numeric reference in a template *always* results in a call to toString(); without involving any special formatting whatsoever. If finer control is desired, an explicit call to intValue() and/or the use of a tool will be necessary.
Is this what you have in mind? If so, I'd like to make an addition to that:
- Two new operators, \ (integer division) and % (integer modulus) operate on Integers (possibly converted from Doubles) and return an Integer *always*.
These are not strictly necessary, just an optimization/syntactic sugaring over the following (assuming *floating-point* division):
#macro( div p q )
#set( $res = $p / $q )
$res.intValue()
#end#macro( mod p q )
#set( $tmp = $p / $q )
#set( $res = $q * ($tmp - $tmp.intValue()) )
$res.intValue()
#endBest regards, Mat�as.
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
