I eval a lisp expression in scratch
(+ (/ 1 2) (/ 1 2))
I got a 0.
normally it should be 1.
I eval a lisp expression in scratch
(+ (/ 1 2) (/ 1 2))
I got a 0.
normally it should be 1.
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:
calc
to the rescue yet again. I should have thought of that. –
Blepharitis 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.
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".
calc
library makes that a "yes". –
Blepharitis 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:9
⇒ 2560
.
© 2022 - 2024 — McMap. All rights reserved.