/usr.bin/bc/bc.library

https://bitbucket.org/freebsd/freebsd-head/ · Unknown · 274 lines · 256 code · 18 blank · 0 comment · 0 complexity · 04a7c92e2a7b9bda7588644c36396f20 MD5 · raw file

  1. /* $FreeBSD$ */
  2. /* $OpenBSD: bc.library,v 1.3 2007/02/03 21:15:06 otto Exp $ */
  3. /*
  4. * Copyright (C) Caldera International Inc. 2001-2002.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code and documentation must retain the above
  11. * copyright notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. All advertising materials mentioning features or use of this software
  16. * must display the following acknowledgement:
  17. * This product includes software developed or owned by Caldera
  18. * International, Inc.
  19. * 4. Neither the name of Caldera International, Inc. nor the names of other
  20. * contributors may be used to endorse or promote products derived from
  21. * this software without specific prior written permission.
  22. *
  23. * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
  24. * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
  25. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  26. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  27. * IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
  28. * INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  29. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  32. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  33. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. /*
  37. * @(#)bc.library 5.1 (Berkeley) 4/17/91
  38. */
  39. scale = 20
  40. define e(x) {
  41. auto a, b, c, d, e, g, t, w, y, r
  42. r = ibase
  43. ibase = A
  44. t = scale
  45. scale = 0
  46. if (x > 0) scale = (0.435*x)/1
  47. scale = scale + t + length(scale + t) + 1
  48. w = 0
  49. if (x < 0) {
  50. x = -x
  51. w = 1
  52. }
  53. y = 0
  54. while (x > 2) {
  55. x = x/2
  56. y = y + 1
  57. }
  58. a = 1
  59. b = 1
  60. c = b
  61. d = 1
  62. e = 1
  63. for (a = 1; 1 == 1; a++) {
  64. b = b*x
  65. c = c*a + b
  66. d = d*a
  67. g = c/d
  68. if (g == e) {
  69. g = g/1
  70. while (y--) {
  71. g = g*g
  72. }
  73. scale = t
  74. ibase = r
  75. if (w == 1) return (1/g)
  76. return (g/1)
  77. }
  78. e = g
  79. }
  80. }
  81. define l(x) {
  82. auto a, b, c, d, e, f, g, u, s, t, r
  83. r = ibase
  84. ibase = A
  85. if (x <= 0) {
  86. a = (1 - 10^scale)
  87. ibase = r
  88. return (a)
  89. }
  90. t = scale
  91. f = 1
  92. if (x < 1) {
  93. s = scale(x)
  94. } else {
  95. s = length(x) - scale(x)
  96. }
  97. scale = 0
  98. a = (2.31*s)/1 /* estimated integer part of the answer */
  99. s = t + length(a) + 2 /* estimated length of the answer */
  100. while (x > 2) {
  101. scale=0
  102. scale = (length(x) + scale(x))/2 + 1
  103. if (scale < s) scale = s
  104. x = sqrt(x)
  105. f = f*2
  106. }
  107. while (x < .5) {
  108. scale = 0
  109. scale = scale(x)/2 + 1
  110. if (scale < s) scale = s
  111. x = sqrt(x)
  112. f = f*2
  113. }
  114. scale = 0
  115. scale = t + length(f) + length((1.05*(t+length(f))/1)) + 1
  116. u = (x - 1)/(x + 1)
  117. s = u*u
  118. scale = t + 2
  119. b = 2*f
  120. c = b
  121. d = 1
  122. e = 1
  123. for (a = 3; 1 == 1 ; a = a + 2) {
  124. b = b*s
  125. c = c*a + d*b
  126. d = d*a
  127. g = c/d
  128. if (g == e) {
  129. scale = t
  130. ibase = r
  131. return (u*c/d)
  132. }
  133. e = g
  134. }
  135. }
  136. define s(x) {
  137. auto a, b, c, s, t, y, p, n, i, r
  138. r = ibase
  139. ibase = A
  140. t = scale
  141. y = x/.7853
  142. s = t + length(y) - scale(y)
  143. if (s < t) s = t
  144. scale = s
  145. p = a(1)
  146. scale = 0
  147. if (x >= 0) n = (x/(2*p) + 1)/2
  148. if (x < 0) n = (x/(2*p) - 1)/2
  149. x = x - 4*n*p
  150. if (n % 2 != 0) x = -x
  151. scale = t + length(1.2*t) - scale(1.2*t)
  152. y = -x*x
  153. a = x
  154. b = 1
  155. s = x
  156. for (i =3 ; 1 == 1; i = i + 2) {
  157. a = a*y
  158. b = b*i*(i - 1)
  159. c = a/b
  160. if (c == 0) {
  161. scale = t
  162. ibase = r
  163. return (s/1)
  164. }
  165. s = s + c
  166. }
  167. }
  168. define c(x) {
  169. auto t, r
  170. r = ibase
  171. ibase = A
  172. t = scale
  173. scale = scale + 1
  174. x = s(x + 2*a(1))
  175. scale = t
  176. ibase = r
  177. return (x/1)
  178. }
  179. define a(x) {
  180. auto a, b, c, d, e, f, g, s, t, r
  181. if (x == 0) return(0)
  182. r = ibase
  183. ibase = A
  184. if (x == 1) {
  185. if (scale < 52) {
  186. a = .7853981633974483096156608458198757210492923498437764/1
  187. ibase = r
  188. return (a)
  189. }
  190. }
  191. t = scale
  192. f = 1
  193. while (x > .5) {
  194. scale = scale + 1
  195. x = -(1 - sqrt(1. + x*x))/x
  196. f = f*2
  197. }
  198. while (x < -.5) {
  199. scale = scale + 1
  200. x = -(1 - sqrt(1. + x*x))/x
  201. f = f*2
  202. }
  203. s = -x*x
  204. b = f
  205. c = f
  206. d = 1
  207. e = 1
  208. for (a = 3; 1 == 1; a = a + 2) {
  209. b = b*s
  210. c = c*a + d*b
  211. d = d*a
  212. g = c/d
  213. if (g == e) {
  214. ibase = r
  215. scale = t
  216. return (x*c/d)
  217. }
  218. e = g
  219. }
  220. }
  221. define j(n,x) {
  222. auto a, b, c, d, e, g, i, s, k, t, r
  223. r = ibase
  224. ibase = A
  225. t = scale
  226. k = 1.36*x + 1.16*t - n
  227. k = length(k) - scale(k)
  228. if (k > 0) scale = scale + k
  229. s = -x*x/4
  230. if (n < 0) {
  231. n = -n
  232. x = -x
  233. }
  234. a = 1
  235. c = 1
  236. for (i = 1; i <= n; i++) {
  237. a = a*x
  238. c = c*2*i
  239. }
  240. b = a
  241. d = 1
  242. e = 1
  243. for (i = 1; 1; i++) {
  244. a = a*s
  245. b = b*i*(n + i) + a
  246. c = c*i*(n + i)
  247. g = b/c
  248. if (g == e) {
  249. ibase = r
  250. scale = t
  251. return (g/1)
  252. }
  253. e = g
  254. }
  255. }
  256. /* vim: set filetype=bc shiftwidth=8 noexpandtab: */