sqrt(3scm) Scheme Programmer's Manual sqrt(3scm)

sqrt, flsqrt - principal square root

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

(sqrt z)
(flsqrt fl)                         ;R6RS

Returns the principal square root of z.
Rational arguments
For rational z, the result has either positive real part, or zero real part and non-negative imaginary part.
Non-real arguments
Let the value of log z for non-real z be defined in terms of log on real numbers as

log z = log |z| + (angle z)i

The value of (sqrt z) can then be expressed as e**((log z)/2).

Exact arguments
The sqrt procedure may return an inexact result even when given an exact argument.
Flonum variant
flsqrt returns the principal square root of fl. For -0.0 it should return −0.0; for other negative arguments, the result may be a NaN or some unspecified flonum.

Chez Scheme
For exact arguments where the principal square root is an integer, Chez Scheme returns an exact integer.

Returns a single value; a number object.

(sqrt 9)         =>  3
(sqrt -1)        =>  +i
(sqrt -5)        =>  0.0+2.23606797749979i ; approximately
(sqrt +inf.0)    =>  +inf.0
(sqrt -inf.0)    =>  +inf.0i
(flsqrt +inf.0)  =>  +inf.0
(flsqrt -0.0)    =>  -0.0

The sqrt procedure works mostly the same everywhere. How exact arguments are handled differs between implementations. R7RS implementations are not required to support all number types and may e.g. omit support for complex numbers.

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.
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.

expt(3scm), log(3scm)

R4RS, IEEE Scheme, R5RS, R6RS, R7RS

This procedure first appeared in R2RS, which got it from Common Lisp.

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

2022-05-08