From: Sam Halliday <sam.halliday@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: calc-eval and lsh/logand of large numbers
Date: Mon, 27 Oct 2014 15:58:37 -0700 (PDT) [thread overview]
Message-ID: <bcee5025-f781-4fb6-9ede-9c47c6ae76e4@googlegroups.com> (raw)
Hi all,
I was playing with calc-eval to get a feel for the arbitrary precision support and decided to see if it could be used to represent a BitSet. If you're not familiar with the concept, imagine a list of bits and if the N'th bit is true then the set contains the integer N. It's reasonably efficient at storing arbitrary collections of integers: memory requirement is specified by the maximum value rather than the number of entries in the set.
This looked promising:
(require 'calc)
(defmath bitIndex (i)
(lsh 1 i))
(calc-eval "bitIndex(10)") ; "1024"
but when I went to a number that would be shifted beyond the 64 bit length of an integer it all fell apart
(calc-eval "bitIndex(64)") ; "0"
I would have expected this to return "18446744073709551616".
Is this a known limitation or have I simply not understood something?
BTW, if anybody is interested I was planning on doing something like the following. If you have a cleaner way of doing this, please let me know as I'll be interested to hear about it. The reason I'm doing this is because I am writing an S-Exp serialisation layer in Scala and this notation works well for serialising Scala's BitSet's... I was curious to see if I could get it to work in elisp: https://github.com/fommil/ensime-server/blob/sexp/src/main/scala/org/ensime/sexp/formats/CollectionFormats.scala#L131
(defmath bitsetAnd (bitset i)
(logand (lsh 1 i) bitset))
(defun bitsetContains (bitset i)
(<= 1 (calc-eval "bitsetAnd($, $$)" 'raw bitset i)))
(bitsetContains "10#3" 0) ; 't
(bitsetContains "10#3" 1) ; 't
(bitsetContains "10#3" 2) ; nil
(bitsetContains "16#10000000000000000" 64) ; nil
(bitsetContains "16#10000000000000001" 0) ; t
(bitsetContains "16#10000000000000001" 64) ; t WRONG
(bitsetContains "16#10000000000000002" 1) ; t
(bitsetContains "16#10000000000000002" 64) ; t WRONG
(bitsetContains "16#10000000000000002" 0) ; nil
Best regards,
Sam
next reply other threads:[~2014-10-27 22:58 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-10-27 22:58 Sam Halliday [this message]
2014-10-28 9:34 ` calc-eval and lsh/logand of large numbers Sam Halliday
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=bcee5025-f781-4fb6-9ede-9c47c6ae76e4@googlegroups.com \
--to=sam.halliday@gmail.com \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.