xref: /trunk/main/filter/source/xslt/common/math.xsl (revision 07a3d7f1)
1<?xml version="1.0" encoding="UTF-8"?>
2<!--***********************************************************
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements.  See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership.  The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License.  You may obtain a copy of the License at
11 *
12 *   http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied.  See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 *
21 ***********************************************************-->
22
23
24<!--
25     xslt math lib by Wind Li
26Public Functions
27    sin(x,rounding-factor=100)
28    cos(x,rounding-factor=100)
29    tan(x,rounding-factor=100)
30    ctan(x,rounding-factor=100)
31    atan2(x, y ,rounding-factor=100)
32    atan(x,rounding-factor=100)
33    acos(x,rounding-factor=100)
34    asin(x,rounding-factor=100)
35    abs(x)
36    max(x1,x2)
37    min(x1,x2)
38    power(x,power(integer only), rounding-factor=100)
39    sqrt(x, rounding-factor=100)
40    convert2radian(x,rounding-factor=100)
41    convert2degree(x,rounding-factor=100)
42    convert2fd(x,rounding-factor=100)
43 -->
44<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:draw="http://openoffice.org/2000/drawing" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:text="http://openoffice.org/2000/text" xmlns:style="http://openoffice.org/2000/style" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:office="http://openoffice.org/2000/office" exclude-result-prefixes="draw svg style office fo text">
45    <xsl:variable name="pi" select="3.1416"/>
46    <xsl:template name="math-test">
47        sin(34.8)
48        <xsl:call-template name="sin">
49            <xsl:with-param name="x" select="34.8"/>
50            <xsl:with-param name="rounding-factor" select="100000"/>
51        </xsl:call-template>
52        cos(34.8)
53        <xsl:call-template name="cos">
54            <xsl:with-param name="x" select="34.8"/>
55            <xsl:with-param name="rounding-factor" select="100000"/>
56        </xsl:call-template>
57        atan(2.74)
58        <xsl:call-template name="atan">
59            <xsl:with-param name="x" select="2.74"/>
60            <xsl:with-param name="rounding-factor" select="100000"/>
61        </xsl:call-template>
62        acos(0.5)
63        <xsl:call-template name="acos">
64            <xsl:with-param name="x" select="0.5"/>
65            <xsl:with-param name="rounding-factor" select="100000"/>
66        </xsl:call-template>
67        asin(0.5)
68        <xsl:call-template name="asin">
69            <xsl:with-param name="x" select="0.5"/>
70            <xsl:with-param name="rounding-factor" select="100000"/>
71        </xsl:call-template>
72        sqrt(1328.3414)
73        <xsl:call-template name="sqrt">
74            <xsl:with-param name="x" select="1328.3414"/>
75            <xsl:with-param name="rounding-factor" select="100000"/>
76        </xsl:call-template>
77    </xsl:template>
78    <!-- public functions start -->
79    <xsl:template name="sin">
80        <xsl:param name="x" select="0"/>
81        <xsl:param name="rounding-factor" select="100"/>
82        <xsl:variable name="angle" select="$x * 180 div $pi "/>
83        <xsl:variable name="mod-angle" select="$angle mod 360"/>
84        <xsl:variable name="sinx">
85            <xsl:call-template name="sin-private">
86                <xsl:with-param name="x" select="  ( $angle mod 360 )  * $pi div 180 "/>
87            </xsl:call-template>
88        </xsl:variable>
89        <xsl:value-of select=" round ( number($sinx) * $rounding-factor ) div $rounding-factor"/>
90    </xsl:template>
91    <xsl:template name="cos">
92        <xsl:param name="x" select="0"/>
93        <xsl:param name="rounding-factor" select="100"/>
94        <xsl:variable name="angle" select="$x * 180 div $pi "/>
95        <xsl:variable name="mod-angle" select="$angle mod 360"/>
96        <xsl:variable name="cosx">
97            <xsl:call-template name="cos-private">
98                <xsl:with-param name="x" select="  ( $angle mod 360 )  * $pi div 180 "/>
99            </xsl:call-template>
100        </xsl:variable>
101        <xsl:value-of select=" round ( number($cosx) * $rounding-factor ) div $rounding-factor"/>
102    </xsl:template>
103    <xsl:template name="tan">
104        <xsl:param name="x" select="0"/>
105        <xsl:param name="rounding-factor" select="100"/>
106        <xsl:variable name="sinx">
107            <xsl:call-template name="sin">
108                <xsl:with-param name="x" select="$x"/>
109                <xsl:with-param name="rounding-factor" select="$rounding-factor * 10"/>
110            </xsl:call-template>
111        </xsl:variable>
112        <xsl:variable name="cosx">
113            <xsl:call-template name="cos">
114                <xsl:with-param name="x" select="$x"/>
115                <xsl:with-param name="rounding-factor" select="$rounding-factor * 10"/>
116            </xsl:call-template>
117        </xsl:variable>
118        <xsl:choose>
119            <xsl:when test=" $cosx = 0 ">
120                <xsl:message>tan error : tan(<xsl:value-of select="$x"/>) is infinite!</xsl:message>
121                <xsl:value-of select="63535"/>
122            </xsl:when>
123            <xsl:otherwise>
124                <xsl:value-of select=" round( $sinx div $cosx * $rounding-factor) div $rounding-factor"/>
125            </xsl:otherwise>
126        </xsl:choose>
127    </xsl:template>
128    <xsl:template name="ctan">
129        <xsl:param name="x" select="0"/>
130        <xsl:param name="rounding-factor" select="100"/>
131        <xsl:variable name="sinx">
132            <xsl:call-template name="sin">
133                <xsl:with-param name="x" select="$x"/>
134                <xsl:with-param name="rounding-factor" select="$rounding-factor * 10"/>
135            </xsl:call-template>
136        </xsl:variable>
137        <xsl:variable name="cosx">
138            <xsl:call-template name="cos">
139                <xsl:with-param name="x" select="$x"/>
140                <xsl:with-param name="rounding-factor" select="$rounding-factor * 10"/>
141            </xsl:call-template>
142        </xsl:variable>
143        <xsl:choose>
144            <xsl:when test=" $sinx = 0 ">
145                <xsl:message>tan error : tan(<xsl:value-of select="$x"/>) is infinite!</xsl:message>
146                <xsl:value-of select="63535"/>
147            </xsl:when>
148            <xsl:otherwise>
149                <xsl:value-of select=" round( $cosx div $sinx * $rounding-factor) div $rounding-factor"/>
150            </xsl:otherwise>
151        </xsl:choose>
152    </xsl:template>
153    <xsl:template name="atan">
154        <xsl:param name="x" select="0"/>
155        <xsl:param name="rounding-factor" select="100"/>
156        <xsl:choose>
157            <xsl:when test="$x = 0">
158                <xsl:value-of select="0"/>
159            </xsl:when>
160            <xsl:when test="$x &lt; 0">
161                <xsl:variable name="atan-x">
162                    <xsl:call-template name="atan">
163                        <xsl:with-param name="x" select=" -1 * $x"/>
164                        <xsl:with-param name="rounding-factor" select="$rounding-factor"/>
165                    </xsl:call-template>
166                </xsl:variable>
167                <xsl:value-of select="-1 * $atan-x"/>
168            </xsl:when>
169            <xsl:when test="$x &gt; 1">
170                <xsl:variable name="atan-div-x">
171                    <xsl:call-template name="atan">
172                        <xsl:with-param name="x" select="1 div $x "/>
173                        <xsl:with-param name="rounding-factor" select="$rounding-factor"/>
174                    </xsl:call-template>
175                </xsl:variable>
176                <xsl:value-of select=" $pi div 2 - $atan-div-x"/>
177            </xsl:when>
178            <xsl:otherwise>
179                <xsl:variable name="arctanx">
180                    <xsl:call-template name="atan-private">
181                        <xsl:with-param name="x" select="  $x "/>
182                    </xsl:call-template>
183                </xsl:variable>
184                <xsl:value-of select=" round ( number($arctanx) * $rounding-factor ) div $rounding-factor"/>
185            </xsl:otherwise>
186        </xsl:choose>
187    </xsl:template>
188    <xsl:template name="atan2">
189        <xsl:param name="x"/>
190        <xsl:param name="y"/>
191        <xsl:param name="rounding-factor" select="100"/>
192        <xsl:choose>
193            <xsl:when test="$x = 0">
194                <xsl:value-of select=" $pi div 2"/>
195            </xsl:when>
196            <xsl:otherwise>
197                <xsl:call-template name="atan">
198                    <xsl:with-param name="x" select="$y div $x"/>
199                    <xsl:with-param name="rounding-factor" select="$rounding-factor"/>
200                </xsl:call-template>
201            </xsl:otherwise>
202        </xsl:choose>
203    </xsl:template>
204    <xsl:template name="acos">
205        <xsl:param name="x"/>
206        <xsl:param name="rounding-factor" select="100"/>
207        <xsl:variable name="abs-x">
208            <xsl:call-template name="abs">
209                <xsl:with-param name="x" select="$x"/>
210            </xsl:call-template>
211        </xsl:variable>
212        <xsl:choose>
213            <xsl:when test="$abs-x  &gt;  1">
214                <xsl:message>acos error : abs(<xsl:value-of select="$x"/>) greate then 1 !</xsl:message>
215            </xsl:when>
216            <xsl:otherwise>
217                <xsl:call-template name="atan2">
218                    <xsl:with-param name="x" select="$x"/>
219                    <xsl:with-param name="y">
220                        <xsl:call-template name="sqrt">
221                            <xsl:with-param name="x" select="1 - $x * $x"/>
222                            <xsl:with-param name="rounding-factor" select=" concat($rounding-factor,'0') "/>
223                        </xsl:call-template>
224                    </xsl:with-param>
225                </xsl:call-template>
226            </xsl:otherwise>
227        </xsl:choose>
228    </xsl:template>
229    <xsl:template name="asin">
230        <xsl:param name="x"/>
231        <xsl:param name="rounding-factor" select="100"/>
232        <xsl:variable name="abs-x">
233            <xsl:call-template name="abs">
234                <xsl:with-param name="x" select="$x"/>
235            </xsl:call-template>
236        </xsl:variable>
237        <xsl:choose>
238            <xsl:when test="$abs-x  &gt;  1">
239                <xsl:message>asin error : abs(<xsl:value-of select="$x"/>) greate then 1 !</xsl:message>
240            </xsl:when>
241            <xsl:otherwise>
242                <xsl:call-template name="atan2">
243                    <xsl:with-param name="y" select="$x"/>
244                    <xsl:with-param name="x">
245                        <xsl:call-template name="sqrt">
246                            <xsl:with-param name="x" select="1 - $x * $x"/>
247                            <xsl:with-param name="rounding-factor" select=" concat($rounding-factor,'0') "/>
248                        </xsl:call-template>
249                    </xsl:with-param>
250                </xsl:call-template>
251            </xsl:otherwise>
252        </xsl:choose>
253    </xsl:template>
254    <xsl:template name="abs">
255        <xsl:param name="x"/>
256        <xsl:choose>
257            <xsl:when test="$x &gt; 0">
258                <xsl:value-of select="$x"/>
259            </xsl:when>
260            <xsl:otherwise>
261                <xsl:value-of select="$x * -1"/>
262            </xsl:otherwise>
263        </xsl:choose>
264    </xsl:template>
265    <xsl:template name="max">
266        <xsl:param name="x1"/>
267        <xsl:param name="x2"/>
268        <xsl:choose>
269            <xsl:when test="$x1 &gt;  $x2">
270                <xsl:value-of select="$x1"/>
271            </xsl:when>
272            <xsl:otherwise>
273                <xsl:value-of select="$x2"/>
274            </xsl:otherwise>
275        </xsl:choose>
276    </xsl:template>
277    <xsl:template name="min">
278        <xsl:param name="x1"/>
279        <xsl:param name="x2"/>
280        <xsl:choose>
281            <xsl:when test="$x1 &lt;  $x2">
282                <xsl:value-of select="$x1"/>
283            </xsl:when>
284            <xsl:otherwise>
285                <xsl:value-of select="$x2"/>
286            </xsl:otherwise>
287        </xsl:choose>
288    </xsl:template>
289    <xsl:template name="power">
290        <xsl:param name="x"/>
291        <xsl:param name="y" select="1"/>
292        <xsl:param name="rounding-factor" select="100"/>
293        <!--  z is a private param -->
294        <xsl:param name="z" select="1"/>
295        <xsl:choose>
296            <xsl:when test="$y &gt; 0">
297                <xsl:call-template name="power">
298                    <xsl:with-param name="x" select="$x"/>
299                    <xsl:with-param name="y" select="$y - 1"/>
300                    <xsl:with-param name="z" select="$z * $x"/>
301                    <xsl:with-param name="rounding-factor" select="$rounding-factor"/>
302                </xsl:call-template>
303            </xsl:when>
304            <xsl:otherwise>
305                <xsl:value-of select=" round( $z * $rounding-factor) div $rounding-factor"/>
306            </xsl:otherwise>
307        </xsl:choose>
308    </xsl:template>
309    <xsl:template name="sqrt">
310        <xsl:param name="x"/>
311        <xsl:param name="rounding-factor" select="100"/>
312        <xsl:choose>
313            <xsl:when test="$x = 0">0</xsl:when>
314            <xsl:when test="$x &lt; 0">
315                <xsl:message>sqrt error : <xsl:value-of select="$x"/>  less then 0!</xsl:message>
316            </xsl:when>
317            <xsl:otherwise>
318                <xsl:call-template name="sqrt-private">
319                    <xsl:with-param name="x" select="$x"/>
320                    <xsl:with-param name="rounding-factor" select="$rounding-factor"/>
321                </xsl:call-template>
322            </xsl:otherwise>
323        </xsl:choose>
324    </xsl:template>
325    <!-- public functions end -->
326    <!--
327Private functions:
328sin-private
329cos-private
330atan-private
331sqrt-private
332integer-sqrt
333Sqrt-GetOneDigit
334-->
335    <xsl:template name="sin-private">
336        <xsl:param name="x" select="0"/>
337        <xsl:param name="n" select="0"/>
338        <xsl:param name="nx" select="1"/>
339        <xsl:param name="sign" select="1"/>
340        <xsl:param name="max-n" select="20"/>
341        <xsl:param name="sinx" select="0"/>
342        <xsl:choose>
343            <xsl:when test="not ($max-n &gt;  $n) or $nx = 0 ">
344                <xsl:value-of select="$sinx"/>
345            </xsl:when>
346            <xsl:when test="$n = 0">
347                <xsl:call-template name="sin-private">
348                    <xsl:with-param name="x" select="$x"/>
349                    <xsl:with-param name="n" select="$n + 1"/>
350                    <xsl:with-param name="sign" select="$sign *  -1"/>
351                    <xsl:with-param name="max-n" select="$max-n"/>
352                    <xsl:with-param name="nx" select="$x "/>
353                    <xsl:with-param name="sinx" select="$sinx + $x"/>
354                </xsl:call-template>
355            </xsl:when>
356            <xsl:otherwise>
357                <xsl:variable name="new-nx" select="($nx * $x * $x)  div ( 2  *  $n )  div ( 2  *  $n  + 1)  "/>
358                <xsl:call-template name="sin-private">
359                    <xsl:with-param name="x" select="$x"/>
360                    <xsl:with-param name="n" select="$n + 1"/>
361                    <xsl:with-param name="sign" select="$sign *  -1"/>
362                    <xsl:with-param name="max-n" select="$max-n"/>
363                    <xsl:with-param name="nx" select=" $new-nx "/>
364                    <xsl:with-param name="sinx" select="$sinx + $new-nx  * $sign"/>
365                </xsl:call-template>
366            </xsl:otherwise>
367        </xsl:choose>
368    </xsl:template>
369    <xsl:template name="cos-private">
370        <xsl:param name="x" select="0"/>
371        <xsl:param name="n" select="0"/>
372        <xsl:param name="nx" select="1"/>
373        <xsl:param name="sign" select="1"/>
374        <xsl:param name="max-n" select="20"/>
375        <xsl:param name="cosx" select="0"/>
376        <xsl:choose>
377            <xsl:when test="not ($max-n &gt;  $n)  or $nx = 0  ">
378                <xsl:value-of select="$cosx"/>
379            </xsl:when>
380            <xsl:when test="$n = 0">
381                <xsl:call-template name="cos-private">
382                    <xsl:with-param name="x" select="$x"/>
383                    <xsl:with-param name="n" select="$n + 1"/>
384                    <xsl:with-param name="sign" select="$sign *  -1"/>
385                    <xsl:with-param name="max-n" select="$max-n"/>
386                    <xsl:with-param name="nx" select=" 1 "/>
387                    <xsl:with-param name="cosx" select="1"/>
388                </xsl:call-template>
389            </xsl:when>
390            <xsl:otherwise>
391                <xsl:variable name="new-nx" select="($nx * $x * $x)  div ( 2  *  $n  -1 )  div ( 2  *  $n )  "/>
392                <xsl:call-template name="cos-private">
393                    <xsl:with-param name="x" select="$x"/>
394                    <xsl:with-param name="n" select="$n + 1"/>
395                    <xsl:with-param name="sign" select="$sign *  -1"/>
396                    <xsl:with-param name="max-n" select="$max-n"/>
397                    <xsl:with-param name="nx" select=" $new-nx "/>
398                    <xsl:with-param name="cosx" select="$cosx + $new-nx  * $sign"/>
399                </xsl:call-template>
400            </xsl:otherwise>
401        </xsl:choose>
402    </xsl:template>
403    <xsl:template name="atan-private">
404        <xsl:param name="x" select="0"/>
405        <xsl:param name="n" select="0"/>
406        <xsl:param name="nx" select="1"/>
407        <xsl:param name="sign" select="1"/>
408        <xsl:param name="max-n" select="40"/>
409        <xsl:param name="arctanx" select="0"/>
410        <xsl:choose>
411            <xsl:when test="not ($max-n &gt;  $n) or $nx = 0 ">
412                <xsl:value-of select="$arctanx"/>
413            </xsl:when>
414            <xsl:when test="$n = 0">
415                <xsl:call-template name="atan-private">
416                    <xsl:with-param name="x" select="$x"/>
417                    <xsl:with-param name="n" select="$n + 1"/>
418                    <xsl:with-param name="sign" select="$sign *  -1"/>
419                    <xsl:with-param name="max-n" select="$max-n"/>
420                    <xsl:with-param name="nx" select="$x "/>
421                    <xsl:with-param name="arctanx" select="$arctanx + $x"/>
422                </xsl:call-template>
423            </xsl:when>
424            <xsl:otherwise>
425                <xsl:variable name="new-nx" select=" $nx * $x * $x "/>
426                <xsl:call-template name="atan-private">
427                    <xsl:with-param name="x" select="$x"/>
428                    <xsl:with-param name="n" select="$n + 1"/>
429                    <xsl:with-param name="sign" select="$sign *  -1"/>
430                    <xsl:with-param name="max-n" select="$max-n"/>
431                    <xsl:with-param name="nx" select=" $new-nx "/>
432                    <xsl:with-param name="arctanx" select="$arctanx + $new-nx div (2 * $n +1)  * $sign"/>
433                </xsl:call-template>
434            </xsl:otherwise>
435        </xsl:choose>
436    </xsl:template>
437    <xsl:template name="sqrt-private">
438        <xsl:param name="x"/>
439        <xsl:param name="rounding-factor" select="100"/>
440        <xsl:variable name="shift" select="string-length( $rounding-factor)"/>
441        <xsl:variable name="power">
442            <xsl:call-template name="power">
443                <xsl:with-param name="x" select="100"/>
444                <xsl:with-param name="y" select="$shift"/>
445                <xsl:with-param name="rounding-factor" select="1"/>
446            </xsl:call-template>
447        </xsl:variable>
448        <xsl:variable name="integer-x" select=" round( $power * $x )"/>
449        <xsl:variable name="integer-quotient">
450            <xsl:call-template name="integer-sqrt">
451                <xsl:with-param name="x" select="$integer-x"/>
452                <xsl:with-param name="length" select=" string-length( $integer-x ) "/>
453                <xsl:with-param name="curr-pos" select=" 2 -  (round (string-length( $integer-x ) div 2 )  * 2 - string-length( $integer-x ) ) "/>
454            </xsl:call-template>
455        </xsl:variable>
456        <xsl:variable name="power-10">
457            <xsl:call-template name="power">
458                <xsl:with-param name="x" select="10"/>
459                <xsl:with-param name="y" select="$shift - 1"/>
460                <xsl:with-param name="rounding-factor" select="1"/>
461            </xsl:call-template>
462        </xsl:variable>
463        <xsl:value-of select="  round( $integer-quotient div 10) div $power-10 "/>
464    </xsl:template>
465    <xsl:template name="integer-sqrt">
466        <xsl:param name="x"/>
467        <xsl:param name="length"/>
468        <xsl:param name="curr-pos"/>
469        <xsl:param name="last-quotient" select="0"/>
470        <xsl:choose>
471            <xsl:when test="$curr-pos &gt; $length">
472                <xsl:value-of select="$last-quotient"/>
473            </xsl:when>
474            <xsl:otherwise>
475                <xsl:variable name="curr-x" select="substring( $x, 1,  $curr-pos )"/>
476                <xsl:variable name="new-quotient">
477                    <xsl:call-template name="get-one-sqrt-digit">
478                        <xsl:with-param name="x" select="$curr-x"/>
479                        <xsl:with-param name="last-quotient" select="$last-quotient"/>
480                        <xsl:with-param name="n" select="5"/>
481                        <xsl:with-param name="direct" select="0"/>
482                    </xsl:call-template>
483                </xsl:variable>
484                <xsl:call-template name="integer-sqrt">
485                    <xsl:with-param name="x" select="$x"/>
486                    <xsl:with-param name="length" select="$length"/>
487                    <xsl:with-param name="curr-pos" select="$curr-pos + 2"/>
488                    <xsl:with-param name="last-quotient" select="number($new-quotient)"/>
489                </xsl:call-template>
490            </xsl:otherwise>
491        </xsl:choose>
492    </xsl:template>
493    <xsl:template name="get-one-sqrt-digit">
494        <xsl:param name="x"/>
495        <xsl:param name="last-quotient"/>
496        <xsl:param name="n"/>
497        <xsl:param name="direct" select="1"/>
498        <xsl:variable name="quotient" select=" concat( $last-quotient, $n) "/>
499        <xsl:variable name="accumulate" select="$quotient * $quotient "/>
500        <xsl:choose>
501            <xsl:when test="$accumulate  =  $x">
502                <xsl:value-of select="concat($last-quotient , $n  )"/>
503            </xsl:when>
504            <xsl:when test="$direct = 0 and $accumulate  &lt;  $x">
505                <xsl:call-template name="get-one-sqrt-digit">
506                    <xsl:with-param name="x" select="$x"/>
507                    <xsl:with-param name="last-quotient" select="$last-quotient"/>
508                    <xsl:with-param name="n" select="$n + 1"/>
509                    <xsl:with-param name="direct" select="1"/>
510                </xsl:call-template>
511            </xsl:when>
512            <xsl:when test="$direct = 0 and $accumulate  &gt;  $x">
513                <xsl:call-template name="get-one-sqrt-digit">
514                    <xsl:with-param name="x" select="$x"/>
515                    <xsl:with-param name="last-quotient" select="$last-quotient"/>
516                    <xsl:with-param name="n" select="$n - 1"/>
517                    <xsl:with-param name="direct" select="-1"/>
518                </xsl:call-template>
519            </xsl:when>
520            <xsl:when test=" $accumulate * $direct  &lt;  $x * $direct  ">
521                <xsl:call-template name="get-one-sqrt-digit">
522                    <xsl:with-param name="x" select="$x"/>
523                    <xsl:with-param name="last-quotient" select="$last-quotient"/>
524                    <xsl:with-param name="n" select="$n+ $direct"/>
525                    <xsl:with-param name="direct" select="$direct"/>
526                </xsl:call-template>
527            </xsl:when>
528            <xsl:when test="not($n &lt; 9)  or $n = -1">
529                <xsl:value-of select="concat($last-quotient , $n - $direct) "/>
530            </xsl:when>
531            <xsl:when test="$direct = 1">
532                <xsl:value-of select="concat($last-quotient , $n - 1) "/>
533            </xsl:when>
534            <xsl:otherwise>
535                <xsl:value-of select="concat($last-quotient , $n) "/>
536            </xsl:otherwise>
537        </xsl:choose>
538    </xsl:template>
539    <xsl:template name="convert2redian">
540        <xsl:param name="x" select="'0'"/>
541        <xsl:param name="rounding-factor" select="100"/>
542        <xsl:choose>
543            <xsl:when test="contains($x,'deg')">
544                <xsl:value-of select="round($rounding-factor * number(substring-before($x, 'deg') div 180 * $pi)) div $rounding-factor"/>
545            </xsl:when>
546            <xsl:when test="contains($x,'fd')">
547                <xsl:value-of select="round($rounding-factor * number(substring-before($x, 'fd') div 180 div 65536 * $pi)) div $rounding-factor"/>
548            </xsl:when>
549            <xsl:otherwise>
550                <xsl:value-of select="round($rounding-factor * number($x) div 180 * $pi) div $rounding-factor"/>
551            </xsl:otherwise>
552        </xsl:choose>
553    </xsl:template>
554    <xsl:template name="convert2degree">
555        <xsl:param name="x" select="'0'"/>
556        <xsl:param name="rounding-factor" select="100"/>
557        <xsl:choose>
558            <xsl:when test="contains($x,'deg')">
559                <xsl:value-of select="round($rounding-factor * substring-before($x,'deg')) div $rounding-factor"/>
560            </xsl:when>
561            <xsl:when test="contains($x,'fd')">
562                <xsl:value-of select="round($rounding-factor * number(substring-before($x, 'fd')) div 65536 ) div $rounding-factor"/>
563            </xsl:when>
564            <xsl:otherwise>
565                <xsl:value-of select="round($rounding-factor * number($x) * 180 div $pi) div $rounding-factor"/>
566            </xsl:otherwise>
567        </xsl:choose>
568    </xsl:template>
569    <xsl:template name="convert2fd">
570        <xsl:param name="x" select="'0'"/>
571        <xsl:param name="rounding-factor" select="100"/>
572        <xsl:choose>
573            <xsl:when test="contains($x,'deg')">
574                <xsl:value-of select="round($rounding-factor * number(substring-before($x, 'deg') * 65535)) div $rounding-factor"/>
575            </xsl:when>
576            <xsl:when test="contains($x,'fd')">
577                <xsl:value-of select="round($rounding-factor * number(substring-before($x, 'fd'))) div $rounding-factor"/>
578            </xsl:when>
579            <xsl:otherwise>
580                <xsl:value-of select="round($rounding-factor * number($x) * 180 div $pi * 65535) div $rounding-factor"/>
581            </xsl:otherwise>
582        </xsl:choose>
583    </xsl:template>
584
585</xsl:stylesheet>
586