magnitude(3scm)

πŸ“¦ version 0-unstable-2024-02, in nixpkgs unstable

πŸ’… magnitude(3scm) β€” the Scheme Programmer's Manual, bestie edition πŸ’…

okay so this is magnitude(3scm) from the Scheme Programmer's Manual, and we're about to get so comfy with complex numbers ✨


🌸 NAME 🌸

magnitude, angle β€” the magnitude and angle of complex numbers, aka polar-form besties 🧚

πŸ“š LIBRARY πŸ“š

import it from wherever you're living these days, no judgement:

(import (rnrs))                     ;R6RS
(import (rnrs base))                ;R6RS
(import (scheme r5rs))              ;R7RS
(import (scheme complex))           ;R7RS

✨ SYNOPSIS ✨

(magnitude z)
(angle z)

πŸ’– DESCRIPTION πŸ’–

magnitude gives you the magnitude of z, and angle gives you the angle of z. Respectively. That's the whole vibe, they each return one and only one of those 🌈

🫢 IMPLEMENTATION NOTES 🫢

so here's the tea β˜• β€” most implementations of complex numbers do not store them in polar form, they store them in rectangular form instead. Which means magnitude and angle gotta do a little math homework to get what you asked for:

The magnitude can be found using the equation for Euclidean distance:

(sqrt (+ (expt (real-part z) 2) (expt (imag-part z) 2)))

The angle can be found using atan:

(atan (imag-part z) (real-part z))

like, it's giving Pythagoras but make it scheme πŸ’…

πŸ”₯ RETURN VALUES πŸ”₯

These procedures return a single value; a real number. One value, one real number, no extra drama πŸŽ€

🌟 EXAMPLES 🌟

(angle -1)       =>  3.141592653589793   ; approximately
(magnitude 1+i)  =>  1.4142135623730951  ; approximately

(angle -1.0+0.0i)
        =>  3.141592653589793   ; approximately
(angle -1.0-0.0i)
        =>  -3.141592653589793  ; approximately
        ; if -0.0 is distinguished

(angle +inf.0)  =>  0.0
(angle -inf.0)  =>  3.141592653589793  ; approximately

(angle 0)        =>  &assertion raised
(magnitude 0)    =>  0
not the -0.0 erasure 😭 that ; if -0.0 is distinguished note is doing so much heavy lifting β€” lowkey iconic that the sign of zero can flip the whole answer, but only if your implementation even cares about distinguishing -0.0. iykyk 🫢

and yeah, (angle 0) straight up throws &assertion while (magnitude 0) happily gives you 0. no cap, that's just how it is πŸ’€

πŸ¦‹ COMPATIBILITY πŸ¦‹

These procedures work the same everywhere that they are supported. Fr fr, consistent queen behavior πŸ‘‘

heads up though β€” R7RS implementations may not have support for complex numbers at all. so like, vibe check your implementation before you go off πŸ™ˆ

πŸ’€ ERRORS πŸ’€

This procedure can raise exceptions with the following condition types:

  • &assertion (R6RS) β€” the wrong number of arguments was passed, or an argument was outside its domain. basically: you fumbled the call βœ‹
  • R7RS β€” the assertions described above are errors. Implementations may signal an error, extend the procedure's domain of definition to include such arguments, or fail catastrophically. yes, catastrophically 😭 that's not a drill, that's the actual vocabulary they chose πŸ’…

πŸ‘‘ SEE ALSO πŸ‘‘

go read those next, they're besties with this page 🫢

πŸŽ€ STANDARDS πŸŽ€

R4RS, IEEE Scheme, R5RS, R6RS, R7RS β€” certified across five whole standards, ate and left no crumbs ⭐

🌈 HISTORY 🌈

These procedures first appeared in R2RS, which used Common Lisp as inspiration for its number system. And in Common Lisp these procedures are called abs and phase. so if you came from the CL side of the timeline, that's your translation key 🧚

πŸ’œ AUTHORS πŸ’œ

This page is part of the scheme-manpages project. It includes materials from the RnRS documents. More information can be found at https://github.com/schemedoc/manpages/.


and that's the whole page, bestie!! πŸŽ€ written 2023-03-14, straight from the scheme-manpages crew (version vibes and all) β€” mwah, go compute some magnitudes πŸ’–βœ¨πŸ¦‹

πŸ“¦ where this page comes from