⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 library_28.html

📁 Linux程序员的工作手册
💻 HTML
📖 第 1 页 / 共 4 页
字号:
For example, when you specify the type of a function argument in afunction prototype, it makes a difference which one you use.  If thesystem header files declare <CODE>malloc</CODE> with an argument of type<CODE>size_t</CODE> and you declare <CODE>malloc</CODE> with an argument of type<CODE>unsigned int</CODE>, you will get a compilation error if <CODE>size_t</CODE>happens to be <CODE>unsigned long int</CODE> on your system.  To avoid anypossibility of error, when a function argument or value is supposed tohave type <CODE>size_t</CODE>, never declare its type in any other way.<P><STRONG>Compatibility Note:</STRONG> Pre-ANSI C implementations generally used<CODE>unsigned int</CODE> for representing object sizes and <CODE>int</CODE> forpointer subtraction results.  They did not necessarily define either<CODE>size_t</CODE> or <CODE>ptrdiff_t</CODE>.  Unix systems did define<CODE>size_t</CODE>, in <TT>`sys/types.h'</TT>, but the definition was usually asigned type.<P><H2><A NAME="SEC484" HREF="library_toc.html#SEC484" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC484">Data Type Measurements</A></H2><P>Most of the time, if you choose the proper C data type for each objectin your program, you need not be concerned with just how it isrepresented or how many bits it uses.  When you do need suchinformation, the C language itself does not provide a way to get it.The header files <TT>`limits.h'</TT> and <TT>`float.h'</TT> contain macroswhich give you this information in full detail.<P><A NAME="IDX1953"></A><A NAME="IDX1954"></A><A NAME="IDX1955"></A><H3><A NAME="SEC485" HREF="library_toc.html#SEC485" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC485">Computing the Width of an Integer Data Type</A></H3><P>The most common reason that a program needs to know how many bits are inan integer type is for using an array of <CODE>long int</CODE> as a bit vector.You can access the bit at index <VAR>n</VAR> with<P><PRE>vector[<VAR>n</VAR> / LONGBITS] &#38; (1 &#60;&#60; (<VAR>n</VAR> % LONGBITS))</PRE><P>provided you define <CODE>LONGBITS</CODE> as the number of bits in a<CODE>long int</CODE>.<A NAME="IDX1956"></A><P>There is no operator in the C language that can give you the number ofbits in an integer data type.  But you can compute it from the macro<CODE>CHAR_BIT</CODE>, defined in the header file <TT>`limits.h'</TT>.<P><DL COMPACT><DT><CODE>CHAR_BIT</CODE><DD>This is the number of bits in a <CODE>char</CODE>---eight, on most systems.The value has type <CODE>int</CODE>.<P>You can compute the number of bits in any data type <VAR>type</VAR> likethis:<P><PRE>sizeof (<VAR>type</VAR>) * CHAR_BIT</PRE></DL><P><A NAME="IDX1957"></A><A NAME="IDX1958"></A><A NAME="IDX1959"></A><H3><A NAME="SEC486" HREF="library_toc.html#SEC486" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC486">Range of an Integer Type</A></H3><P>Suppose you need to store an integer value which can range from zero toone million.  Which is the smallest type you can use?  There is nogeneral rule; it depends on the C compiler and target machine.  You canuse the <SAMP>`MIN'</SAMP> and <SAMP>`MAX'</SAMP> macros in <TT>`limits.h'</TT> to determinewhich type will work.<P>Each signed integer type has a pair of macros which give the smallestand largest values that it can hold.  Each unsigned integer type has onesuch macro, for the maximum value; the minimum value is, of course,zero.<P>The values of these macros are all integer constant expressions.  The<SAMP>`MAX'</SAMP> and <SAMP>`MIN'</SAMP> macros for <CODE>char</CODE> and <CODE>shortint</CODE> types have values of type <CODE>int</CODE>.  The <SAMP>`MAX'</SAMP> and<SAMP>`MIN'</SAMP> macros for the other types have values of the same typedescribed by the macro--thus, <CODE>ULONG_MAX</CODE> has type<CODE>unsigned long int</CODE>.<P><DL COMPACT><DT><CODE>SCHAR_MIN</CODE><DD><P>This is the minimum value that can be represented by a <CODE>signed char</CODE>.<P><DT><CODE>SCHAR_MAX</CODE><DD><DT><CODE>UCHAR_MAX</CODE><DD><P>These are the maximum values that can be represented by a<CODE>signed char</CODE> and <CODE>unsigned char</CODE>, respectively.<P><DT><CODE>CHAR_MIN</CODE><DD><P>This is the minimum value that can be represented by a <CODE>char</CODE>.It's equal to <CODE>SCHAR_MIN</CODE> if <CODE>char</CODE> is signed, or zerootherwise.<P><DT><CODE>CHAR_MAX</CODE><DD><P>This is the maximum value that can be represented by a <CODE>char</CODE>.It's equal to <CODE>SCHAR_MAX</CODE> if <CODE>char</CODE> is signed, or<CODE>UCHAR_MAX</CODE> otherwise.<P><DT><CODE>SHRT_MIN</CODE><DD><P>This is the minimum value that can be represented by a <CODE>signedshort int</CODE>.  On most machines that the GNU C library runs on,<CODE>short</CODE> integers are 16-bit quantities.<P><DT><CODE>SHRT_MAX</CODE><DD><DT><CODE>USHRT_MAX</CODE><DD><P>These are the maximum values that can be represented by a<CODE>signed short int</CODE> and <CODE>unsigned short int</CODE>,respectively.<P><DT><CODE>INT_MIN</CODE><DD><P>This is the minimum value that can be represented by a <CODE>signedint</CODE>.  On most machines that the GNU C system runs on, an <CODE>int</CODE> isa 32-bit quantity.<P><DT><CODE>INT_MAX</CODE><DD><DT><CODE>UINT_MAX</CODE><DD><P>These are the maximum values that can be represented by, respectively,the type <CODE>signed int</CODE> and the type <CODE>unsigned int</CODE>.<P><DT><CODE>LONG_MIN</CODE><DD><P>This is the minimum value that can be represented by a <CODE>signedlong int</CODE>.  On most machines that the GNU C system runs on, <CODE>long</CODE>integers are 32-bit quantities, the same size as <CODE>int</CODE>.<P><DT><CODE>LONG_MAX</CODE><DD><DT><CODE>ULONG_MAX</CODE><DD><P>These are the maximum values that can be represented by a<CODE>signed long int</CODE> and <CODE>unsigned long int</CODE>, respectively.<P><DT><CODE>LONG_LONG_MIN</CODE><DD><P>This is the minimum value that can be represented by a <CODE>signedlong long int</CODE>.  On most machines that the GNU C system runs on,<CODE>long long</CODE> integers are 64-bit quantities.<P><DT><CODE>LONG_LONG_MAX</CODE><DD><DT><CODE>ULONG_LONG_MAX</CODE><DD><P>These are the maximum values that can be represented by a <CODE>signedlong long int</CODE> and <CODE>unsigned long long int</CODE>, respectively.<P><DT><CODE>WCHAR_MAX</CODE><DD><P>This is the maximum value that can be represented by a <CODE>wchar_t</CODE>.@xref{Wide Character Intro}.</DL><P>The header file <TT>`limits.h'</TT> also defines some additional constantsthat parameterize various operating system and file system limits.  Theseconstants are described in section <A HREF="library_27.html#SEC454" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_27.html#SEC454">System Configuration Parameters</A>.<P><A NAME="IDX1960"></A><A NAME="IDX1961"></A><A NAME="IDX1962"></A><A NAME="IDX1963"></A><H3><A NAME="SEC487" HREF="library_toc.html#SEC487" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC487">Floating Type Macros</A></H3><P>The specific representation of floating point numbers varies frommachine to machine.  Because floating point numbers are representedinternally as approximate quantities, algorithms for manipulatingfloating point data often need to take account of the precise details ofthe machine's floating point representation.<P>Some of the functions in the C library itself need this information; forexample, the algorithms for printing and reading floating point numbers(see section <A HREF="library_11.html#SEC117" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_11.html#SEC117">Input/Output on Streams</A>) and for calculating trigonometric andirrational functions (see section <A HREF="library_17.html#SEC290" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_17.html#SEC290">Mathematics</A>) use it to avoid round-offerror and loss of accuracy.  User programs that implement numericalanalysis techniques also often need this information in order tominimize or compute error bounds.<P>The header file <TT>`float.h'</TT> describes the format used by yourmachine.<P><H4><A NAME="SEC488" HREF="library_toc.html#SEC488" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC488">Floating Point Representation Concepts</A></H4><P>This section introduces the terminology for describing floating pointrepresentations.<P>You are probably already familiar with most of these concepts in termsof scientific or exponential notation for floating point numbers.  Forexample, the number <CODE>123456.0</CODE> could be expressed in exponentialnotation as <CODE>1.23456e+05</CODE>, a shorthand notation indicating that themantissa <CODE>1.23456</CODE> is multiplied by the base <CODE>10</CODE> raised topower <CODE>5</CODE>.<P>More formally, the internal representation of a floating point numbercan be characterized in terms of the following parameters:<P><UL><A NAME="IDX1964"></A><LI>The <DFN>sign</DFN> is either <CODE>-1</CODE> or <CODE>1</CODE>.<P><A NAME="IDX1965"></A><A NAME="IDX1966"></A><LI>The <DFN>base</DFN> or <DFN>radix</DFN> for exponentiation, an integer greaterthan <CODE>1</CODE>.  This is a constant for a particular representation.<P><A NAME="IDX1967"></A><LI>The <DFN>exponent</DFN> to which the base is raised.  The upper and lowerbounds of the exponent value are constants for a particularrepresentation.<A NAME="IDX1968"></A><P>Sometimes, in the actual bits representing the floating point number,the exponent is <DFN>biased</DFN> by adding a constant to it, to make italways be represented as an unsigned quantity.  This is only importantif you have some reason to pick apart the bit fields making up thefloating point number by hand, which is something for which the GNUlibrary provides no support.  So this is ignored in the discussion thatfollows.<P><A NAME="IDX1969"></A><A NAME="IDX1970"></A><LI>The <DFN>mantissa</DFN> or <DFN>significand</DFN>, an unsigned integer which is apart of each floating point number.<P><A NAME="IDX1971"></A><LI>The <DFN>precision</DFN> of the mantissa.  If the base of the representationis <VAR>b</VAR>, then the precision is the number of base-<VAR>b</VAR> digits inthe mantissa.  This is a constant for a particular representation.<A NAME="IDX1972"></A><P>Many floating point representations have an implicit <DFN>hidden bit</DFN> inthe mantissa.  This is a bit which is present virtually in the mantissa,but not stored in memory because its value is always 1 in a normalizednumber.  The precision figure (see above) includes any hidden bits.<P>Again, the GNU library provides no facilities for dealing with suchlow-level aspects of the representation.</UL><P>The mantissa of a floating point number actually represents an implicitfraction whose denominator is the base raised to the power of theprecision.  Since the largest representable mantissa is one less thanthis denominator, the value of the fraction is always strictly less than<CODE>1</CODE>.  The mathematical value of a floating point number is then theproduct of this fraction, the sign, and the base raised to the exponent.<A NAME="IDX1973"></A><P>We say that the floating point number is <DFN>normalized</DFN> if thefraction is at least <CODE>1/<VAR>b</VAR></CODE>, where <VAR>b</VAR> is the base.  Inother words, the mantissa would be too large to fit if it weremultiplied by the base.  Non-normalized numbers are sometimes called<DFN>denormal</DFN>; they contain less precision than the representationnormally can hold.<P>If the number is not normalized, then you can subtract <CODE>1</CODE> from theexponent while multiplying the mantissa by the base, and get another

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -