* Strange results
@ 2013-09-07 7:06 Dwaddle
2013-09-07 7:22 ` Erich Neuwirth
` (3 more replies)
0 siblings, 4 replies; 8+ messages in thread
From: Dwaddle @ 2013-09-07 7:06 UTC (permalink / raw)
To: help-gnu-emacs
Hi
i've been dabbling around in elisp for a while. Made this function
The issue is the byte var which 'remembers' the result from a previous calculation.
(integer-bin 5) [0 0 0 0 0 1 0 1]
(integer-bin 3) [0 0 0 0 0 1 1 1]
To beat the critics I know it isn't very clean programmed and a loop would make it al lot shorter
<code>
(defun integer-bin (dec)
"Convert integer to binairy"
(setq 8bit [128 64 32 16 8 4 2 1])
(setq byte [0 0 0 0 0 0 0 0])
(if (= 1 (/ dec (aref 8bit 0))) (when
(aset byte 0 1)
(setq dec (- dec 128))))
(if (< 0 dec)
(if (= 1 (/ dec (aref 8bit 1))) (when
(aset byte 1 1)
(setq dec (- dec 64)))))
(if (< 0 dec)
(if (= 1 (/ dec (aref 8bit 2))) (when
(aset byte 2 1)
(setq dec (- dec 32)))))
(if (< 0 dec)
(if (= 1 (/ dec (aref 8bit 3))) (when
(aset byte 3 1)
(setq dec (- dec 16)))))
(if (< 0 dec)
(if (= 1 (/ dec (aref 8bit 4))) (when
(aset byte 4 1)
(setq dec (- dec 8)))))
(if (< 0 dec)
(if (= 1 (/ d<ec (aref 8bit 5))) (when
(aset byte 5 1)
(setq dec (- dec 4)))))
(if (< 0 dec)
(if (= 1 (/ dec (aref 8bit 6))) (when
(aset byte 6 1)
(setq dec (- dec 2)))))
(if (< 0 dec)
(if (= 1 1) (when (aset byte 7 1)
(setq dec (- dec 1)))))
(print byte)
)
</code>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Strange results
2013-09-07 7:06 Strange results Dwaddle
@ 2013-09-07 7:22 ` Erich Neuwirth
2013-09-07 7:49 ` Dwaddle
` (2 subsequent siblings)
3 siblings, 0 replies; 8+ messages in thread
From: Erich Neuwirth @ 2013-09-07 7:22 UTC (permalink / raw)
To: Dwaddle; +Cc: help-gnu-emacs@gnu.org
You need to learn about set and setq vs let
Von meinem iPad gesendet
Am 07.09.2013 um 09:06 schrieb Dwaddle <rmborchers@gmail.com>:
> Hi
>
> i've been dabbling around in elisp for a while. Made this function
>
> The issue is the byte var which 'remembers' the result from a previous calculation.
>
> (integer-bin 5) [0 0 0 0 0 1 0 1]
> (integer-bin 3) [0 0 0 0 0 1 1 1]
>
> To beat the critics I know it isn't very clean programmed and a loop would make it al lot shorter
>
> <code>
> (defun integer-bin (dec)
> "Convert integer to binairy"
> (setq 8bit [128 64 32 16 8 4 2 1])
> (setq byte [0 0 0 0 0 0 0 0])
> (if (= 1 (/ dec (aref 8bit 0))) (when
> (aset byte 0 1)
> (setq dec (- dec 128))))
> (if (< 0 dec)
> (if (= 1 (/ dec (aref 8bit 1))) (when
> (aset byte 1 1)
> (setq dec (- dec 64)))))
> (if (< 0 dec)
> (if (= 1 (/ dec (aref 8bit 2))) (when
> (aset byte 2 1)
> (setq dec (- dec 32)))))
> (if (< 0 dec)
> (if (= 1 (/ dec (aref 8bit 3))) (when
> (aset byte 3 1)
> (setq dec (- dec 16)))))
> (if (< 0 dec)
> (if (= 1 (/ dec (aref 8bit 4))) (when
> (aset byte 4 1)
> (setq dec (- dec 8)))))
> (if (< 0 dec)
> (if (= 1 (/ d<ec (aref 8bit 5))) (when
> (aset byte 5 1)
> (setq dec (- dec 4)))))
> (if (< 0 dec)
> (if (= 1 (/ dec (aref 8bit 6))) (when
> (aset byte 6 1)
> (setq dec (- dec 2)))))
> (if (< 0 dec)
> (if (= 1 1) (when (aset byte 7 1)
> (setq dec (- dec 1)))))
> (print byte)
> )
> </code>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Strange results
2013-09-07 7:06 Strange results Dwaddle
2013-09-07 7:22 ` Erich Neuwirth
@ 2013-09-07 7:49 ` Dwaddle
2013-09-07 7:59 ` Neuwirth Erich
2013-09-07 8:20 ` Dwaddle
2013-11-03 0:53 ` WJ
3 siblings, 1 reply; 8+ messages in thread
From: Dwaddle @ 2013-09-07 7:49 UTC (permalink / raw)
To: help-gnu-emacs
Op zaterdag 7 september 2013 09:06:21 UTC+2 schreef Dwaddle:
> Hi
Ok it's a problem of var scope. Until now never encountered scope problems
Thanks Erich
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Strange results
2013-09-07 7:49 ` Dwaddle
@ 2013-09-07 7:59 ` Neuwirth Erich
0 siblings, 0 replies; 8+ messages in thread
From: Neuwirth Erich @ 2013-09-07 7:59 UTC (permalink / raw)
To: Dwaddle; +Cc: help-gnu-emacs@gnu.org
[-- Attachment #1: Type: text/plain, Size: 289 bytes --]
It's a problem of scope and persistence
On Sep 7, 2013, at 9:49 AM, Dwaddle <rmborchers@gmail.com> wrote:
> Op zaterdag 7 september 2013 09:06:21 UTC+2 schreef Dwaddle:
>> Hi
>
> Ok it's a problem of var scope. Until now never encountered scope problems
>
> Thanks Erich
[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 1422 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Strange results
2013-09-07 7:06 Strange results Dwaddle
2013-09-07 7:22 ` Erich Neuwirth
2013-09-07 7:49 ` Dwaddle
@ 2013-09-07 8:20 ` Dwaddle
2013-09-07 12:34 ` Pascal J. Bourguignon
2013-11-03 0:53 ` WJ
3 siblings, 1 reply; 8+ messages in thread
From: Dwaddle @ 2013-09-07 8:20 UTC (permalink / raw)
To: help-gnu-emacs
let didn't do the job, same results. But the problem is solved by explicit setting the value to 0 or 1
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Strange results
2013-09-07 8:20 ` Dwaddle
@ 2013-09-07 12:34 ` Pascal J. Bourguignon
2013-09-07 13:54 ` Neuwirth Erich
0 siblings, 1 reply; 8+ messages in thread
From: Pascal J. Bourguignon @ 2013-09-07 12:34 UTC (permalink / raw)
To: help-gnu-emacs
Dwaddle <rmborchers@gmail.com> writes:
> let didn't do the job, same results. But the problem is solved by explicit setting the value to 0 or 1
Indeed, it's not only a problem of scope (with setq you modified global
dynamic variables, which is dangerous because it _may_ interfer with
other functions using the same variables.
But your problem was because you modified a literal object originally
built by the [0 0 0 0 0 0 0 0] expression. But it is always the same
object that is modified every time you call the function. Hence the use
of vector or make-vector, which create new vectors each time they're
evaluated.
--
__Pascal Bourguignon__
http://www.informatimago.com/
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Strange results
2013-09-07 12:34 ` Pascal J. Bourguignon
@ 2013-09-07 13:54 ` Neuwirth Erich
0 siblings, 0 replies; 8+ messages in thread
From: Neuwirth Erich @ 2013-09-07 13:54 UTC (permalink / raw)
To: Pascal J. Bourguignon; +Cc: help-gnu-emacs
[-- Attachment #1: Type: text/plain, Size: 930 bytes --]
One more remark.
Writing number base conversions without recursive functions feels utterly strange for me.
On Sep 7, 2013, at 2:34 PM, "Pascal J. Bourguignon" <pjb@informatimago.com> wrote:
> Dwaddle <rmborchers@gmail.com> writes:
>
>> let didn't do the job, same results. But the problem is solved by explicit setting the value to 0 or 1
>
> Indeed, it's not only a problem of scope (with setq you modified global
> dynamic variables, which is dangerous because it _may_ interfer with
> other functions using the same variables.
>
> But your problem was because you modified a literal object originally
> built by the [0 0 0 0 0 0 0 0] expression. But it is always the same
> object that is modified every time you call the function. Hence the use
> of vector or make-vector, which create new vectors each time they're
> evaluated.
>
> --
> __Pascal Bourguignon__
> http://www.informatimago.com/
[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 1422 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Strange results
2013-09-07 7:06 Strange results Dwaddle
` (2 preceding siblings ...)
2013-09-07 8:20 ` Dwaddle
@ 2013-11-03 0:53 ` WJ
3 siblings, 0 replies; 8+ messages in thread
From: WJ @ 2013-11-03 0:53 UTC (permalink / raw)
To: help-gnu-emacs
Dwaddle wrote:
> Hi
>
> i've been dabbling around in elisp for a while. Made this function
>
> The issue is the byte var which 'remembers' the result from a previous calculation.
>
> (integer-bin 5) [0 0 0 0 0 1 0 1]
> (integer-bin 3) [0 0 0 0 0 1 1 1]
>
> To beat the critics I know it isn't very clean programmed and a loop would make it al lot shorter
>
> <code>
> (defun integer-bin (dec)
> "Convert integer to binairy"
> (setq 8bit [128 64 32 16 8 4 2 1])
> (setq byte [0 0 0 0 0 0 0 0])
> (if (= 1 (/ dec (aref 8bit 0))) (when
> (aset byte 0 1)
> (setq dec (- dec 128))))
> (if (< 0 dec)
> (if (= 1 (/ dec (aref 8bit 1))) (when
> (aset byte 1 1)
> (setq dec (- dec 64)))))
> (if (< 0 dec)
> (if (= 1 (/ dec (aref 8bit 2))) (when
> (aset byte 2 1)
> (setq dec (- dec 32)))))
> (if (< 0 dec)
> (if (= 1 (/ dec (aref 8bit 3))) (when
> (aset byte 3 1)
> (setq dec (- dec 16)))))
> (if (< 0 dec)
> (if (= 1 (/ dec (aref 8bit 4))) (when
> (aset byte 4 1)
> (setq dec (- dec 8)))))
> (if (< 0 dec)
> (if (= 1 (/ d<ec (aref 8bit 5))) (when
> (aset byte 5 1)
> (setq dec (- dec 4)))))
> (if (< 0 dec)
> (if (= 1 (/ dec (aref 8bit 6))) (when
> (aset byte 6 1)
> (setq dec (- dec 2)))))
> (if (< 0 dec)
> (if (= 1 1) (when (aset byte 7 1)
> (setq dec (- dec 1)))))
> (print byte)
> )
> </code>
(defun integer->bin (byte)
(let (result)
(dotimes (_ 8)
(push (logand byte 1) result)
(setq byte (lsh byte -1)))
result))
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2013-11-03 0:53 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-09-07 7:06 Strange results Dwaddle
2013-09-07 7:22 ` Erich Neuwirth
2013-09-07 7:49 ` Dwaddle
2013-09-07 7:59 ` Neuwirth Erich
2013-09-07 8:20 ` Dwaddle
2013-09-07 12:34 ` Pascal J. Bourguignon
2013-09-07 13:54 ` Neuwirth Erich
2013-11-03 0:53 ` WJ
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.