📄 r5rs-z-h-9.html
字号:
Implementations may also support only a limited range of numbers ofany type, subject to the requirements of this section. The supportedrange for exact numbers of any type may be different from thesupported range for inexact numbers of that type. For example,an implementation that uses flonums to represent all itsinexact real numbers maysupport a practically unbounded range of exact integersand rationalswhile limiting the range of inexact reals (and thereforethe range of inexact integers and rationals)to the dynamic range of the flonum format.Furthermorethe gaps between the representable inexact integers andrationals arelikely to be very large in such an implementation as the limits of thisrange are approached.<p>An implementation of Scheme must support exact integersthroughout the range of numbers that may be used for indexes oflists, vectors, and strings or that may result from computing the length of alist, vector, or string. The <tt>length</tt>, <tt>vector-length</tt>,and <tt>string-length</tt> procedures must return an exactinteger, and it is an error to use anything but an exact integer as anindex. Furthermore any integer constant within the index range, ifexpressed by an exact integer syntax, will indeed be read as an exactinteger, regardless of any implementation restrictions that may applyoutside this range. Finally, the procedures listed below will alwaysreturn an exact integer result provided all their arguments are exact integersand the mathematically expected result is representable as an exact integerwithin the implementation:<p><tt><p>+ - *<br>quotient remainder modulo<br>max min abs<br>numerator denominator gcd<br>lcm floor ceiling<br>truncate round rationalize<br>expt<p></tt><p>Implementations are encouraged, but not required, to supportexact integers and exact rationals ofpractically unlimited size and precision, and to implement theabove procedures and the <tt>/</tt> procedure insuch a way that they always return exact results when given exactarguments. If one of these procedures is unable to deliver an exactresult when given exact arguments, then it may either report aviolation of animplementation restriction or it may silently coerce its result to aninexact number. Such a coercion may cause an error later.<p>An implementation may use floating point and other approximate representation strategies for inexact numbers.This report recommends, but does not require, that the IEEE 32-bitand 64-bit floating point standards be followed by implementations that useflonum representations, and that implementations usingother representations should match or exceed the precision achievableusing these floating point standards [<a href="r5rs-Z-H-14.html#%_sec_7.3">12</a>].<p>In particular, implementations that use flonum representationsmust follow these rules: A flonum resultmust be represented with at least as much precision as is used to express any ofthe inexact arguments to that operation. It is desirable (but not required) forpotentially inexact operations such as <tt>sqrt</tt>, when applied to exactarguments, to produce exact answers whenever possible (for example thesquare root of an exact 4 ought to be an exact 2).If, however, anexact number is operated upon so as to produce an inexact result(as by <tt>sqrt</tt>), and if the result is represented as a flonum, thenthe most precise flonum format available must be used; but if the resultis represented in some other way then the representation must have at least asmuch precision as the most precise flonum format available.<p>Although Scheme allows a variety of writtennotations fornumbers, any particular implementation may support only some of them.For example, an implementation in which all numbers are realneed not support the rectangular and polar notations for complexnumbers. If an implementation encounters an exact numerical constant thatit cannot represent as an exact number, then it may either report aviolation of an implementation restriction or it may silently represent theconstant by an inexact number.<p><a name="%_sec_6.2.4"></a><h3><a href="r5rs-Z-H-2.html#%_toc_%_sec_6.2.4">6.2.4 Syntax of numerical constants</a></h3><p><p>The syntax of the written representations for numbers is described formally insection <a href="r5rs-Z-H-10.html#%_sec_7.1.1">7.1.1</a>. Note that case is not significant in numericalconstants.<p>A number may be written in binary, octal, decimal, orhexadecimal by the use of a radix prefix. The radix prefixes are <tt>#b</tt><a name="%_idx_228"></a> (binary), <tt>#o</tt><a name="%_idx_230"></a> (octal), <tt>#d</tt><a name="%_idx_232"></a> (decimal), and <tt>#x</tt><a name="%_idx_234"></a> (hexadecimal). Withno radix prefix, a number is assumed to be expressed in decimal.<p>Anumerical constant may be specified to be either exact orinexact by a prefix. The prefixes are <tt>#e</tt><a name="%_idx_236"></a>for exact, and <tt>#i</tt><a name="%_idx_238"></a> for inexact. An exactnessprefix may appear before or after any radix prefix that is used. Ifthe written representation of a number has no exactness prefix, theconstant may be either inexact or exact. It isinexact if it contains a decimal point, anexponent, or a ``<tt>#</tt>'' character in the place of a digit,otherwise it is exact.In systems with inexact numbersof varying precisions it may be useful to specifythe precision of a constant. For this purpose, numerical constantsmay be written with an exponent marker that indicates thedesired precision of the inexactrepresentation. The letters <tt>s</tt>, <tt>f</tt>,<tt>d</tt>, and <tt>l</tt> specify the use of <i>short</i>, <i>single</i>,<i>double</i>, and <i>long</i> precision, respectively. (When fewerthan four internalinexactrepresentations exist, the four sizespecifications are mapped onto those available. For example, animplementation with two internal representations may map short andsingle together and long and double together.) In addition, theexponent marker <tt>e</tt> specifies the default precision for theimplementation. The default precision has at least as much precisionas <i>double</i>, butimplementations may wish to allow this default to be set by the user.<p><tt><p>3.14159265358979F0<br> Round to single --- 3.141593<br>0.6L0<br> Extend to long --- .600000000000000<p></tt><p><a name="%_sec_6.2.5"></a><h3><a href="r5rs-Z-H-2.html#%_toc_%_sec_6.2.5">6.2.5 Numerical operations</a></h3><p>The reader is referred to section <a href="r5rs-Z-H-4.html#%_sec_1.3.3">1.3.3</a> for a summaryof the naming conventions used to specify restrictions on the types ofarguments to numerical routines.The examples used in this section assume that any numerical constant writtenusing an exact notation is indeed represented as an exactnumber. Some examples also assume that certain numerical constants writtenusing an inexact notation can be represented without loss ofaccuracy; the inexact constants were chosen so that this islikely to be true in implementations that use flonums to representinexact numbers.<p><p><p><div align=left><u>procedure:</u> <tt>(<a name="%_idx_240"></a>number?<i> obj</i>)</tt> </div><div align=left><u>procedure:</u> <tt>(<a name="%_idx_242"></a>complex?<i> obj</i>)</tt> </div><div align=left><u>procedure:</u> <tt>(<a name="%_idx_244"></a>real?<i> obj</i>)</tt> </div><div align=left><u>procedure:</u> <tt>(<a name="%_idx_246"></a>rational?<i> obj</i>)</tt> </div><div align=left><u>procedure:</u> <tt>(<a name="%_idx_248"></a>integer?<i> obj</i>)</tt> </div><p>These numerical type predicates can be applied to any kind ofargument, including non-numbers. They return <tt>#t</tt> if the object isof the named type, and otherwise they return <tt>#f</tt>.In general, if a type predicate is true of a number then all highertype predicates are also true of that number. Consequently, if a typepredicate is false of a number, then all lower type predicates arealso false of that number.If <em>z</em> is an inexact complex number, then <tt>(real? <em>z</em>)</tt> is true ifand only if <tt>(zero? (imag-part <em>z</em>))</tt> is true. If <em>x</em> is an inexactreal number, then <tt>(integer? <em>x</em>)</tt> is true if and only if<tt>(= <em>x</em> (round <em>x</em>))</tt>.<p><tt><p>(complex? 3+4i) ===> <tt>#t</tt><br>(complex? 3) ===> <tt>#t</tt><br>(real? 3) ===> <tt>#t</tt><br>(real? -2.5+0.0i) ===> <tt>#t</tt><br>(real? #e1e10) ===> <tt>#t</tt><br>(rational? 6/10) ===> <tt>#t</tt><br>(rational? 6/3) ===> <tt>#t</tt><br>(integer? 3+0i) ===> <tt>#t</tt><br>(integer? 3.0) ===> <tt>#t</tt><br>(integer? 8/4) ===> <tt>#t</tt><p></tt><p><blockquote><em>Note: </em>The behavior of these type predicates on inexact numbersis unreliable, since any inaccuracy may affect the result.</blockquote><p><blockquote><em>Note: </em>In many implementations the <tt>rational?</tt> procedure will be the sameas <tt>real?</tt>, and the <tt>complex?</tt> procedure will be the same as<tt>number?</tt>, but unusual implementations may be able to representsome irrational numbers exactly or may extend the number system tosupport some kind of non-complex numbers.</blockquote><p><p><p><p><div align=left><u>procedure:</u> <tt>(<a name="%_idx_250"></a>exact?<i> <em>z</em></i>)</tt> </div><div align=left><u>procedure:</u> <tt>(<a name="%_idx_252"></a>inexact?<i> <em>z</em></i>)</tt> </div><p>These numerical predicates provide tests for the exactness of a
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -