make-rectangular(3scm)
π make-rectangular(3scm) β the Scheme Programmer's Manual, bestie edition
okay so this is a page from the Scheme Programmer's Manual and it's about building complex numbers from scratch. two little procedures, so much power. let's go π«Ά
β¨ NAME β¨
make-rectangular, make-polar β construct complex numbers
π LIBRARY π
the imports, exactly as they appear, no notes:
(import (rnrs)) ;R6RS
(import (rnrs base)) ;R6RS
(import (scheme r5rs)) ;R7RS
(import (scheme complex)) ;R7RS
π SYNOPSIS π
(make-rectangular x y)
(make-polar m a)
π DESCRIPTION π
So make-rectangular hands you back the complex number whose real part is x and whose imaginary part is y. iconic behavior honestly.
And make-polar hands you back the complex number with approximate magnitude m and approximate angle a. yes queen, polar coordinates, we stan π
π₯ IMPLEMENTATION NOTES π₯
this is where it gets juicy, bestie, there are variations:
- GNU Guile 3.0 β Guile literally has no way to represent exact complex numbers with non-zero imaginary parts π which means
1+2igets automatically turned into1.0+2.0i. it's a deviation from R6RS but R7RS is cool with it, so we move. - Loko Scheme β Loko Scheme can internally represent complex numbers in polar notation, and it keeps the polar notation around when that's convenient. and why? mostly as a joke π it's not something you'd normally find in Scheme at all. most implementations just convert the polar notation into an inexact rectangular representation like a normal person.
Also! the real numbers, the rational numbers and the integers are all a subset of the complex numbers. and many implementations will simplify the return type when one of y, m, and a is exact zero. little favor, no cap.
π RETURN VALUES π
Both procedures return a single value: a complex number. If either argument is inexact then the returned number is inexact. and heads up β make-polar may return inexact results even when the arguments are exact. yes it can do that, don't come for it π
πΈ EXAMPLES πΈ
(make-rectangular 1 2)
=> 1+2i
(make-rectangular 1.1 2.2)
=> 1.1+2.2i ; approximately
(make-polar 1.1 2.2)
=> 1.1@2.2 ; approximately
(that ; approximately is doing so much heavy lifting, shoutout to it)
π¦ COMPATIBILITY π¦
These procedures, where they are implemented, are intended to achieve the same result: return the mathematically correct result to the extent allowed by the implementation. Some variations between implementations are noted above (scroll up, it's the Guile and Loko section, you got this β¨).
Support for these procedures is required by R6RS. There are however still variations in which types of numbers that can be represented. Some of the possible variations are:
- IEEE 754 floats are not required
-0.0does not need to be distinct- the precision of inexact numbers can vary
- numbers in polar notation can be turned into inexact numbers in rectangular notation
Support for complex numbers is not guaranteed at all in R7RS. not at all. zero promises. π
An R7RS implementation's support can be tested with cond-expand(7scm):
exact-complexβ Exact complex numbers are provided. (Note that this refers to the rectangular representation only).ieee-floatβ Inexact numbers are IEEE 754 binary floating point values.
π 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. In particular, all arguments must be real numbers.- 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. π yeah. catastrophically. that's the word they used.
- R7RS and unsupported numbers β If an implementation encounters an exact numerical constant that it cannot represent as an exact number, then it may either report a violation of an implementation restriction or it may silently represent the constant by an inexact number. sneaky but legal.
β SEE ALSO β
exp(3scm), sin(3scm), angle(3scm), real-part(3scm)
π STANDARDS π
R4RS, IEEE Scheme, R5RS, R6RS, R7RS β the whole family, they're all here
π§ HISTORY π§
These procedures first appeared in R2RS, which used Common Lisp as inspiration for its number system. and fun fact for your next group chat: the make-rectangular procedure is called complex in Common Lisp. iykyk π
π 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 page, bestie! this lil tour was brought to you by the Scheme Programmer's Manual, dated 2023-02-18, from make-rectangular(3scm) β stay complex, stay exact when you can, and remember: make-polar might go inexact on you and that's just its vibe πβ¨
π¦ 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