egallesio / STklos

STklos Scheme
http://stklos.net
GNU General Public License v2.0
68 stars 17 forks source link

Make `abs` work for complexes #588

Closed jpellegrini closed 10 months ago

jpellegrini commented 11 months ago

R7RS says it "computes the absolute value of its argument", but doesn't imply it should be real... So we do compute the norm, (abs a+bi) $= \sqrt{a^2+b^2}$ as usual.

A very small patch... :)

(abs -3+4i)             => 5
(abs -3.0-4i)           => 5.0

Here's a list of how other lisps deal with (abs -3-4i):

System result remark
Bigloo no complexes
Biwa no complexes
Chez error
Chibi -3-4i leaves unchanged
Chicken error
Cyclone error
Gambit error
Gauche 5.0
Guile error
Kawa 5.0
LIPS error
MIT error
Racket error
Sagittarius 5 keeps exactness
STklos error
Unsyntax -3-4i leaves unchanged
ABCL 5.0
CCL 5 keeps exactness
Clisp 5 keeps exactness
ECL 5.0
GCL 5.0
SBCL 5.0
lassik commented 11 months ago

R7RS says the signature is (abs x). For the number procedures, x means a real number. z ("zahl") means any number.

Nevertheless, R7RS doesn't forbid procedures that work with more data types than is standard.

jpellegrini commented 11 months ago

R7RS says the signature is (abs x). For the number procedures, x means a real number. z ("zahl") means any number.

I haven't paid attention to the signature... Indeed! Anyway, @egallesio, here's the PR. Any decision seems to be fine! :)

jpellegrini commented 11 months ago

Hm, I think I hadn't had coffee when I proposed this PR. If one wants to make abs work for complexes, it would be simpler to do something like

(define abs magnitude)

No?

egallesio commented 10 months ago

I have applied your patch (and simplified the writing of magnitude, BTW)