emacs.d/clones/lisp/www.cs.cmu.edu/Groups/AI/html/cltl/clm/node18.html

80 lines
4.2 KiB
HTML
Raw Normal View History

2022-08-26 19:11:35 +02:00
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
<!Converted with LaTeX2HTML 0.6.5 (Tue Nov 15 1994) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
<HEAD>
<TITLE>2.1.2. Ratios</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Ratios">
<meta name="keywords" value="clm">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<b>Common Lisp the Language, 2nd Edition</b>
<BR> <HR><A NAME=tex2html1733 HREF="node19.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html1731 HREF="node16.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html1725 HREF="node17.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html1735 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html1736 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html1734 HREF="node19.html"> Floating-Point Numbers</A>
<B>Up:</B> <A NAME=tex2html1732 HREF="node16.html"> Numbers</A>
<B> Previous:</B> <A NAME=tex2html1726 HREF="node17.html"> Integers</A>
<HR> <P>
<H2><A NAME=SECTION00612000000000000000>2.1.2. Ratios</A></H2>
<P>
<A NAME=591>A</A>
<A NAME=592><i>ratio</i></A>
is a number representing the mathematical ratio
of two integers. Integers and ratios collectively constitute
the type <tt>rational</tt>.
The canonical representation of a rational number is as an
integer if its value is integral, and otherwise as the ratio of two
integers, the <i>numerator</i> and <i>denominator</i>, whose greatest
common divisor is 1, and of which the denominator is positive (and in
fact greater than 1, or else the value would be integral).
A ratio is notated with
<tt>/</tt> as a separator, thus: <tt>3/5</tt>. It is possible to notate
ratios in non-canonical (unreduced) forms, such as <tt>4/6</tt>, but the
Lisp function <tt>prin1</tt> always prints the canonical form for a
ratio.
<P>
If any computation produces a result that is a ratio of
two integers such that the denominator evenly divides the
numerator, then the result is immediately converted to the equivalent
integer. This is called the rule of <i>rational canonicalization</i>.
<P>
Rational numbers may be written as the possibly signed quotient of
decimal numerals: an optional sign followed by two non-empty sequences of
digits separated by a <tt>/</tt>. This syntax may be described as
follows:
<PRE><TT> <i>ratio</i> ::= [<i>sign</i>] {<i>digit</i>}+ <tt>/</tt> {<i>digit</i>}+
<P>
</TT></PRE>
The second sequence may not consist
entirely of zeros.
For example:
<P><pre>
2/3 ;This is in canonical form
4/6 ;A non-canonical form for the same number
-17/23 ;A not very interesting ratio
-30517578125/32768 ;This is <IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap40209.gif">
10/5 ;The canonical form for this is <tt>2</tt>
</pre>
<P>
To notate rational numbers in radices other than ten,
one uses the same radix specifiers
(one of <tt>#<i>nn</i>R</tt>, <tt>#O</tt>, <tt>#B</tt>, or <tt>#X</tt>) as for integers.
For example:
<P><pre>
#o-101/75 ;Octal notation for <tt>-65/61</tt>
#3r120/21 ;Ternary notation for <tt>15/7</tt>
#Xbc/ad ;Hexadecimal notation for <tt>188/173</tt>
#xFADED/FACADE ;Hexadecimal notation for <tt>1027565/16435934</tt>
</pre>
<P>
<BR> <HR><A NAME=tex2html1733 HREF="node19.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html1731 HREF="node16.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html1725 HREF="node17.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html1735 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html1736 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html1734 HREF="node19.html"> Floating-Point Numbers</A>
<B>Up:</B> <A NAME=tex2html1732 HREF="node16.html"> Numbers</A>
<B> Previous:</B> <A NAME=tex2html1726 HREF="node17.html"> Integers</A>
<HR> <P>
<HR>
<P><ADDRESS>
AI.Repository@cs.cmu.edu
</ADDRESS>
</BODY>