xref: /aosp_15_r20/external/apache-xml/test/tests/contrib/xsltc/schemasoft/pi.xsl (revision 1212f9a0ffdc28482b8821715d2222bf16dc14e2)
1<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
2
3     version="1.0">
4
5<xsl:output method="text"/>
6
7
8
9<!-- Author: Bob DuCharme -->
10
11<!-- Reference:  http://www.xml.com/pub/a/2001/05/07/xsltmath.html -->
12
13<!-- Description: Compute pi. Based on Leibniz's algorithm that
14
15       pi/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11... which I did as
16
17       pi = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11...
18
19-->
20
21
22
23<xsl:variable name="iterations" select="4500"/>
24
25
26
27<xsl:template name="pi">
28
29  <!-- named template called by main template below -->
30
31  <xsl:param name="i">1</xsl:param>
32
33  <xsl:param name="piValue">0</xsl:param>
34
35
36
37  <xsl:choose>
38
39  <!-- If there are more iterations to do, add the passed
40
41       value of pi to another round of calculations. -->
42
43  <xsl:when test="$i &lt;= $iterations">
44
45    <xsl:call-template name="pi">
46
47      <xsl:with-param name="i" select="$i + 4"/>
48
49      <xsl:with-param name="piValue"
50
51           select="$piValue + (4 div $i) - (4 div ($i + 2))"/>
52
53    </xsl:call-template>
54
55  </xsl:when>
56
57
58
59  <!-- If no more iterations to do, add
60
61       computed value to result tree. -->
62
63  <xsl:otherwise>
64
65   <xsl:value-of select="$piValue"/>
66
67  </xsl:otherwise>
68
69
70
71  </xsl:choose>
72
73
74
75</xsl:template>
76
77
78
79
80
81<xsl:template match="/">
82
83  <xsl:call-template name="pi"/>
84
85</xsl:template>
86
87  <!--
88   * Licensed to the Apache Software Foundation (ASF) under one
89   * or more contributor license agreements. See the NOTICE file
90   * distributed with this work for additional information
91   * regarding copyright ownership. The ASF licenses this file
92   * to you under the Apache License, Version 2.0 (the  "License");
93   * you may not use this file except in compliance with the License.
94   * You may obtain a copy of the License at
95   *
96   *     http://www.apache.org/licenses/LICENSE-2.0
97   *
98   * Unless required by applicable law or agreed to in writing, software
99   * distributed under the License is distributed on an "AS IS" BASIS,
100   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
101   * See the License for the specific language governing permissions and
102   * limitations under the License.
103  -->
104
105</xsl:stylesheet>
106