Re: [xsl] Calculate mantissa and exponent?

Subject: Re: [xsl] Calculate mantissa and exponent?
From: David_Marston@xxxxxxxxx
Date: Fri, 10 Aug 2001 15:10:57 -0400
Edmund Mitchell writes:
>I have to calculate a numbers' mantissa and exponent.
>So if I have 123456, it should be normalized as 1,23456e+5.
>Unfortunately, I have to use only pure xsl, no extension functions
>or scripts are allowed.

I've been pondering this one. This could be tackled as a string issue
rather than a numeric one, which might be more efficient. To begin,
if $abs-input is the absolute value of the input, then
string-length(floor($abs-input))
is the number of decimal places in the whole part, and your exponent
is one less than that number. It's also the amount that you would
want to shift the decimal-separator. The various string functions
would let you pull apart the number and build the new string with no
div operations at all and no recursion.

If you want to do exactly one div or multiply to shift the
decimal-separator, you could use recursion to calculate the Nth
power of ten, or a string operation. For positive values of N,
number(concat('1',substring('000000000000000',1,$N)))
is ten to the Nth power. (Expand that string of zeroes if you know
that your XSLT processor can handle more.)

If you knew that your input numbers were limited to a few orders of
magnitude in a contiguous range, you could set up an efficient
xsl:choose by depending on the fact that the first matching xsl:when
sub-element is the only one executed. Each xsl:when would perform the
exact div for a particular number of decimal places (e.g., the
four-place shift for 10000 to 99999) and concat on the exponent.

Keep in mind that format-number alone won't do the necessary shift of
the decimal-separator. You need to do your own multiply/divide or the
string equivalent.

Details are left as an exercise for the reader, just in case this is
somebody's homework assignment.    :)
.................David Marston


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread