10
votes
elisp: Why does + without parameters return 0?
Math. The sum of nothing is obviously nothing. The product of nothing is more mysterious, because it isn’t nothing. But if you look closely you will soon notice that in both cases you get back the ...
9
votes
elisp: Why does + without parameters return 0?
This is a very natural outcome.
For instance,
(+ 2 10 30)
you know the result is 42, right?
So, what if we remove the 10?
Obviously, the result will decrease by 10, meaning the result of
(+ 2 30)
...
8
votes
Quickly Evaluate Infix Math Expression?
You can use the built-in calc package to do so.
Mark your expression, e.g. 3 * (1 + 1)
Call calc-grab-region
Call calc-dispatch (default C-x *), then hit g
7
votes
Accepted
Hоw add numbers in differents lines?
My favourite method for summing a column:
Mark one corner of the rectangle
Move point to the opposite corner
Type C-x*:
Acting on a rectangle is useful because it still works as-is if the numbers ...
7
votes
Accepted
How to subtract 1 from the nth element of a list of numbers?
Using the built-in cl-lib package, you can write the following:
(eval-when-compile
(require 'cl-lib))
(cl-decf (nth 1 x))
Alternatively, in vanilla Elisp:
(setf (nth 1 x) (1- (nth 1 x)))
Be ...
6
votes
Does anyone know of a quick method to get hexadecimal line numbers in emacs?
Linum mode provides the linum-format customizable variable that allows you to format how you want your line numbers to appear. Set this variable to have the value "%x" to get hexadecimal ...
6
votes
minor mode to display long integers?
I did some searching and surprisingly couldn't find any existing minor modes that seemed to do this. I thought it would be a fun little project, however, so I've written a minimal minor mode (rainbow-...
6
votes
elisp: Why does + without parameters return 0?
This is due to associativity of addition:
Sum(Array1) + Sum(Array2) = Sum(concat(Array1, Array2))
If Array1 is empty, we get
Sum(Empty) + Sum(Array2) = Sum(Array2)
thus Sum(Empty)=0.
See also https:/...
4
votes
Accepted
How to change the color of numbers for a theme?
You can achieve highlighting of numbers in the buffers using highlight-numbers package. The face used for numbering is highlight-numbers-number and it inherits from font-lock-constant-face.
For a ...
4
votes
How to use fixed point decimals in emacs lisp?
You could use the fractional representation of numbers from calc.
I am not sure why you think that passing the amount as string is inconvenient for the user.
I would pass that input to calc and ...
4
votes
elisp: Why does + without parameters return 0?
I think you ask both:
Why are + and * multiple-arity functions and they allow even zero arguments?
Why isn't this the case for / (division)?
Others have answered #1: both + and * have an identity ...
3
votes
Hоw add numbers in differents lines?
Wrap the numbers in (+ ...), that is insert (+ at the beginning and ) at the end, then hit C-x C-e (eval-last-sexp).
3
votes
Treat all numbers as floats
For numeric calculation there is a parallel universe within Elisp: Calc.
The division operator / within defmath works with the floating numbers of Calc.
Nevertheless, the interface of Calc with the ...
3
votes
Accepted
Treat all numbers as floats
The following Elisp code demonstrates how you can define your own wrapper macro with-float/
that treats all explicit occurences of / in its body as division of floats.
Division operators that are ...
3
votes
Quickly Evaluate Infix Math Expression?
You might also use calc's embedded mode if you intent to replace an expression with its result in the current buffer. C-u C-x * e when your formula / expression is on it's own on a separate line.
...
3
votes
Accepted
Next greater or lesser number than ARG in a list of integers
Assuming that by lesser and greater you mean something like < and >, this seems to do what you ask.
(defun my-func (num nbs pred)
(let (ans)
(dolist (nb nbs)
(when (and (funcall ...
3
votes
Accepted
Why can't I do this arithmetic operation on eshell? Error: "Symbol’s function definition is void: \,"
Eshell doesn’t like your numbers. It expects you to use a period for the decimal separator, and it also expects you not to use any thousands separator. Like this:
~/$ (+ 0
-171055.81
-826735.77 ...
3
votes
Accepted
How can I check whether a string represents a number in Elisp?
string-match-p can be used to check for numbers in a string STRING using the syntax (string-match-p REGEXP STRING) where REGEXP is a string holding a regular expression. Note that string-match-p does ...
3
votes
Using `string-to-number` causes rounding/precision issue for specific floating point values
What Every Programmer Should Know About Floating-Point Arithmetic is an excellent guide to understanding floating point numbers. I thoroughly recommend reading that in full -- I think it will answer ...
3
votes
Accepted
How can I read a number as user input?
What function should I call
read-number
how should it be used
You can call it directly, e.g., (read-number "").
But if you are writing a function, I recommend you put (interactive "n&...
2
votes
Accepted
How should one interpret the printed representation of integers in Emacs Lisp?
The ?\C-j is the character representation of 10, in this case it's the same as control-j.
For other values, it's more natural, for example 65, which is the ASCII value of A:
65
-> 65 (#o101, #x41,...
2
votes
floating-point addition
What you're seeing is a byproduct of binary floating-point arithmetic. Non-integer decimal values generally don't have a representation in binary floating point that is truly equal. For many ...
2
votes
How can I use a char as a counter?
For your specific example, you can also use
C-M-% affiliation RET \&[\,(string (+ ?a \#))] RET
C-M-% is the default binding of query-replace-regexp
affiliation is regexp to search for
\&[\,(...
2
votes
How can I use a char as a counter?
You probably want the make-string function. If count starts at 0 and counts up, then:
(make-string 1 (+ ?a count))
will go a, b, c, etc.
2
votes
How to replace a digit by a letter?
The following should work:
M-x query-replace-regexp RET \([0-9]+\) RET \,(string (+ (1- ?a) \#1)) RET
2
votes
Hоw add numbers in differents lines?
(defun sum-it-up ()
"Sum the whole buffer if no active region, or just the active region (if applicable).
Copy the result to the kill ring. The result is thrown just in case anyone needs it."
(...
2
votes
Accepted
Representing leading zeroes in a list
You can use this org table formula, format is used to ensure the number's width is 2 and padded with zero, as @sds's answer suggested:
#+NAME: addition-table
| | 0 | 1 | 2 | 3 | 4 | 5 | 6 | ...
2
votes
How to replace first 100 digits with letters?
You can use org-number-to-letter from org-table.el (requires at least Emacs 26.1 or Org 9.1.9?)
(require 'org-table)
(org-number-to-letters 26)
;; => "Z"
(org-number-to-letters 52)
;; => "AZ"
...
2
votes
How to replace first 100 digits with letters?
104 would be cz I think.
Using org mode eval setup:-- I suspect there are better choices fr functions but I'm an elisp learner.
These conversions might be more appropriates in your code in that ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
numbers × 36characters × 6
string × 5
replace × 4
regular-expressions × 3
list × 3
calc × 3
org-mode × 2
format × 2
debugging × 1
search × 1
themes × 1
syntax-highlighting × 1
eshell × 1
minibuffer × 1
variables × 1
highlighting × 1
abbrev × 1
query-replace × 1
markdown-mode × 1
insert × 1
symbols × 1
linum-mode × 1
text × 1
json × 1