calc-bin.el 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. ;;; calc-bin.el --- binary functions for Calc
  2. ;; Copyright (C) 1990-1993, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: David Gillespie <daveg@synaptics.com>
  4. ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. ;; This file is autoloaded from calc-ext.el.
  19. (require 'calc-ext)
  20. (require 'calc-macs)
  21. ;;; Some useful numbers
  22. (defconst math-bignum-logb-digit-size
  23. (logb math-bignum-digit-size)
  24. "The logb of the size of a bignum digit.
  25. This is the largest value of B such that 2^B is less than
  26. the size of a Calc bignum digit.")
  27. (defconst math-bignum-digit-power-of-two
  28. (expt 2 (logb math-bignum-digit-size))
  29. "The largest power of 2 less than the size of a Calc bignum digit.")
  30. ;;; b-prefix binary commands.
  31. (defun calc-and (n)
  32. (interactive "P")
  33. (calc-slow-wrapper
  34. (calc-enter-result 2 "and"
  35. (append '(calcFunc-and)
  36. (calc-top-list-n 2)
  37. (and n (list (prefix-numeric-value n)))))))
  38. (defun calc-or (n)
  39. (interactive "P")
  40. (calc-slow-wrapper
  41. (calc-enter-result 2 "or"
  42. (append '(calcFunc-or)
  43. (calc-top-list-n 2)
  44. (and n (list (prefix-numeric-value n)))))))
  45. (defun calc-xor (n)
  46. (interactive "P")
  47. (calc-slow-wrapper
  48. (calc-enter-result 2 "xor"
  49. (append '(calcFunc-xor)
  50. (calc-top-list-n 2)
  51. (and n (list (prefix-numeric-value n)))))))
  52. (defun calc-diff (n)
  53. (interactive "P")
  54. (calc-slow-wrapper
  55. (calc-enter-result 2 "diff"
  56. (append '(calcFunc-diff)
  57. (calc-top-list-n 2)
  58. (and n (list (prefix-numeric-value n)))))))
  59. (defun calc-not (n)
  60. (interactive "P")
  61. (calc-slow-wrapper
  62. (calc-enter-result 1 "not"
  63. (append '(calcFunc-not)
  64. (calc-top-list-n 1)
  65. (and n (list (prefix-numeric-value n)))))))
  66. (defun calc-lshift-binary (n)
  67. (interactive "P")
  68. (calc-slow-wrapper
  69. (let ((hyp (if (calc-is-hyperbolic) 2 1)))
  70. (calc-enter-result hyp "lsh"
  71. (append '(calcFunc-lsh)
  72. (calc-top-list-n hyp)
  73. (and n (list (prefix-numeric-value n))))))))
  74. (defun calc-rshift-binary (n)
  75. (interactive "P")
  76. (calc-slow-wrapper
  77. (let ((hyp (if (calc-is-hyperbolic) 2 1)))
  78. (calc-enter-result hyp "rsh"
  79. (append '(calcFunc-rsh)
  80. (calc-top-list-n hyp)
  81. (and n (list (prefix-numeric-value n))))))))
  82. (defun calc-lshift-arith (n)
  83. (interactive "P")
  84. (calc-slow-wrapper
  85. (let ((hyp (if (calc-is-hyperbolic) 2 1)))
  86. (calc-enter-result hyp "ash"
  87. (append '(calcFunc-ash)
  88. (calc-top-list-n hyp)
  89. (and n (list (prefix-numeric-value n))))))))
  90. (defun calc-rshift-arith (n)
  91. (interactive "P")
  92. (calc-slow-wrapper
  93. (let ((hyp (if (calc-is-hyperbolic) 2 1)))
  94. (calc-enter-result hyp "rash"
  95. (append '(calcFunc-rash)
  96. (calc-top-list-n hyp)
  97. (and n (list (prefix-numeric-value n))))))))
  98. (defun calc-rotate-binary (n)
  99. (interactive "P")
  100. (calc-slow-wrapper
  101. (let ((hyp (if (calc-is-hyperbolic) 2 1)))
  102. (calc-enter-result hyp "rot"
  103. (append '(calcFunc-rot)
  104. (calc-top-list-n hyp)
  105. (and n (list (prefix-numeric-value n))))))))
  106. (defun calc-clip (n)
  107. (interactive "P")
  108. (calc-slow-wrapper
  109. (calc-enter-result 1 "clip"
  110. (append '(calcFunc-clip)
  111. (calc-top-list-n 1)
  112. (and n (list (prefix-numeric-value n)))))))
  113. (defun calc-word-size (n)
  114. (interactive "P")
  115. (calc-wrapper
  116. (or n (setq n (read-string (format "Binary word size: (default %d) "
  117. calc-word-size))))
  118. (setq n (if (stringp n)
  119. (if (equal n "")
  120. calc-word-size
  121. (if (string-match "\\`[-+]?[0-9]+\\'" n)
  122. (string-to-number n)
  123. (error "Expected an integer")))
  124. (prefix-numeric-value n)))
  125. (or (= n calc-word-size)
  126. (if (> (math-abs n) 100)
  127. (calc-change-mode 'calc-word-size n calc-leading-zeros)
  128. (calc-change-mode '(calc-word-size calc-previous-modulo)
  129. (list n (math-power-of-2 (math-abs n)))
  130. calc-leading-zeros)))
  131. (setq math-2-word-size (math-power-of-2 (math-abs n)))
  132. (setq math-half-2-word-size (math-power-of-2 (1- (math-abs n))))
  133. (calc-do-refresh)
  134. (calc-refresh-evaltos)
  135. (if (< n 0)
  136. (message "Binary word size is %d bits (two's complement)" (- n))
  137. (message "Binary word size is %d bits" n))))
  138. ;;; d-prefix mode commands.
  139. (defun calc-radix (n &optional arg)
  140. (interactive "NDisplay radix (2-36): ")
  141. (calc-wrapper
  142. (if (and (>= n 2) (<= n 36))
  143. (progn
  144. (calc-change-mode
  145. (list 'calc-number-radix 'calc-twos-complement-mode)
  146. (list n (or arg (calc-is-option))) t)
  147. ;; also change global value so minibuffer sees it
  148. (setq-default calc-number-radix calc-number-radix))
  149. (setq n calc-number-radix))
  150. (if calc-twos-complement-mode
  151. (message "Number radix is %d, two's complement mode is on." n)
  152. (message "Number radix is %d" n))))
  153. (defun calc-decimal-radix ()
  154. (interactive)
  155. (calc-radix 10))
  156. (defun calc-binary-radix (&optional arg)
  157. (interactive "P")
  158. (calc-radix 2 arg))
  159. (defun calc-octal-radix (&optional arg)
  160. (interactive "P")
  161. (calc-radix 8 arg))
  162. (defun calc-hex-radix (&optional arg)
  163. (interactive "P")
  164. (calc-radix 16 arg))
  165. (defun calc-leading-zeros (n)
  166. (interactive "P")
  167. (calc-wrapper
  168. (if (calc-change-mode 'calc-leading-zeros n t t)
  169. (message "Zero-padding integers to %d digits (assuming radix %d)"
  170. (let* ((calc-internal-prec 6))
  171. (math-compute-max-digits (math-abs calc-word-size)
  172. calc-number-radix))
  173. calc-number-radix)
  174. (message "Omitting leading zeros on integers"))))
  175. (defvar math-power-of-2-cache (list 1 2 4 8 16 32 64 128 256 512 1024))
  176. (defvar math-big-power-of-2-cache nil)
  177. (defun math-power-of-2 (n) ; [I I] [Public]
  178. (if (and (natnump n) (<= n 100))
  179. (or (nth n math-power-of-2-cache)
  180. (let* ((i (length math-power-of-2-cache))
  181. (val (nth (1- i) math-power-of-2-cache)))
  182. (while (<= i n)
  183. (setq val (math-mul val 2)
  184. math-power-of-2-cache (nconc math-power-of-2-cache
  185. (list val))
  186. i (1+ i)))
  187. val))
  188. (let ((found (assq n math-big-power-of-2-cache)))
  189. (if found
  190. (cdr found)
  191. (let ((po2 (math-ipow 2 n)))
  192. (setq math-big-power-of-2-cache
  193. (cons (cons n po2) math-big-power-of-2-cache))
  194. po2)))))
  195. (defun math-integer-log2 (n) ; [I I] [Public]
  196. (let ((i 0)
  197. (p math-power-of-2-cache)
  198. val)
  199. (while (and p (Math-natnum-lessp (setq val (car p)) n))
  200. (setq p (cdr p)
  201. i (1+ i)))
  202. (if p
  203. (and (equal val n)
  204. i)
  205. (while (Math-natnum-lessp
  206. (prog1
  207. (setq val (math-mul val 2))
  208. (setq math-power-of-2-cache (nconc math-power-of-2-cache
  209. (list val))))
  210. n)
  211. (setq i (1+ i)))
  212. (and (equal val n)
  213. i))))
  214. ;;; Bitwise operations.
  215. (defun calcFunc-and (a b &optional w) ; [I I I] [Public]
  216. (cond ((Math-messy-integerp w)
  217. (calcFunc-and a b (math-trunc w)))
  218. ((and w (not (integerp w)))
  219. (math-reject-arg w 'fixnump))
  220. ((and (integerp a) (integerp b))
  221. (math-clip (logand a b) w))
  222. ((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod))
  223. (math-binary-modulo-args 'calcFunc-and a b w))
  224. ((not (Math-num-integerp a))
  225. (math-reject-arg a 'integerp))
  226. ((not (Math-num-integerp b))
  227. (math-reject-arg b 'integerp))
  228. (t (math-clip (cons 'bigpos
  229. (math-and-bignum (math-binary-arg a w)
  230. (math-binary-arg b w)))
  231. w))))
  232. (defun math-binary-arg (a w)
  233. (if (not (Math-integerp a))
  234. (setq a (math-trunc a)))
  235. (if (Math-integer-negp a)
  236. (math-not-bignum (cdr (math-bignum-test (math-sub -1 a)))
  237. (math-abs (if w (math-trunc w) calc-word-size)))
  238. (cdr (Math-bignum-test a))))
  239. (defun math-binary-modulo-args (f a b w)
  240. (let (mod)
  241. (if (eq (car-safe a) 'mod)
  242. (progn
  243. (setq mod (nth 2 a)
  244. a (nth 1 a))
  245. (if (eq (car-safe b) 'mod)
  246. (if (equal mod (nth 2 b))
  247. (setq b (nth 1 b))
  248. (math-reject-arg b "*Inconsistent modulus"))))
  249. (setq mod (nth 2 b)
  250. b (nth 1 b)))
  251. (if (Math-messy-integerp mod)
  252. (setq mod (math-trunc mod))
  253. (or (Math-integerp mod)
  254. (math-reject-arg mod 'integerp)))
  255. (let ((bits (math-integer-log2 mod)))
  256. (if bits
  257. (if w
  258. (if (/= w bits)
  259. (calc-record-why
  260. "*Warning: Modulus inconsistent with word size"))
  261. (setq w bits))
  262. (calc-record-why "*Warning: Modulus is not a power of 2"))
  263. (math-make-mod (if b
  264. (funcall f a b w)
  265. (funcall f a w))
  266. mod))))
  267. (defun math-and-bignum (a b) ; [l l l]
  268. (and a b
  269. (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two))
  270. (qb (math-div-bignum-digit b math-bignum-digit-power-of-two)))
  271. (math-mul-bignum-digit (math-and-bignum (math-norm-bignum (car qa))
  272. (math-norm-bignum (car qb)))
  273. math-bignum-digit-power-of-two
  274. (logand (cdr qa) (cdr qb))))))
  275. (defun calcFunc-or (a b &optional w) ; [I I I] [Public]
  276. (cond ((Math-messy-integerp w)
  277. (calcFunc-or a b (math-trunc w)))
  278. ((and w (not (integerp w)))
  279. (math-reject-arg w 'fixnump))
  280. ((and (integerp a) (integerp b))
  281. (math-clip (logior a b) w))
  282. ((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod))
  283. (math-binary-modulo-args 'calcFunc-or a b w))
  284. ((not (Math-num-integerp a))
  285. (math-reject-arg a 'integerp))
  286. ((not (Math-num-integerp b))
  287. (math-reject-arg b 'integerp))
  288. (t (math-clip (cons 'bigpos
  289. (math-or-bignum (math-binary-arg a w)
  290. (math-binary-arg b w)))
  291. w))))
  292. (defun math-or-bignum (a b) ; [l l l]
  293. (and (or a b)
  294. (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two))
  295. (qb (math-div-bignum-digit b math-bignum-digit-power-of-two)))
  296. (math-mul-bignum-digit (math-or-bignum (math-norm-bignum (car qa))
  297. (math-norm-bignum (car qb)))
  298. math-bignum-digit-power-of-two
  299. (logior (cdr qa) (cdr qb))))))
  300. (defun calcFunc-xor (a b &optional w) ; [I I I] [Public]
  301. (cond ((Math-messy-integerp w)
  302. (calcFunc-xor a b (math-trunc w)))
  303. ((and w (not (integerp w)))
  304. (math-reject-arg w 'fixnump))
  305. ((and (integerp a) (integerp b))
  306. (math-clip (logxor a b) w))
  307. ((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod))
  308. (math-binary-modulo-args 'calcFunc-xor a b w))
  309. ((not (Math-num-integerp a))
  310. (math-reject-arg a 'integerp))
  311. ((not (Math-num-integerp b))
  312. (math-reject-arg b 'integerp))
  313. (t (math-clip (cons 'bigpos
  314. (math-xor-bignum (math-binary-arg a w)
  315. (math-binary-arg b w)))
  316. w))))
  317. (defun math-xor-bignum (a b) ; [l l l]
  318. (and (or a b)
  319. (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two))
  320. (qb (math-div-bignum-digit b math-bignum-digit-power-of-two)))
  321. (math-mul-bignum-digit (math-xor-bignum (math-norm-bignum (car qa))
  322. (math-norm-bignum (car qb)))
  323. math-bignum-digit-power-of-two
  324. (logxor (cdr qa) (cdr qb))))))
  325. (defun calcFunc-diff (a b &optional w) ; [I I I] [Public]
  326. (cond ((Math-messy-integerp w)
  327. (calcFunc-diff a b (math-trunc w)))
  328. ((and w (not (integerp w)))
  329. (math-reject-arg w 'fixnump))
  330. ((and (integerp a) (integerp b))
  331. (math-clip (logand a (lognot b)) w))
  332. ((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod))
  333. (math-binary-modulo-args 'calcFunc-diff a b w))
  334. ((not (Math-num-integerp a))
  335. (math-reject-arg a 'integerp))
  336. ((not (Math-num-integerp b))
  337. (math-reject-arg b 'integerp))
  338. (t (math-clip (cons 'bigpos
  339. (math-diff-bignum (math-binary-arg a w)
  340. (math-binary-arg b w)))
  341. w))))
  342. (defun math-diff-bignum (a b) ; [l l l]
  343. (and a
  344. (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two))
  345. (qb (math-div-bignum-digit b math-bignum-digit-power-of-two)))
  346. (math-mul-bignum-digit (math-diff-bignum (math-norm-bignum (car qa))
  347. (math-norm-bignum (car qb)))
  348. math-bignum-digit-power-of-two
  349. (logand (cdr qa) (lognot (cdr qb)))))))
  350. (defun calcFunc-not (a &optional w) ; [I I] [Public]
  351. (cond ((Math-messy-integerp w)
  352. (calcFunc-not a (math-trunc w)))
  353. ((eq (car-safe a) 'mod)
  354. (math-binary-modulo-args 'calcFunc-not a nil w))
  355. ((and w (not (integerp w)))
  356. (math-reject-arg w 'fixnump))
  357. ((not (Math-num-integerp a))
  358. (math-reject-arg a 'integerp))
  359. ((< (or w (setq w calc-word-size)) 0)
  360. (math-clip (calcFunc-not a (- w)) w))
  361. (t (math-normalize
  362. (cons 'bigpos
  363. (math-not-bignum (math-binary-arg a w)
  364. w))))))
  365. (defun math-not-bignum (a w) ; [l l]
  366. (let ((q (math-div-bignum-digit a math-bignum-digit-power-of-two)))
  367. (if (<= w math-bignum-logb-digit-size)
  368. (list (logand (lognot (cdr q))
  369. (1- (lsh 1 w))))
  370. (math-mul-bignum-digit (math-not-bignum (math-norm-bignum (car q))
  371. (- w math-bignum-logb-digit-size))
  372. math-bignum-digit-power-of-two
  373. (logxor (cdr q)
  374. (1- math-bignum-digit-power-of-two))))))
  375. (defun calcFunc-lsh (a &optional n w) ; [I I] [Public]
  376. (setq a (math-trunc a)
  377. n (if n (math-trunc n) 1))
  378. (if (eq (car-safe a) 'mod)
  379. (math-binary-modulo-args 'calcFunc-lsh a n w)
  380. (setq w (if w (math-trunc w) calc-word-size))
  381. (or (integerp w)
  382. (math-reject-arg w 'fixnump))
  383. (or (Math-integerp a)
  384. (math-reject-arg a 'integerp))
  385. (or (Math-integerp n)
  386. (math-reject-arg n 'integerp))
  387. (if (< w 0)
  388. (math-clip (calcFunc-lsh a n (- w)) w)
  389. (if (Math-integer-negp a)
  390. (setq a (math-clip a w)))
  391. (cond ((or (Math-lessp n (- w))
  392. (Math-lessp w n))
  393. 0)
  394. ((< n 0)
  395. (math-quotient (math-clip a w) (math-power-of-2 (- n))))
  396. (t
  397. (math-clip (math-mul a (math-power-of-2 n)) w))))))
  398. (defun calcFunc-rsh (a &optional n w) ; [I I] [Public]
  399. (calcFunc-lsh a (math-neg (or n 1)) w))
  400. (defun calcFunc-ash (a &optional n w) ; [I I] [Public]
  401. (if (or (null n)
  402. (not (Math-negp n)))
  403. (calcFunc-lsh a n w)
  404. (setq a (math-trunc a)
  405. n (if n (math-trunc n) 1))
  406. (if (eq (car-safe a) 'mod)
  407. (math-binary-modulo-args 'calcFunc-ash a n w)
  408. (setq w (if w (math-trunc w) calc-word-size))
  409. (or (integerp w)
  410. (math-reject-arg w 'fixnump))
  411. (or (Math-integerp a)
  412. (math-reject-arg a 'integerp))
  413. (or (Math-integerp n)
  414. (math-reject-arg n 'integerp))
  415. (if (< w 0)
  416. (math-clip (calcFunc-ash a n (- w)) w)
  417. (if (Math-integer-negp a)
  418. (setq a (math-clip a w)))
  419. (let ((two-to-sizem1 (math-power-of-2 (1- w)))
  420. (sh (calcFunc-lsh a n w)))
  421. (cond ((Math-natnum-lessp a two-to-sizem1)
  422. sh)
  423. ((Math-lessp n (- 1 w))
  424. (math-add (math-mul two-to-sizem1 2) -1))
  425. (t (let ((two-to-n (math-power-of-2 (- n))))
  426. (math-add (calcFunc-lsh (math-add two-to-n -1)
  427. (+ w n) w)
  428. sh)))))))))
  429. (defun calcFunc-rash (a &optional n w) ; [I I] [Public]
  430. (calcFunc-ash a (math-neg (or n 1)) w))
  431. (defun calcFunc-rot (a &optional n w) ; [I I] [Public]
  432. (setq a (math-trunc a)
  433. n (if n (math-trunc n) 1))
  434. (if (eq (car-safe a) 'mod)
  435. (math-binary-modulo-args 'calcFunc-rot a n w)
  436. (setq w (if w (math-trunc w) calc-word-size))
  437. (or (integerp w)
  438. (math-reject-arg w 'fixnump))
  439. (or (Math-integerp a)
  440. (math-reject-arg a 'integerp))
  441. (or (Math-integerp n)
  442. (math-reject-arg n 'integerp))
  443. (if (< w 0)
  444. (math-clip (calcFunc-rot a n (- w)) w)
  445. (if (Math-integer-negp a)
  446. (setq a (math-clip a w)))
  447. (cond ((or (Math-integer-negp n)
  448. (not (Math-natnum-lessp n w)))
  449. (calcFunc-rot a (math-mod n w) w))
  450. (t
  451. (math-add (calcFunc-lsh a (- n w) w)
  452. (calcFunc-lsh a n w)))))))
  453. (defun math-clip (a &optional w) ; [I I] [Public]
  454. (cond ((Math-messy-integerp w)
  455. (math-clip a (math-trunc w)))
  456. ((eq (car-safe a) 'mod)
  457. (math-binary-modulo-args 'math-clip a nil w))
  458. ((and w (not (integerp w)))
  459. (math-reject-arg w 'fixnump))
  460. ((not (Math-num-integerp a))
  461. (math-reject-arg a 'integerp))
  462. ((< (or w (setq w calc-word-size)) 0)
  463. (setq a (math-clip a (- w)))
  464. (if (Math-natnum-lessp a (math-power-of-2 (- -1 w)))
  465. a
  466. (math-sub a (math-power-of-2 (- w)))))
  467. ((Math-negp a)
  468. (math-normalize (cons 'bigpos (math-binary-arg a w))))
  469. ((and (integerp a) (< a math-small-integer-size))
  470. (if (> w (logb math-small-integer-size))
  471. a
  472. (logand a (1- (lsh 1 w)))))
  473. (t
  474. (math-normalize
  475. (cons 'bigpos
  476. (math-clip-bignum (cdr (math-bignum-test (math-trunc a)))
  477. w))))))
  478. (defalias 'calcFunc-clip 'math-clip)
  479. (defun math-clip-bignum (a w) ; [l l]
  480. (let ((q (math-div-bignum-digit a math-bignum-digit-power-of-two)))
  481. (if (<= w math-bignum-logb-digit-size)
  482. (list (logand (cdr q)
  483. (1- (lsh 1 w))))
  484. (math-mul-bignum-digit (math-clip-bignum (math-norm-bignum (car q))
  485. (- w math-bignum-logb-digit-size))
  486. math-bignum-digit-power-of-two
  487. (cdr q)))))
  488. (defvar math-max-digits-cache nil)
  489. (defun math-compute-max-digits (w r)
  490. (let* ((pair (+ (* r 100000) w))
  491. (res (assq pair math-max-digits-cache)))
  492. (if res
  493. (cdr res)
  494. (let* ((calc-command-flags nil)
  495. (digs (math-ceiling (math-div w (math-real-log2 r)))))
  496. (setq math-max-digits-cache (cons (cons pair digs)
  497. math-max-digits-cache))
  498. digs))))
  499. (defvar math-log2-cache (list '(2 . 1)
  500. '(4 . 2)
  501. '(8 . 3)
  502. '(10 . (float 332193 -5))
  503. '(16 . 4)
  504. '(32 . 5)))
  505. (defun math-real-log2 (x) ;;; calc-internal-prec must be 6
  506. (let ((res (assq x math-log2-cache)))
  507. (if res
  508. (cdr res)
  509. (let* ((calc-symbolic-mode nil)
  510. (calc-display-working-message nil)
  511. (log (calcFunc-log x 2)))
  512. (setq math-log2-cache (cons (cons x log) math-log2-cache))
  513. log))))
  514. (defconst math-radix-digits ["0" "1" "2" "3" "4" "5" "6" "7" "8" "9"
  515. "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
  516. "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T"
  517. "U" "V" "W" "X" "Y" "Z"])
  518. (defsubst math-format-radix-digit (a) ; [X D]
  519. (aref math-radix-digits a))
  520. (defun math-format-radix (a) ; [X S]
  521. (if (< a calc-number-radix)
  522. (if (< a 0)
  523. (concat "-" (math-format-radix (- a)))
  524. (math-format-radix-digit a))
  525. (let ((s ""))
  526. (while (> a 0)
  527. (setq s (concat (math-format-radix-digit (% a calc-number-radix)) s)
  528. a (/ a calc-number-radix)))
  529. s)))
  530. (defconst math-binary-digits ["000" "001" "010" "011"
  531. "100" "101" "110" "111"])
  532. (defun math-format-binary (a) ; [X S]
  533. (if (< a 8)
  534. (if (< a 0)
  535. (concat "-" (math-format-binary (- a)))
  536. (math-format-radix a))
  537. (let ((s ""))
  538. (while (> a 7)
  539. (setq s (concat (aref math-binary-digits (% a 8)) s)
  540. a (/ a 8)))
  541. (concat (math-format-radix a) s))))
  542. (defun math-format-bignum-radix (a) ; [X L]
  543. (cond ((null a) "0")
  544. ((and (null (cdr a))
  545. (< (car a) calc-number-radix))
  546. (math-format-radix-digit (car a)))
  547. (t
  548. (let ((q (math-div-bignum-digit a calc-number-radix)))
  549. (concat (math-format-bignum-radix (math-norm-bignum (car q)))
  550. (math-format-radix-digit (cdr q)))))))
  551. (defun math-format-bignum-binary (a) ; [X L]
  552. (cond ((null a) "0")
  553. ((null (cdr a))
  554. (math-format-binary (car a)))
  555. (t
  556. (let ((q (math-div-bignum-digit a 512)))
  557. (concat (math-format-bignum-binary (math-norm-bignum (car q)))
  558. (aref math-binary-digits (/ (cdr q) 64))
  559. (aref math-binary-digits (% (/ (cdr q) 8) 8))
  560. (aref math-binary-digits (% (cdr q) 8)))))))
  561. (defun math-format-bignum-octal (a) ; [X L]
  562. (cond ((null a) "0")
  563. ((null (cdr a))
  564. (math-format-radix (car a)))
  565. (t
  566. (let ((q (math-div-bignum-digit a 512)))
  567. (concat (math-format-bignum-octal (math-norm-bignum (car q)))
  568. (math-format-radix-digit (/ (cdr q) 64))
  569. (math-format-radix-digit (% (/ (cdr q) 8) 8))
  570. (math-format-radix-digit (% (cdr q) 8)))))))
  571. (defun math-format-bignum-hex (a) ; [X L]
  572. (cond ((null a) "0")
  573. ((null (cdr a))
  574. (math-format-radix (car a)))
  575. (t
  576. (let ((q (math-div-bignum-digit a 256)))
  577. (concat (math-format-bignum-hex (math-norm-bignum (car q)))
  578. (math-format-radix-digit (/ (cdr q) 16))
  579. (math-format-radix-digit (% (cdr q) 16)))))))
  580. ;;; Decompose into integer and fractional parts, without depending
  581. ;;; on calc-internal-prec.
  582. (defun math-float-parts (a need-frac) ; returns ( int frac fracdigs )
  583. (if (>= (nth 2 a) 0)
  584. (list (math-scale-rounding (nth 1 a) (nth 2 a)) '(float 0 0) 0)
  585. (let* ((d (math-numdigs (nth 1 a)))
  586. (n (- (nth 2 a))))
  587. (if need-frac
  588. (if (>= n d)
  589. (list 0 a n)
  590. (let ((qr (math-idivmod (nth 1 a) (math-scale-int 1 n))))
  591. (list (car qr) (math-make-float (cdr qr) (- n)) n)))
  592. (list (math-scale-rounding (nth 1 a) (nth 2 a))
  593. '(float 0 0) 0)))))
  594. (defun math-format-radix-float (a prec)
  595. (let ((fmt (car calc-float-format))
  596. (figs (nth 1 calc-float-format))
  597. (point calc-point-char)
  598. (str nil)
  599. pos)
  600. (if (eq fmt 'fix)
  601. (let* ((afigs (math-abs figs))
  602. (fp (math-float-parts a (> afigs 0)))
  603. (calc-internal-prec (+ 3 (max (nth 2 fp)
  604. (math-convert-radix-digits
  605. afigs t))))
  606. (int (car fp))
  607. (frac (math-round (math-mul (math-normalize (nth 1 fp))
  608. (math-radix-float-power afigs)))))
  609. (if (not (and (math-zerop frac) (math-zerop int) (< figs 0)))
  610. (let ((math-radix-explicit-format nil))
  611. (let ((calc-group-digits nil))
  612. (setq str (if (> afigs 0) (math-format-number frac) ""))
  613. (if (< (length str) afigs)
  614. (setq str (concat (make-string (- afigs (length str)) ?0)
  615. str))
  616. (if (> (length str) afigs)
  617. (setq str (substring str 1)
  618. int (math-add int 1))))
  619. (setq str (concat (math-format-number int) point str)))
  620. (when calc-group-digits
  621. (setq str (math-group-float str))))
  622. (setq figs 0))))
  623. (or str
  624. (let* ((prec calc-internal-prec)
  625. (afigs (if (> figs 0)
  626. figs
  627. (max 1 (+ figs
  628. (1- (math-convert-radix-digits
  629. (max prec
  630. (math-numdigs (nth 1 a)))))))))
  631. (calc-internal-prec (+ 3 (math-convert-radix-digits afigs t)))
  632. (explo -1) (vlo (math-radix-float-power explo))
  633. (exphi 1) (vhi (math-radix-float-power exphi))
  634. expmid vmid eadj)
  635. (setq a (math-normalize a))
  636. (if (Math-zerop a)
  637. (setq explo 0)
  638. (if (math-lessp-float '(float 1 0) a)
  639. (while (not (math-lessp-float a vhi))
  640. (setq explo exphi vlo vhi
  641. exphi (math-mul exphi 2)
  642. vhi (math-radix-float-power exphi)))
  643. (while (math-lessp-float a vlo)
  644. (setq exphi explo vhi vlo
  645. explo (math-mul explo 2)
  646. vlo (math-radix-float-power explo))))
  647. (while (not (eq (math-sub exphi explo) 1))
  648. (setq expmid (math-div2 (math-add explo exphi))
  649. vmid (math-radix-float-power expmid))
  650. (if (math-lessp-float a vmid)
  651. (setq exphi expmid vhi vmid)
  652. (setq explo expmid vlo vmid)))
  653. (setq a (math-div-float a vlo)))
  654. (let* ((sc (math-round (math-mul a (math-radix-float-power
  655. (1- afigs)))))
  656. (math-radix-explicit-format nil))
  657. (let ((calc-group-digits nil))
  658. (setq str (math-format-number sc))))
  659. (if (> (length str) afigs)
  660. (setq str (substring str 0 -1)
  661. explo (1+ explo)))
  662. (if (and (eq fmt 'float)
  663. (math-lessp explo (+ (if (= figs 0)
  664. (1- (math-convert-radix-digits
  665. prec))
  666. afigs)
  667. calc-display-sci-high 1))
  668. (math-lessp calc-display-sci-low explo))
  669. (let ((dpos (1+ explo)))
  670. (cond ((<= dpos 0)
  671. (setq str (concat "0" point (make-string (- dpos) ?0)
  672. str)))
  673. ((> dpos (length str))
  674. (setq str (concat str (make-string (- dpos (length str))
  675. ?0) point)))
  676. (t
  677. (setq str (concat (substring str 0 dpos) point
  678. (substring str dpos)))))
  679. (setq explo nil))
  680. (setq eadj (if (eq fmt 'eng)
  681. (min (math-mod explo 3) (length str))
  682. 0)
  683. str (concat (substring str 0 (1+ eadj)) point
  684. (substring str (1+ eadj)))))
  685. (setq pos (length str))
  686. (while (eq (aref str (1- pos)) ?0) (setq pos (1- pos)))
  687. (and explo (eq (aref str (1- pos)) ?.) (setq pos (1- pos)))
  688. (setq str (substring str 0 pos))
  689. (when calc-group-digits
  690. (setq str (math-group-float str)))
  691. (if explo
  692. (let ((estr (let ((calc-number-radix 10)
  693. (calc-group-digits nil))
  694. (math-format-number
  695. (math-sub explo eadj)))))
  696. (setq str (if (or (memq calc-language '(math maple))
  697. (> calc-number-radix 14))
  698. (format "%s*%d.^%s" str calc-number-radix estr)
  699. (format "%se%s" str estr)))))))
  700. str))
  701. (defvar math-radix-digits-cache nil)
  702. (defun math-convert-radix-digits (n &optional to-dec)
  703. (let ((key (cons n (cons to-dec calc-number-radix))))
  704. (or (cdr (assoc key math-radix-digits-cache))
  705. (let* ((calc-internal-prec 6)
  706. (log (math-div (math-real-log2 calc-number-radix)
  707. '(float 332193 -5))))
  708. (cdr (car (setq math-radix-digits-cache
  709. (cons (cons key (math-ceiling (if to-dec
  710. (math-mul n log)
  711. (math-div n log))))
  712. math-radix-digits-cache))))))))
  713. (defvar math-radix-float-cache-tag nil)
  714. (defvar math-radix-float-cache)
  715. (defun math-radix-float-power (n)
  716. (if (eq n 0)
  717. '(float 1 0)
  718. (or (and (eq calc-number-radix (car math-radix-float-cache-tag))
  719. (<= calc-internal-prec (cdr math-radix-float-cache-tag)))
  720. (setq math-radix-float-cache-tag (cons calc-number-radix
  721. calc-internal-prec)
  722. math-radix-float-cache nil))
  723. (math-normalize
  724. (or (cdr (assoc n math-radix-float-cache))
  725. (cdr (car (setq math-radix-float-cache
  726. (cons (cons
  727. n
  728. (let ((calc-internal-prec
  729. (cdr math-radix-float-cache-tag)))
  730. (if (math-negp n)
  731. (math-div-float '(float 1 0)
  732. (math-radix-float-power
  733. (math-neg n)))
  734. (math-mul-float (math-sqr-float
  735. (math-radix-float-power
  736. (math-div2 n)))
  737. (if (math-evenp n)
  738. '(float 1 0)
  739. (math-float
  740. calc-number-radix))))))
  741. math-radix-float-cache))))))))
  742. ;;; Two's complement mode
  743. (defun math-format-twos-complement (a)
  744. "Format an integer in two's complement mode."
  745. (let* (;(calc-leading-zeros t)
  746. (overflow nil)
  747. (negative nil)
  748. (num
  749. (cond
  750. ((or (eq a 0)
  751. (and (Math-integer-posp a)))
  752. (if (integerp a)
  753. (math-format-radix a)
  754. (math-format-bignum-radix (cdr a))))
  755. ((Math-integer-negp a)
  756. (let ((newa (math-add a math-2-word-size)))
  757. (if (integerp newa)
  758. (math-format-radix newa)
  759. (math-format-bignum-radix (cdr newa))))))))
  760. (let* ((calc-internal-prec 6)
  761. (digs (math-compute-max-digits (math-abs calc-word-size)
  762. calc-number-radix))
  763. (len (length num)))
  764. (if (< len digs)
  765. (setq num (concat (make-string (- digs len) ?0) num))))
  766. (when calc-group-digits
  767. (setq num (math-group-float num)))
  768. (concat
  769. (number-to-string calc-number-radix)
  770. "##"
  771. num)))
  772. (provide 'calc-bin)
  773. ;;; calc-bin.el ends here