1
0
Fork 0
cl-sites/novaspec.org/cl/f_sqrt.html
2025-02-05 18:52:26 +01:00

425 lines
No EOL
8.1 KiB
HTML

<!DOCTYPE HTML>
<HTML LANG="en-us"
><HEAD
><TITLE
>sqrt, isqrt | Common Lisp Nova Spec</TITLE
><META CHARSET="US-ASCII"
><LINK REL="canonical" HREF="f_sqrt.html"
><LINK REL="next" HREF="t_random-state.html" TYPE="text/html" TITLE="random-state"
><LINK REL="prev" HREF="f_signum.html" TYPE="text/html" TITLE="signum"
><LINK REL="up" HREF="12_2_Numbers_Dictionary.html" TYPE="text/html" TITLE="12.2 Numbers Dictionary"
><LINK REL="start" HREF="index.html" TYPE="text/html" TITLE="Common Lisp Nova Spec"
><META NAME="VIEWPORT" CONTENT="width=device-width, initial-scale=1.0"
><LINK REL="STYLESHEET" HREF="dpans.css%3F3909942064.css"
><SCRIPT SRC="dpans.js%3F3909942064"
></SCRIPT
><SCRIPT SRC="apropos.js%3F3909942064"
></SCRIPT
></HEAD
><BODY
><DIV
><DIV CLASS="topnav"
><DIV CLASS="breadcrumb"
><SPAN CLASS="breadcrumb-item"
><A HREF="index.html"
>Common Lisp Nova Spec</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="12_Numbers.html"
>12. Numbers</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="12_2_Numbers_Dictionary.html"
>12.2 Numbers Dictionary</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="f_sqrt.html"
>sqrt, isqrt</A
></SPAN
></DIV
><DIV CLASS="apropos"
><DIV CLASS="apropos-io"
><A HREF="f_signum.html" CLASS="prev"
>&#8592;</A
><SPAN ID="apropos-label"
>Apropos </SPAN
><INPUT ID="apropos" AUTOFOCUS="AUTOFOCUS" PLACEHOLDER="Type here to search" ONINPUT="aproposInput(this);" ONKEYUP="aproposKeyup(event);" ONCHANGE="aproposChange(this);" ONFOCUS="aproposFocus(this);" ONFOCUSOUT="aproposFocusout(this);"
><A HREF="t_random-state.html" CLASS="next"
>&#8594;</A
></DIV
><DIV ID="apropos-res"
></DIV
></DIV
></DIV
><DIV CLASS="matter"
><DIV CLASS="com"
><DIV CLASS="begincom"
><HR
><TABLE WIDTH="100%" CELLSPACING="0" CELLPADDING="0"
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" WIDTH="100%" CLASS="name"
><SPAN CLASS="idx" DATA-KIND="idxref" DATA-TERM="sqrt, isqrt"
></SPAN
><B
>sqrt, isqrt</B
></TD
><TD ALIGN="RIGHT" VALIGN="BASELINE" WIDTH="0" NOWRAP="NOWRAP" CLASS="ftype"
><I
>Function</I
></TD
></TR
></TABLE
><HR
></DIV
><UL CLASS="subtoc"
></UL
><DL
><DT
><B
>Syntax</B
></DT
><DD
><P CLASS="j"
><B
>sqrt</B
> <SPAN CLASS="cmssi"
>number</SPAN
> <SPAN CLASS="arrow"
>&#8594;</SPAN
> <SPAN CLASS="cmssi"
>root</SPAN
></P
><P CLASS="j"
><B
>isqrt</B
> <SPAN CLASS="cmssi"
>natural</SPAN
> <SPAN CLASS="arrow"
>&#8594;</SPAN
> <SPAN CLASS="cmssi"
>natural-root</SPAN
></P
></DD
><DT
><B
>Arguments and Values</B
></DT
><DD
><P CLASS="j"
><VAR CLASS="param"
>number</VAR
>, <VAR CLASS="param"
>root</VAR
> &#8212; a <A HREF="26_1_Glossary.html#number"
><EM CLASS="term"
>number</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>natural</VAR
>, <VAR CLASS="param"
>natural-root</VAR
> &#8212; a non-negative <A HREF="26_1_Glossary.html#integer"
><EM CLASS="term"
>integer</EM
></A
>.</P
></DD
><DT
><B
>Description</B
></DT
><DD
><P CLASS="j"
><A HREF="f_sqrt.html" CLASS="funref"
><B
>sqrt</B
></A
> and <A HREF="f_sqrt.html" CLASS="funref"
><B
>isqrt</B
></A
> compute square roots. </P
><P CLASS="j"
><A HREF="f_sqrt.html" CLASS="funref"
><B
>sqrt</B
></A
> returns the <A HREF="26_1_Glossary.html#principal"
><EM CLASS="term"
>principal</EM
></A
> square root of <VAR CLASS="param"
>number</VAR
>. If the <VAR CLASS="param"
>number</VAR
> is not a <A HREF="26_1_Glossary.html#complex"
><EM CLASS="term"
>complex</EM
></A
> but is negative, then the result is a <A HREF="26_1_Glossary.html#complex"
><EM CLASS="term"
>complex</EM
></A
>. </P
><P CLASS="j"
><A HREF="f_sqrt.html" CLASS="funref"
><B
>isqrt</B
></A
> returns the greatest <A HREF="26_1_Glossary.html#integer"
><EM CLASS="term"
>integer</EM
></A
> less than or equal to the exact positive square root of <VAR CLASS="param"
>natural</VAR
>. </P
><P CLASS="j"
>If <VAR CLASS="param"
>number</VAR
> is a positive <A HREF="26_1_Glossary.html#rational"
><EM CLASS="term"
>rational</EM
></A
>, it is <A HREF="26_1_Glossary.html#implementation-dependent"
><EM CLASS="term"
>implementation-dependent</EM
></A
> whether <VAR CLASS="param"
>root</VAR
> is a <A HREF="26_1_Glossary.html#rational"
><EM CLASS="term"
>rational</EM
></A
> or a <A HREF="26_1_Glossary.html#float"
><EM CLASS="term"
>float</EM
></A
>. If <VAR CLASS="param"
>number</VAR
> is a negative <A HREF="26_1_Glossary.html#rational"
><EM CLASS="term"
>rational</EM
></A
>, it is <A HREF="26_1_Glossary.html#implementation-dependent"
><EM CLASS="term"
>implementation-dependent</EM
></A
> whether <VAR CLASS="param"
>root</VAR
> is a <A HREF="26_1_Glossary.html#complex_rational"
><EM CLASS="term"
>complex rational</EM
></A
> or a <A HREF="26_1_Glossary.html#complex_float"
><EM CLASS="term"
>complex float</EM
></A
>. </P
><P CLASS="j"
>The mathematical definition of complex square root (whether or not minus zero is supported) follows: </P
><P CLASS="j"
><CODE CLASS="f"
>(sqrt <I CLASS="i"
><I
>x</I
></I
>) = (exp (/ (log <I CLASS="i"
><I
>x</I
></I
>) 2))</CODE
> </P
><P CLASS="j"
>The branch cut for square root lies along the negative real axis, continuous with quadrant II. The range consists of the right half-plane, including the non-negative imaginary axis and excluding the negative imaginary axis.</P
></DD
><DT
><B
>Examples</B
></DT
><DD
><PRE CLASS="screen"
> (sqrt 9.0) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 3.0
(sqrt -9.0) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> #C(0.0 3.0)
(isqrt 9) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 3
(sqrt 12) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 3.4641016
(isqrt 12) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 3
(isqrt 300) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 17
(isqrt 325) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 18
(sqrt 25)
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 5
<SPAN CLASS="sp"
><SPAN CLASS="arrow withabove"
><SPAN CLASS="above"
>or </SPAN
>&#8594;</SPAN
></SPAN
> 5.0
(isqrt 25) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 5
(sqrt -1) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> #C(0.0 1.0)
(sqrt #c(0 2)) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> #C(1.0 1.0)</PRE
></DD
><DT
><B
>Exceptional Situations</B
></DT
><DD
><P CLASS="j"
>The <A HREF="26_1_Glossary.html#function"
><EM CLASS="term"
>function</EM
></A
> <A HREF="f_sqrt.html" CLASS="funref"
><B
>sqrt</B
></A
> should signal <A HREF="t_type-error.html" CLASS="typeref"
><B
>type-error</B
></A
> if its argument is not a <A HREF="26_1_Glossary.html#number"
><EM CLASS="term"
>number</EM
></A
>. </P
><P CLASS="j"
>The <A HREF="26_1_Glossary.html#function"
><EM CLASS="term"
>function</EM
></A
> <A HREF="f_sqrt.html" CLASS="funref"
><B
>isqrt</B
></A
> should signal <A HREF="t_type-error.html" CLASS="typeref"
><B
>type-error</B
></A
> if its argument is not a non-negative <A HREF="26_1_Glossary.html#integer"
><EM CLASS="term"
>integer</EM
></A
>. </P
><P CLASS="j"
>The functions <A HREF="f_sqrt.html" CLASS="funref"
><B
>sqrt</B
></A
> and <A HREF="f_sqrt.html" CLASS="funref"
><B
>isqrt</B
></A
> might signal <A HREF="t_arithmetic-error.html" CLASS="typeref"
><B
>arithmetic-error</B
></A
>.</P
></DD
><DT
><B
>See Also</B
></DT
><DD
><P CLASS="j"
><A HREF="f_exp.html" CLASS="funref"
><B
>exp</B
></A
>, <A HREF="f_log.html" CLASS="funref"
><B
>log</B
></A
>, <A HREF="12_1_Number_Concepts.html#sec_12_1_3_3" CLASS="secref"
><SPAN CLASS="cmr"
>Section</SPAN
> <SPAN CLASS="cmr"
>12.1.3.3</SPAN
> <SPAN CLASS="cmr"
>(Rule</SPAN
> <SPAN CLASS="cmr"
>of</SPAN
> <SPAN CLASS="cmr"
>Float</SPAN
> <SPAN CLASS="cmr"
>Substitutability)</SPAN
></A
></P
></DD
><DT
><B
>Notes</B
></DT
><DD
><PRE CLASS="screen"
>(isqrt x) <SPAN CLASS="cmsy"
><SPAN STYLE="font-size:16px;vertical-align:-2px"
>&#8801;</SPAN
></SPAN
> (values (floor (sqrt x)))</PRE
><P CLASS="j"
>but it is potentially more efficient.</P
></DD
></DL
></DIV
></DIV
><DIV CLASS="footer"
><DIV CLASS="btmnav"
><A HREF="f_signum.html" CLASS="prev"
>&#8592;</A
><A HREF="t_random-state.html" CLASS="next"
>&#8594;</A
></DIV
><DIV CLASS="trail"
>Conversion to HTML copyright 2023 by Gilbert Baumann</DIV
></DIV
></DIV
><SCRIPT
>domReady();</SCRIPT
></BODY
></HTML
>