magnitude(3scm)
π 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.0erasure π that; if -0.0 is distinguishednote 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
- nixpkgs unstable: scheme-manpages 0-unstable-2024-02-11
- π the page's license: MIT
- Copyright (c) 2023 Gwen Weinholt
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms