make-rectangular(3scm)

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

πŸ’… 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+2i gets automatically turned into 1.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.0 does 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