📄 arith.texi
字号:
If @var{denominator} is zero, @code{fmod} fails and sets @code{errno} to@code{EDOM}.@end deftypefun@comment math.h@comment BSD@deftypefun double drem (double @var{numerator}, double @var{denominator})The function @code{drem} is like @code{fmod} except that it rounds theinternal quotient @var{n} to the nearest integer instead of towards zeroto an integer. For example, @code{drem (6.5, 2.3)} returns @code{-0.4},which is @code{6.5} minus @code{6.9}.The absolute value of the result is less than or equal to half theabsolute value of the @var{denominator}. The difference between@code{fmod (@var{numerator}, @var{denominator})} and @code{drem(@var{numerator}, @var{denominator})} is always either@var{denominator}, minus @var{denominator}, or zero.If @var{denominator} is zero, @code{drem} fails and sets @code{errno} to@code{EDOM}.@end deftypefun@node Integer Division@section Integer Division@cindex integer division functionsThis section describes functions for performing integer division. Thesefunctions are redundant in the GNU C library, since in GNU C the @samp{/}operator always rounds towards zero. But in other C implementations,@samp{/} may round differently with negative arguments. @code{div} and@code{ldiv} are useful because they specify how to round the quotient:towards zero. The remainder has the same sign as the numerator.These functions are specified to return a result @var{r} such that the value@code{@var{r}.quot*@var{denominator} + @var{r}.rem} equals@var{numerator}.@pindex stdlib.hTo use these facilities, you should include the header file@file{stdlib.h} in your program.@comment stdlib.h@comment ANSI@deftp {Data Type} div_tThis is a structure type used to hold the result returned by the @code{div}function. It has the following members:@table @code@item int quotThe quotient from the division.@item int remThe remainder from the division.@end table@end deftp@comment stdlib.h@comment ANSI@deftypefun div_t div (int @var{numerator}, int @var{denominator})This function @code{div} computes the quotient and remainder fromthe division of @var{numerator} by @var{denominator}, returning theresult in a structure of type @code{div_t}.If the result cannot be represented (as in a division by zero), thebehavior is undefined.Here is an example, albeit not a very useful one.@smallexamplediv_t result;result = div (20, -6);@end smallexample@noindentNow @code{result.quot} is @code{-3} and @code{result.rem} is @code{2}.@end deftypefun@comment stdlib.h@comment ANSI@deftp {Data Type} ldiv_tThis is a structure type used to hold the result returned by the @code{ldiv}function. It has the following members:@table @code@item long int quotThe quotient from the division.@item long int remThe remainder from the division.@end table(This is identical to @code{div_t} except that the components are oftype @code{long int} rather than @code{int}.)@end deftp@comment stdlib.h@comment ANSI@deftypefun ldiv_t ldiv (long int @var{numerator}, long int @var{denominator})The @code{ldiv} function is similar to @code{div}, except that thearguments are of type @code{long int} and the result is returned as astructure of type @code{ldiv}.@end deftypefun@node Parsing of Numbers@section Parsing of Numbers@cindex parsing numbers (in formatted input)@cindex converting strings to numbers@cindex number syntax, parsing@cindex syntax, for reading numbersThis section describes functions for ``reading'' integer andfloating-point numbers from a string. It may be more convenient in somecases to use @code{sscanf} or one of the related functions; see@ref{Formatted Input}. But often you can make a program more robust byfinding the tokens in the string by hand, then converting the numbersone by one.@menu* Parsing of Integers:: Functions for conversion of integer values.* Parsing of Floats:: Functions for conversion of floating-point values.@end menu@node Parsing of Integers@subsection Parsing of Integers@pindex stdlib.hThese functions are declared in @file{stdlib.h}.@comment stdlib.h@comment ANSI@deftypefun {long int} strtol (const char *@var{string}, char **@var{tailptr}, int @var{base})The @code{strtol} (``string-to-long'') function converts the initialpart of @var{string} to a signed integer, which is returned as a valueof type @code{long int}. This function attempts to decompose @var{string} as follows:@itemize @bullet@item A (possibly empty) sequence of whitespace characters. Which charactersare whitespace is determined by the @code{isspace} function(@pxref{Classification of Characters}). These are discarded.@item An optional plus or minus sign (@samp{+} or @samp{-}).@item A nonempty sequence of digits in the radix specified by @var{base}.If @var{base} is zero, decimal radix is assumed unless the series ofdigits begins with @samp{0} (specifying octal radix), or @samp{0x} or@samp{0X} (specifying hexadecimal radix); in other words, the samesyntax used for integer constants in C.Otherwise @var{base} must have a value between @code{2} and @code{35}.If @var{base} is @code{16}, the digits may optionally be preceded by@samp{0x} or @samp{0X}.@item Any remaining characters in the string. If @var{tailptr} is not a nullpointer, @code{strtol} stores a pointer to this tail in@code{*@var{tailptr}}.@end itemizeIf the string is empty, contains only whitespace, or does not contain aninitial substring that has the expected syntax for an integer in thespecified @var{base}, no conversion is performed. In this case,@code{strtol} returns a value of zero and the value stored in@code{*@var{tailptr}} is the value of @var{string}.In a locale other than the standard @code{"C"} locale, this functionmay recognize additional implementation-dependent syntax.If the string has valid syntax for an integer but the value is notrepresentable because of overflow, @code{strtol} returns either@code{LONG_MAX} or @code{LONG_MIN} (@pxref{Range of Type}), asappropriate for the sign of the value. It also sets @code{errno}to @code{ERANGE} to indicate there was overflow.There is an example at the end of this section.@end deftypefun@comment stdlib.h@comment ANSI@deftypefun {unsigned long int} strtoul (const char *@var{string}, char **@var{tailptr}, int @var{base})The @code{strtoul} (``string-to-unsigned-long'') function is like@code{strtol} except that it returns its value with type @code{unsignedlong int}. The value returned in case of overflow is @code{ULONG_MAX}(@pxref{Range of Type}).@end deftypefun@comment stdlib.h@comment ANSI@deftypefun {long int} atol (const char *@var{string})This function is similar to the @code{strtol} function with a @var{base}argument of @code{10}, except that it need not detect overflow errors.The @code{atol} function is provided mostly for compatibility withexisting code; using @code{strtol} is more robust.@end deftypefun@comment stdlib.h@comment ANSI@deftypefun int atoi (const char *@var{string})This function is like @code{atol}, except that it returns an @code{int}value rather than @code{long int}. The @code{atoi} function is alsoconsidered obsolete; use @code{strtol} instead.@end deftypefunHere is a function which parses a string as a sequence of integers andreturns the sum of them:@smallexampleintsum_ints_from_string (char *string)@{ int sum = 0; while (1) @{ char *tail; int next; /* @r{Skip whitespace by hand, to detect the end.} */ while (isspace (*string)) string++; if (*string == 0) break; /* @r{There is more nonwhitespace,} */ /* @r{so it ought to be another number.} */ errno = 0; /* @r{Parse it.} */ next = strtol (string, &tail, 0); /* @r{Add it in, if not overflow.} */ if (errno) printf ("Overflow\n"); else sum += next; /* @r{Advance past it.} */ string = tail; @} return sum;@}@end smallexample@node Parsing of Floats@subsection Parsing of Floats@pindex stdlib.hThese functions are declared in @file{stdlib.h}.@comment stdlib.h@comment ANSI@deftypefun double strtod (const char *@var{string}, char **@var{tailptr})The @code{strtod} (``string-to-double'') function converts the initialpart of @var{string} to a floating-point number, which is returned as avalue of type @code{double}. This function attempts to decompose @var{string} as follows:@itemize @bullet@item A (possibly empty) sequence of whitespace characters. Which charactersare whitespace is determined by the @code{isspace} function(@pxref{Classification of Characters}). These are discarded.@itemAn optional plus or minus sign (@samp{+} or @samp{-}).@itemA nonempty sequence of digits optionally containing a decimal-pointcharacter---normally @samp{.}, but it depends on the locale(@pxref{Numeric Formatting}).@itemAn optional exponent part, consisting of a character @samp{e} or@samp{E}, an optional sign, and a sequence of digits.@itemAny remaining characters in the string. If @var{tailptr} is not a nullpointer, a pointer to this tail of the string is stored in@code{*@var{tailptr}}.@end itemizeIf the string is empty, contains only whitespace, or does not contain aninitial substring that has the expected syntax for a floating-pointnumber, no conversion is performed. In this case, @code{strtod} returnsa value of zero and the value returned in @code{*@var{tailptr}} is thevalue of @var{string}.In a locale other than the standard @code{"C"} locale, this function mayrecognize additional locale-dependent syntax.If the string has valid syntax for a floating-point number but the valueis not representable because of overflow, @code{strtod} returns eitherpositive or negative @code{HUGE_VAL} (@pxref{Mathematics}), depending onthe sign of the value. Similarly, if the value is not representablebecause of underflow, @code{strtod} returns zero. It also sets @code{errno}to @code{ERANGE} if there was overflow or underflow.@end deftypefun@comment stdlib.h@comment ANSI@deftypefun double atof (const char *@var{string})This function is similar to the @code{strtod} function, except that itneed not detect overflow and underflow errors. The @code{atof} functionis provided mostly for compatibility with existing code; using@code{strtod} is more robust.@end deftypefun
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -