1
0
Fork 0
cl-sites/novaspec.org/cl/f_rational.html

320 lines
6.3 KiB
HTML
Raw Normal View History

2025-02-05 18:52:26 +01:00
<!DOCTYPE HTML>
<HTML LANG="en-us"
><HEAD
><TITLE
>rational, rationalize | Common Lisp Nova Spec</TITLE
><META CHARSET="US-ASCII"
><LINK REL="canonical" HREF="f_rational.html"
><LINK REL="next" HREF="f_rationalp.html" TYPE="text/html" TITLE="rationalp"
><LINK REL="prev" HREF="f_numerator.html" TYPE="text/html" TITLE="numerator, denominator"
><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_rational.html"
>rational, rationalize</A
></SPAN
></DIV
><DIV CLASS="apropos"
><DIV CLASS="apropos-io"
><A HREF="f_numerator.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="f_rationalp.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="rational, rationalize"
></SPAN
><B
>rational, rationalize</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
>rational</B
> <SPAN CLASS="cmssi"
>number</SPAN
> <SPAN CLASS="arrow"
>&#8594;</SPAN
> <SPAN CLASS="cmssi"
>rational</SPAN
></P
><P CLASS="j"
><B
>rationalize</B
> <SPAN CLASS="cmssi"
>number</SPAN
> <SPAN CLASS="arrow"
>&#8594;</SPAN
> <SPAN CLASS="cmssi"
>rational</SPAN
></P
></DD
><DT
><B
>Arguments and Values</B
></DT
><DD
><P CLASS="j"
><VAR CLASS="param"
>number</VAR
> &#8212; a <EM CLASS="term"
>real</EM
>. </P
><P CLASS="j"
><VAR CLASS="param"
>rational</VAR
> &#8212; a <A HREF="26_1_Glossary.html#rational"
><EM CLASS="term"
>rational</EM
></A
>.</P
></DD
><DT
><B
>Description</B
></DT
><DD
><P CLASS="j"
><A HREF="f_rational.html" CLASS="funref"
><B
>rational</B
></A
> and <A HREF="f_rational.html" CLASS="funref"
><B
>rationalize</B
></A
> convert <EM CLASS="term"
>reals</EM
> to <A HREF="26_1_Glossary.html#rational"
><EM CLASS="term"
>rationals</EM
></A
>. </P
><P CLASS="j"
>If <VAR CLASS="param"
>number</VAR
> is already <A HREF="26_1_Glossary.html#rational"
><EM CLASS="term"
>rational</EM
></A
>, it is returned. </P
><P CLASS="j"
>If <VAR CLASS="param"
>number</VAR
> is a <A HREF="26_1_Glossary.html#float"
><EM CLASS="term"
>float</EM
></A
>, <A HREF="f_rational.html" CLASS="funref"
><B
>rational</B
></A
> returns a <A HREF="26_1_Glossary.html#rational"
><EM CLASS="term"
>rational</EM
></A
> that is mathematically equal in value to the <A HREF="26_1_Glossary.html#float"
><EM CLASS="term"
>float</EM
></A
>. <A HREF="f_rational.html" CLASS="funref"
><B
>rationalize</B
></A
> returns a <A HREF="26_1_Glossary.html#rational"
><EM CLASS="term"
>rational</EM
></A
> that approximates the <A HREF="26_1_Glossary.html#float"
><EM CLASS="term"
>float</EM
></A
> to the accuracy of the underlying floating-point representation. </P
><P CLASS="j"
><A HREF="f_rational.html" CLASS="funref"
><B
>rational</B
></A
> assumes that the <A HREF="26_1_Glossary.html#float"
><EM CLASS="term"
>float</EM
></A
> is completely accurate. </P
><P CLASS="j"
><A HREF="f_rational.html" CLASS="funref"
><B
>rationalize</B
></A
> assumes that the <A HREF="26_1_Glossary.html#float"
><EM CLASS="term"
>float</EM
></A
> is accurate only to the precision of the floating-point representation.</P
></DD
><DT
><B
>Examples</B
></DT
><DD
><PRE CLASS="screen"
>(rational 0) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 0
(rationalize -11/100) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> -11/100
(rational .1) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 13421773/134217728 ;implementation-dependent
(rationalize .1) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 1/10</PRE
></DD
><DT
><B
>Affected By</B
></DT
><DD
><P CLASS="j"
>The <A HREF="26_1_Glossary.html#implementation"
><EM CLASS="term"
>implementation</EM
></A
>.</P
></DD
><DT
><B
>Exceptional Situations</B
></DT
><DD
><P CLASS="j"
>Should signal an error of <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> <A HREF="t_type-error.html" CLASS="typeref"
><B
>type-error</B
></A
> if <VAR CLASS="param"
>number</VAR
> is not a <EM CLASS="term"
>real</EM
>. Might signal <A HREF="t_arithmetic-error.html" CLASS="typeref"
><B
>arithmetic-error</B
></A
>.</P
></DD
><DT
><B
>Notes</B
></DT
><DD
><P CLASS="j"
>It is always the case that</P
><PRE CLASS="screen"
>(float (rational x) x) <SPAN CLASS="cmsy"
><SPAN STYLE="font-size:16px;vertical-align:-2px"
>&#8801;</SPAN
></SPAN
> x</PRE
><P CLASS="j"
>and</P
><PRE CLASS="screen"
>(float (rationalize x) x) <SPAN CLASS="cmsy"
><SPAN STYLE="font-size:16px;vertical-align:-2px"
>&#8801;</SPAN
></SPAN
> x</PRE
><P CLASS="j"
>That is, rationalizing a <A HREF="26_1_Glossary.html#float"
><EM CLASS="term"
>float</EM
></A
> by either method and then converting it back to a <A HREF="26_1_Glossary.html#float"
><EM CLASS="term"
>float</EM
></A
> of the same format produces the original <VAR CLASS="param"
>number</VAR
>.</P
></DD
></DL
></DIV
></DIV
><DIV CLASS="footer"
><DIV CLASS="btmnav"
><A HREF="f_numerator.html" CLASS="prev"
>&#8592;</A
><A HREF="f_rationalp.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
>