#20 Round() function rounds in wrong 'direction'

open
nobody
None
5
2013-01-15
2005-12-20
Mikael
No

Given

<xsl:value-of select="round(1 div 2)"/>

XMLPartner returns '0' (zero) - MSXML returns 1. From
the www.zvon.org XSLT reference
(http://www.zvon.org/xxl/XSLTreference/Output/function_round.html):

"The round function returns the number that is closest
to the argument and that is an integer. If there are
two such numbers, then the one that is closest to
positive infinity is returned."

Fix: edit XpDOM.pas -

1) Add 'Math' to uses

2) Line 5189 (under 'var' section) add additional variable:

RoundMode : TFPURoundingMode;

3) Line 5580, change from this:

Result := IntToStr(Round(XpConvertNumber(sTmp,
oTmpList, oExpressionType)));

...to this

RoundMode := GetRoundMode;
SetRoundMode(rmUp);
Result := IntToStr(Round(XpConvertNumber(sTmp,
oTmpList, oExpressionType)));
SetRoundMode(RoundMode);

i.e. record current rounding method in temporary
variable, change rounding method to 'Rounds toward
positive infinity' (rmUp - see Delphi help), perform
round(), then restore pre-existing rounding method.

Same issue may apply to similar functions (ceiling
etc.) but untested at present.

Discussion

  • Mikael
    Mikael
    2005-12-21

    Logged In: YES
    user_id=1408813

    In fact, there's tacit acknowledgement in the 'Known
    Limitations' section of the ReadMe document:

    "The XPath engine does not support NaN, positive zero,
    negative zero, positive infinity, and negative infinity."