does emacs has rational number data type
Asked Answered
M

4

5

I eval a lisp expression in scratch

(+ (/ 1 2) (/ 1 2))

I got a 0.

normally it should be 1.

Mildamilde answered 23/5, 2012 at 11:40 Comment(1)
Didn't you just answer the question yourself?Congdon
C
9

As Oleg points out, operators usually default to integer arithmetic unless you include floating point arguments (like 1.0).

With respect to your question about rational number support, emacs-calc (which is part of emacs) supports many number types including fractions (i.e. rational numbers), complex numbers, infinite precision integers, etc. Your code must call emacs-calc functions (instead of /, etc.) in order to use calc's arithmetic.

GNU Emacs Calc Manual:

Colorcast answered 23/5, 2012 at 14:31 Comment(1)
Ah, calc to the rescue yet again. I should have thought of that.Blepharitis
K
4

Try this way

(+ (/ 1.0 2) (/ 1.0 2))

According to emacs doc

Function: / dividend divisor &rest divisors

if all the arguments are integers, then the result is an integer too.

Kalevala answered 23/5, 2012 at 11:58 Comment(1)
That fails to answer the question, although between the lines, you can infer that because Emacs uses floats, it doesn't have a separate type for real numbers. (Many Lisp implementations have one; that is, 1/3 is represented precisely, rather than as a floating-point approximation.)Congdon
B
1

You can read all about numbers in elisp here:

C-hig (elisp) Numbers RET

As already indicated by tripleee, it is apparent that the answer is "no".

Blepharitis answered 23/5, 2012 at 13:16 Comment(1)
...except that, as per Juancho's answer, the calc library makes that a "yes".Blepharitis
G
0

Emacs calc has rational data type: use colon, like 1:2 == 0.5 or 5:3 == 1 + 2:3 == 1:2:3.

This way Emacs calc simplifies expressions, for example if you deal with display resolutions for 1920:1080 it prints 16:9! If you want 1440p with the 16:9 ratio: 1440 * 16:92560.

Guanabana answered 27/11, 2022 at 15:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.