emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: New library num-base-converters


From: Tino Calancha
Subject: RE: New library num-base-converters
Date: Tue, 15 Aug 2017 15:35:10 +0900 (JST)
User-agent: Alpine 2.20 (DEB 67 2015-01-07)



On Mon, 14 Aug 2017, Drew Adams wrote:

One point of this library is the convenience of not having to
know anything about neither calculator.el nor calc.el to get
the answer with easy.

Every Emacs user will understand the following:
M-x nbc-hex2dec ff RET
"ff hexadecimal = 255 decimal"

M-: (nbc-oct2dec "644") RET
=> 420

;; Or just `hex2dec' and `oct2dec' if you customize
;; `nbc-define-aliases' to non-nil.

(format "%d" #xff)  ; hex to decimal
--> "255"

(format "%x" 255)   ; decimal to hex
--> "ff"

(format "%d" #o644) ; octal to decimal
--> "420"

(format "%o" 420)   ; decimal to octal
--> "644"
For an user familiar with the Emacs '#' read syntaxis might be OK.
But for the average user, it's better to accept more input formats:
(nbc-hex2dec "#xff")
"255"
(nbc-hex2dec "ff")
"255"
(nbc-hex2dec "0xff")
"255"



And didn't Pascal B. point this out in the thread previously cited?

   (format "#8r%o #10r%d #16r%x" 42 42 42)
   --> "#8r52 #10r42 #16r2a"

*) cannot be called interactively.
*) This covers the base range 2 <= b <= 16.  Even worse, it silently
   returns a wrong result for b > 16:
(format "#16r%x" 42)
"#16r2a"
(format "#17r%x" 42)
"#17r2a"
(format "#37r%x" 42)
"#37r2a"

Compare with:

(nbc-number-base-converter "42" 10 16)
"2A"
(nbc-number-base-converter "42" 10 17)
"28"
(nbc-number-base-converter "42" 10 37)
;; Signal error: Base ‘b’ must satisfy 2 <= b <= 36: base-in ‘10’ base-out ‘37’
reply via email to

[Prev in Thread] Current Thread [Next in Thread]