math.xsl 26 KB

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