help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: Calculator: no exponent, full number ?


From: Emanuel Berg
Subject: Re: Calculator: no exponent, full number ?
Date: Thu, 06 Jun 2019 01:47:37 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux)

jonetsu wrote:

> Emmanuel Berg: "Or use Lisp, create a file
> and just type Lisp. With `format', you can
> get the result look anyway you want."
>
> I must say that I do not know anything about
> Lisp [...]

If you want to do math with Lisp, it is much
better to be good with math and bad at Lisp,
than the other way around.

Because it is very easy. Just understand the
"fully parenthesized prefix notation" [1] and
that will be it.

So, the operator comes first:
(+ 1 2 3 4) ; is 1 + 2 + 3 + 4 = 10

And, the priority of operators is never an
issue as _everything_ is parenthesized.

That's it!

For more advanced math, you'll probably need to
find a math library with additional operators
and constants, perhaps in on of the
[M]ELPA packs, but for the basic stuff, it is
as simple as it can be.

Here, have a look:


(defun hypotenuse (c1 c2)
  (sqrt (+ (* c1 c1) (* c2 c2))) )


or, a little bit more advanced, involving
a list and a set function:


(defun mean-value (vs)
  (let*((sum  (apply #'+ vs))
        (mean (/ sum (length vs) 1.0)) )
    mean) ) ; [2]


Eval me: (mean-value '(1 2 3 4 5.5)) ; 3.1


One of the advantages with this Lisp-file
method is that you have every data item in the
file. Nothing gets lost in the history of the
calculator, and what you have you can change
and instantly have the whole thing
computed anew.


[1] https://en.wikipedia.org/wiki/Lisp_(programming_language)
[2] https://dataswamp.org/~incal/emacs-init/my-math.el

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




reply via email to

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