math.xsl 26 KB

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