How to calculate power of a number with decimal exponent in XSLT 1.0?



I am looking for a solution to calculate power of a number with a decimal exponent in XSLT 1.0. Currently the calculation method that i use calculates power only for non-decimal exponents. This is the template that i use currently:



<xsl:call-template name="Pow">
<xsl:with-param name="Base" select="10"/>
<xsl:with-param name="Exponent" select="2"/>
<xsl:with-param name="Result" select="1"/>
</xsl:call-template>
<xsl:template name="Pow"> <!-- TODO: Handle decimal exponents -->
<xsl:param name="Base"/>
<xsl:param name="Exponent"/>
<xsl:param name="Result"/>
<xsl:choose>
<xsl:when test="$Exponent = 0">
<xsl:value-of select="1"/>
</xsl:when>
<xsl:when test="$Exponent = 1">
<xsl:value-of select="$Result * $Base"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="Pow">
<xsl:with-param name="Base" select="$Base"/>
<xsl:with-param name="Exponent" select="$Exponent - 1"/>
<xsl:with-param name="Result" select="$Result * $Base"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>


I don't necessarily need a generalized solution. I only need to find value of a number raised to the power 2.4.


I tried to break down my problem as follows:



x ^ (2.4) = (x ^ 2) * (x ^ 0.4)
= (x ^ 2) * (x ^ (2/5))
= (x ^ 2) * ((x ^ 2) ^ 1/5)


Finding square of x can be done, so my problem breaks down to calculating the "5th root" of a number.


I also thought of breaking my problem into a logarithmic equation but didn't reached anywhere with that.


I am thinking of writing a code implementing the long-divison method of calculating roots but that seems highly inefficient and non-elegant.


Can anyone suggest me a more simpler and efficient way of solving my problem? If not, then has anyone tried coding the long-divison method of calculating roots?


Thanx in advance!!


Note: this is a template that i would be using many many times in my execution so efficiency is all the more important for me.


No comments:

Post a Comment