Programming languages — C (INTERNATIONAL STANDARD ISO/IEC 9899:TC3) - page 13

 

  Главная      Manuals     Programming languages — C (INTERNATIONAL STANDARD ISO/IEC 9899:TC3) - 2007 year

 

Search            copyright infringement  

 

 

 

 

 

 

 

 

 

 

 

Content      ..     11      12      13      14     ..

 

 

 

Programming languages — C (INTERNATIONAL STANDARD ISO/IEC 9899:TC3) - page 13

 

 

B.23 Extended multibyte/wide character utilities

<wchar.h>

wchar_t

size_t

mbstate_t

wint_t

struct tm

NULL

WCHAR_MAX

WCHAR_MIN

WEOF

int fwprintf(FILE * restrict stream,

const wchar_t * restrict format, ...);

int fwscanf(FILE * restrict stream,

const wchar_t * restrict format, ...);

int swprintf(wchar_t * restrict s, size_t n,

const wchar_t * restrict format, ...);

int swscanf(const wchar_t * restrict s,

const wchar_t * restrict format, ...);

int vfwprintf(FILE * restrict stream,

const wchar_t * restrict format, va_list arg);

int vfwscanf(FILE * restrict stream,

const wchar_t * restrict format, va_list arg);

int vswprintf(wchar_t * restrict s, size_t n,

const wchar_t * restrict format, va_list arg);

int vswscanf(const wchar_t * restrict s,

const wchar_t * restrict format, va_list arg);

int vwprintf(const wchar_t * restrict format,

va_list arg);

int vwscanf(const wchar_t * restrict format,

va_list arg);

int wprintf(const wchar_t * restrict format, ...);

int wscanf(const wchar_t * restrict format, ...);

wint_t fgetwc(FILE *stream);

wchar_t *fgetws(wchar_t * restrict s, int n,

FILE * restrict stream);

wint_t fputwc(wchar_t c, FILE *stream);

int fputws(const wchar_t * restrict s,

FILE * restrict stream);

int fwide(FILE *stream, int mode);

wint_t getwc(FILE *stream);

wint_t getwchar(void);

wint_t putwc(wchar_t c, FILE *stream);

wint_t putwchar(wchar_t c);

wint_t ungetwc(wint_t c, FILE *stream);

§B.23 Library summary 435

double wcstod(const wchar_t * restrict nptr,

wchar_t ** restrict endptr);

float wcstof(const wchar_t * restrict nptr,

wchar_t ** restrict endptr);

long double wcstold(const wchar_t * restrict nptr,

wchar_t ** restrict endptr);

long int wcstol(const wchar_t * restrict nptr,

wchar_t ** restrict endptr, int base);

long long int wcstoll(const wchar_t * restrict nptr,

wchar_t ** restrict endptr, int base);

unsigned long int wcstoul(const wchar_t * restrict nptr,

wchar_t ** restrict endptr, int base);

unsigned long long int wcstoull(

const wchar_t * restrict nptr,

wchar_t ** restrict endptr, int base);

wchar_t *wcscpy(wchar_t * restrict s1,

const wchar_t * restrict s2);

wchar_t *wcsncpy(wchar_t * restrict s1,

const wchar_t * restrict s2, size_t n);

wchar_t *wmemcpy(wchar_t * restrict s1,

const wchar_t * restrict s2, size_t n);

wchar_t *wmemmove(wchar_t *s1, const wchar_t *s2,

size_t n);

wchar_t *wcscat(wchar_t * restrict s1,

const wchar_t * restrict s2);

wchar_t *wcsncat(wchar_t * restrict s1,

const wchar_t * restrict s2, size_t n);

int wcscmp(const wchar_t *s1, const wchar_t *s2);

int wcscoll(const wchar_t *s1, const wchar_t *s2);

int wcsncmp(const wchar_t *s1, const wchar_t *s2,

size_t n);

size_t wcsxfrm(wchar_t * restrict s1,

const wchar_t * restrict s2, size_t n);

int wmemcmp(const wchar_t *s1, const wchar_t *s2,

size_t n);

wchar_t *wcschr(const wchar_t *s, wchar_t c);

size_t wcscspn(const wchar_t *s1, const wchar_t *s2);

wchar_t *wcspbrk(const wchar_t *s1, const wchar_t *s2);

wchar_t *wcsrchr(const wchar_t *s, wchar_t c);

size_t wcsspn(const wchar_t *s1, const wchar_t *s2);

wchar_t *wcsstr(const wchar_t *s1, const wchar_t *s2);

436 Library summary §B.23

wchar_t *wcstok(wchar_t * restrict s1,

const wchar_t * restrict s2,

wchar_t ** restrict ptr);

wchar_t *wmemchr(const wchar_t *s, wchar_t c, size_t n);

size_t wcslen(const wchar_t *s);

wchar_t *wmemset(wchar_t *s, wchar_t c, size_t n);

size_t wcsftime(wchar_t * restrict s, size_t maxsize,

const wchar_t * restrict format,

const struct tm * restrict timeptr);

wint_t btowc(int c);

int wctob(wint_t c);

int mbsinit(const mbstate_t *ps);

size_t mbrlen(const char * restrict s, size_t n,

mbstate_t * restrict ps);

size_t mbrtowc(wchar_t * restrict pwc,

const char * restrict s, size_t n,

mbstate_t * restrict ps);

size_t wcrtomb(char * restrict s, wchar_t wc,

mbstate_t * restrict ps);

size_t mbsrtowcs(wchar_t * restrict dst,

const char ** restrict src, size_t len,

mbstate_t * restrict ps);

size_t wcsrtombs(char * restrict dst,

const wchar_t ** restrict src, size_t len,

mbstate_t * restrict ps);

B.24 Wide character classification and mapping utilities

<wctype.h>

wint_t

wctrans_t

wctype_t

WEOF

int iswalnum(wint_t wc);

int iswalpha(wint_t wc);

int iswblank(wint_t wc);

int iswcntrl(wint_t wc);

int iswdigit(wint_t wc);

int iswgraph(wint_t wc);

int iswlower(wint_t wc);

int iswprint(wint_t wc);

int iswpunct(wint_t wc);

int iswspace(wint_t wc);

int iswupper(wint_t wc);

int iswxdigit(wint_t wc);

int iswctype(wint_t wc, wctype_t desc);

§B.24 Library summary 437

wctype_t wctype(const char *property);

wint_t towlower(wint_t wc);

wint_t towupper(wint_t wc);

wint_t towctrans(wint_t wc, wctrans_t desc);

wctrans_t wctrans(const char *property);

438 Library summary §B.24

Annex C

(informative)

Sequence points

1

The following are the sequence points described in 5.1.2.3:

— The call to a function, after the arguments have been evaluated (6.5.2.2).

— The end of the first operand of the following operators: logical

AND

&&

(6.5.13);

logical

OR

||

(6.5.14); conditional

?

(6.5.15); comma

,

(6.5.17).

— The end of a full declarator: declarators (6.7.5);

— The end of a full expression: an initializer (6.7.8); the expression in an expression

statement (6.8.3); the controlling expression of a selection statement (

if

or

switch

)

(6.8.4); the controlling expression of a

while

or

do

statement (6.8.5); each of the

expressions of a

for

statement (6.8.5.3); the expression in a

return

statement

(6.8.6.4).

— Immediately before a library function returns (7.1.4).

— After the actions associated with each formatted input/output function conversion

specifier (7.19.6, 7.24.2).

— Immediately before and immediately after each call to a comparison function, and

also between any call to a comparison function and any movement of the objects
passed as arguments to that call (7.20.5).

§C Sequence points 439

Annex D

(normative)

Universal character names for identifiers

1

This clause lists the hexadecimal code values that are valid in universal character names
in identifiers.

2

This table is reproduced unchanged from ISO/IEC TR 10176:1998, produced by ISO/IEC
JTC 1/SC 22/WG 20, except for the omission of ranges that are part of the basic character
sets.

Latin: 00AA, 00BA, 00C0−00D6, 00D8−00F6, 00F8−01F5, 01FA−0217,

0250−02A8, 1E00−1E9B, 1EA0−1EF9, 207F

Greek: 0386, 0388−038A, 038C, 038E−03A1, 03A3−03CE, 03D0−03D6,

03DA, 03DC, 03DE, 03E0, 03E2−03F3, 1F00−1F15, 1F18−1F1D,
1F20−1F45, 1F48−1F4D, 1F50−1F57, 1F59, 1F5B, 1F5D,
1F5F−1F7D, 1F80−1FB4, 1FB6−1FBC, 1FC2−1FC4, 1FC6−1FCC,
1FD0−1FD3, 1FD6−1FDB, 1FE0−1FEC, 1FF2−1FF4, 1FF6−1FFC

Cyrillic: 0401−040C,

040E−044F,

0451−045C, 045E−0481, 0490−04C4,

04C7−04C8, 04CB−04CC, 04D0−04EB, 04EE−04F5, 04F8−04F9

Armenian: 0531−0556, 0561−0587

Hebrew: 05B0−05B9,

05BB−05BD, 05BF,

05C1−05C2, 05D0−05EA,

05F0−05F2

Arabic: 0621−063A, 0640−0652, 0670−06B7, 06BA−06BE, 06C0−06CE,

06D0−06DC, 06E5−06E8, 06EA−06ED

Devanagari: 0901−0903, 0905−0939, 093E−094D, 0950−0952, 0958−0963

Bengali: 0981−0983, 0985−098C, 098F−0990, 0993−09A8, 09AA−09B0,

09B2, 09B6−09B9, 09BE−09C4, 09C7−09C8, 09CB−09CD,
09DC−09DD, 09DF−09E3, 09F0−09F1

Gurmukhi: 0A02,

0A05−0A0A, 0A0F−0A10, 0A13−0A28, 0A2A−0A30,

0A32−0A33, 0A35−0A36, 0A38−0A39, 0A3E−0A42, 0A47−0A48,
0A4B−0A4D, 0A59−0A5C, 0A5E, 0A74

Gujarati: 0A81−0A83,

0A85−0A8B, 0A8D, 0A8F−0A91, 0A93−0AA8,

0AAA−0AB0, 0AB2−0AB3, 0AB5−0AB9, 0ABD−0AC5,
0AC7−0AC9, 0ACB−0ACD, 0AD0, 0AE0

Oriya: 0B01−0B03, 0B05−0B0C, 0B0F−0B10, 0B13−0B28, 0B2A−0B30,

0B32−0B33, 0B36−0B39, 0B3E−0B43, 0B47−0B48, 0B4B−0B4D,

440 Universal character names for identifiers

§D

0B5C−0B5D, 0B5F−0B61

Tamil: 0B82−0B83, 0B85−0B8A, 0B8E−0B90, 0B92−0B95, 0B99−0B9A,

0B9C, 0B9E−0B9F,

0BA3−0BA4, 0BA8−0BAA, 0BAE−0BB5,

0BB7−0BB9, 0BBE−0BC2, 0BC6−0BC8, 0BCA−0BCD

Telugu: 0C01−0C03, 0C05−0C0C, 0C0E−0C10, 0C12−0C28, 0C2A−0C33,

0C35−0C39, 0C3E−0C44, 0C46−0C48, 0C4A−0C4D, 0C60−0C61

Kannada: 0C82−0C83, 0C85−0C8C, 0C8E−0C90, 0C92−0CA8, 0CAA−0CB3,

0CB5−0CB9, 0CBE−0CC4, 0CC6−0CC8, 0CCA−0CCD, 0CDE,
0CE0−0CE1

Malayalam: 0D02−0D03, 0D05−0D0C, 0D0E−0D10, 0D12−0D28, 0D2A−0D39,

0D3E−0D43, 0D46−0D48, 0D4A−0D4D, 0D60−0D61

Thai: 0E01−0E3A, 0E40−0E5B

Lao: 0E81−0E82,

0E84, 0E87−0E88, 0E8A, 0E8D, 0E94−0E97,

0E99−0E9F,

0EA1−0EA3, 0EA5, 0EA7, 0EAA−0EAB,

0EAD−0EAE, 0EB0−0EB9, 0EBB−0EBD, 0EC0−0EC4, 0EC6,
0EC8−0ECD, 0EDC−0EDD

Tibetan: 0F00, 0F18−0F19, 0F35, 0F37, 0F39, 0F3E−0F47, 0F49−0F69,

0F71−0F84, 0F86−0F8B, 0F90−0F95, 0F97, 0F99−0FAD,
0FB1−0FB7, 0FB9

Georgian: 10A0−10C5, 10D0−10F6

Hiragana: 3041−3093, 309B−309C

Katakana: 30A1−30F6, 30FB−30FC

Bopomofo: 3105−312C

CJK Unified Ideographs: 4E00−9FA5

Hangul: AC00−D7A3

Digits: 0660−0669,

06F0−06F9, 0966−096F,

09E6−09EF,

0A66−0A6F,

0AE6−0AEF, 0B66−0B6F, 0BE7−0BEF, 0C66−0C6F, 0CE6−0CEF,
0D66−0D6F, 0E50−0E59, 0ED0−0ED9, 0F20−0F33

Special characters: 00B5,

00B7, 02B0−02B8, 02BB, 02BD−02C1, 02D0−02D1,

02E0−02E4, 037A, 0559, 093D, 0B3D, 1FBE, 203F−2040, 2102,
2107, 210A−2113, 2115, 2118−211D, 2124, 2126, 2128, 212A−2131,
2133−2138, 2160−2182, 3005−3007, 3021−3029

§D Universal character names for identifiers

441

Annex E

(informative)

Implementation limits

1

The contents of the header

<limits.h>

are given below, in alphabetical order. The

minimum magnitudes shown shall be replaced by implementation-defined magnitudes
with the same sign. The values shall all be constant expressions suitable for use in

#if

preprocessing directives. The components are described further in 5.2.4.2.1.

#define CHAR_BIT

8

#define CHAR_MAX

UCHAR_MAX

or

SCHAR_MAX

#define CHAR_MIN

0

or

SCHAR_MIN

#define INT_MAX

+32767

#define INT_MIN

-32767

#define LONG_MAX

+2147483647

#define LONG_MIN

-2147483647

#define LLONG_MAX

+9223372036854775807

#define LLONG_MIN

-9223372036854775807

#define MB_LEN_MAX

1

#define SCHAR_MAX

+127

#define SCHAR_MIN

-127

#define SHRT_MAX

+32767

#define SHRT_MIN

-32767

#define UCHAR_MAX

255

#define USHRT_MAX

65535

#define UINT_MAX

65535

#define ULONG_MAX

4294967295

#define ULLONG_MAX

18446744073709551615

2

The contents of the header

<float.h>

are given below. All integer values, except

FLT_ROUNDS

, shall be constant expressions suitable for use in

#if

preprocessing

directives; all floating values shall be constant expressions. The components are
described further in 5.2.4.2.2.

3

The values given in the following list shall be replaced by implementation-defined
expressions:

#define FLT_EVAL_METHOD

#define FLT_ROUNDS

4

The values given in the following list shall be replaced by implementation-defined
constant expressions that are greater or equal in magnitude (absolute value) to those
shown, with the same sign:

442 Implementation limits §E

#define DBL_DIG

10

#define DBL_MANT_DIG

#define DBL_MAX_10_EXP

+37

#define DBL_MAX_EXP

#define DBL_MIN_10_EXP

-37

#define DBL_MIN_EXP

#define DECIMAL_DIG

10

#define FLT_DIG

6

#define FLT_MANT_DIG

#define FLT_MAX_10_EXP

+37

#define FLT_MAX_EXP

#define FLT_MIN_10_EXP

-37

#define FLT_MIN_EXP

#define FLT_RADIX

2

#define LDBL_DIG

10

#define LDBL_MANT_DIG

#define LDBL_MAX_10_EXP

+37

#define LDBL_MAX_EXP

#define LDBL_MIN_10_EXP

-37

#define LDBL_MIN_EXP

5

The values given in the following list shall be replaced by implementation-defined
constant expressions with values that are greater than or equal to those shown:

#define DBL_MAX

1E+37

#define FLT_MAX

1E+37

#define LDBL_MAX

1E+37

6

The values given in the following list shall be replaced by implementation-defined
constant expressions with (positive) values that are less than or equal to those shown:

#define DBL_EPSILON

1E-9

#define DBL_MIN

1E-37

#define FLT_EPSILON

1E-5

#define FLT_MIN

1E-37

#define LDBL_EPSILON

1E-9

#define LDBL_MIN

1E-37

§E Implementation limits 443

Annex F

(normative)

IEC 60559 floating-point arithmetic

F.1 Introduction

1

This annex specifies C language support for the IEC 60559 floating-point standard. The
IEC 60559 floating-point standard is specifically Binary floating-point arithmetic for
microprocessor systems, second edition 
(IEC 60559:1989), previously designated
IEC 559:1989

and as

IEEE Standard

for Binary Floating-Point Arithmetic

(ANSI/IEEE 754−1985).

IEEE Standard

for Radix-Independent Floating-Point

Arithmetic

(ANSI/IEEE 854−1987)

generalizes the binary standard to remove

dependencies on radix and word length. IEC 60559 generally refers to the floating-point
standard, as in IEC 60559 operation, IEC 60559 format, etc. An implementation that
defines

_ _STDC_IEC_559_ _

shall conform to the specifications in this annex. Where

a binding between the C language and IEC 60559 is indicated, the IEC 60559-specified
behavior is adopted by reference, unless stated otherwise.

F.2 Types

1

The C floating types match the IEC 60559 formats as follows:

— The

float

type matches the IEC 60559 single format.

— The

double

type matches the IEC 60559 double format.

— The

long double

type matches an IEC 60559 extended format,

307)

else a

non-IEC 60559 extended format, else the IEC 60559

double

format.

Any non-IEC 60559 extended format used for the

long double

type shall have more

precision than IEC 60559 double and at least the range of IEC 60559 double.

308)

Recommended practice

2

The

long double

type should match an IEC 60559 extended format.

307) ‘‘Extended’’ is IEC 60559’s double-extended data format. Extended refers to both the common 80-bit

and quadruple 128-bit IEC 60559 formats.

308) A non-IEC 60559

long double

type is required to provide infinity and NaNs, as its values include

all

double

values.

444 IEC 60559 floating-point arithmetic

§F.2

F.2.1 Infinities, signed zeros, and NaNs

1

This specification does not define the behavior of signaling NaNs.

309)

It generally uses

the term NaN to denote quiet NaNs. The

NAN

and

INFINITY

macros and the

nan

functions in

<math.h>

provide designations for IEC 60559 NaNs and infinities.

F.3 Operators and functions

1

C operators and functions provide IEC 60559 required and recommended facilities as
listed below.

— The

+

,

,

*

, and

/

operators provide the IEC 60559 add, subtract, multiply, and

divide operations.

— The

sqrt

functions in

<math.h>

provide the IEC 60559 square root operation.

— The

remainder

functions in

<math.h>

provide the IEC 60559 remainder

operation. The

remquo

functions in

<math.h>

provide the same operation but

with additional information.

— The

rint

functions in

<math.h>

provide the IEC 60559 operation that rounds a

floating-point number to an integer value (in the same precision). The

nearbyint

functions in

<math.h>

provide the nearbyinteger function recommended in the

Appendix to ANSI/IEEE 854.

— The conversions for floating types provide the IEC 60559 conversions between

floating-point precisions.

— The conversions from integer to floating types provide the IEC 60559 conversions

from integer to floating point.

— The conversions from floating to integer types provide IEC 60559-like conversions

but always round toward zero.

— The

lrint

and

llrint

functions in

<math.h>

provide the IEC 60559

conversions, which honor the directed rounding mode, from floating point to the

long int

and

long long int

integer formats. The

lrint

and

llrint

functions can be used to implement IEC 60559 conversions from floating to other
integer formats.

— The translation time conversion of floating constants and the

strtod

,

strtof

,

strtold

,

fprintf

,

fscanf

, and related library functions in

<stdlib.h>

,

<stdio.h>

, and

<wchar.h>

provide IEC 60559 binary-decimal conversions. The

strtold

function in

<stdlib.h>

provides the conv function recommended in the

Appendix to ANSI/IEEE 854.

309) Since NaNs created by IEC 60559 operations are always quiet, quiet NaNs (along with infinities) are

sufficient for closure of the arithmetic.

§F.3 IEC 60559 floating-point arithmetic

445

— The relational and equality operators provide IEC 60559 comparisons. IEC 60559

identifies a need for additional comparison predicates to facilitate writing code that
accounts for NaNs. The comparison macros (

isgreater

,

isgreaterequal

,

isless

,

islessequal

,

islessgreater

, and

isunordered

) in

<math.h>

supplement the language operators to address this need. The

islessgreater

and

isunordered

macros provide respectively a quiet version of the <> predicate and

the unordered predicate recommended in the Appendix to IEC 60559.

— The

feclearexcept

,

feraiseexcept

, and

fetestexcept

functions in

<fenv.h>

provide the facility to test and alter the IEC 60559 floating-point

exception status flags.

The

fegetexceptflag

and

fesetexceptflag

functions in

<fenv.h>

provide the facility to save and restore all five status flags at

one time. These functions are used in conjunction with the type

fexcept_t

and the

floating-point exception macros (

FE_INEXACT

,

FE_DIVBYZERO

,

FE_UNDERFLOW

,

FE_OVERFLOW

,

FE_INVALID

) also in

<fenv.h>

.

— The

fegetround

and

fesetround

functions in

<fenv.h>

provide the facility

to select among the IEC 60559 directed rounding modes represented by the rounding
direction macros in

<fenv.h>

(

FE_TONEAREST

,

FE_UPWARD

,

FE_DOWNWARD

,

FE_TOWARDZERO

) and the values

0

,

1

,

2

, and

3

of

FLT_ROUNDS

are the

IEC 60559 directed rounding modes.

— The

fegetenv

,

feholdexcept

,

fesetenv

, and

feupdateenv

functions in

<fenv.h>

provide a facility to manage the floating-point environment, comprising

the IEC 60559 status flags and control modes.

— The

copysign

functions in

<math.h>

provide the copysign function

recommended in the Appendix to IEC 60559.

— The unary minus (

) operator provides the minus (−) operation recommended in the

Appendix to IEC 60559.

— The

scalbn

and

scalbln

functions in

<math.h>

provide the scalb function

recommended in the Appendix to IEC 60559.

— The

logb

functions in

<math.h>

provide the logb function recommended in the

Appendix to IEC 60559, but following the newer specifications in ANSI/IEEE 854.

— The

nextafter

and

nexttoward

functions in

<math.h>

provide the nextafter

function recommended in the Appendix to IEC 60559 (but with a minor change to
better handle signed zeros).

— The

isfinite

macro in

<math.h>

provides the finite function recommended in

the Appendix to IEC 60559.

— The

isnan

macro in

<math.h>

provides the isnan function recommended in the

Appendix to IEC 60559.

446 IEC 60559 floating-point arithmetic

§F.3

— The

signbit

macro and the

fpclassify

macro in

<math.h>

, used in

conjunction with the number classification macros (

FP_NAN

,

FP_INFINITE

,

FP_NORMAL

,

FP_SUBNORMAL

,

FP_ZERO

), provide the facility of the class

function recommended in the Appendix to IEC 60559 (except that the classification
macros defined in 7.12.3 do not distinguish signaling from quiet NaNs).

F.4 Floating to integer conversion

1

If the floating value is infinite or NaN or if the integral part of the floating value exceeds
the range of the integer type, then the ‘‘invalid’’ floating-point exception is raised and the
resulting value is unspecified. Whether conversion of non-integer floating values whose
integral part is within the range of the integer type raises the ‘‘inexact’’ floating-point
exception is unspecified.

310)

F.5 Binary-decimal conversion

1

Conversion from the widest supported IEC 60559 format to decimal with

DECIMAL_DIG

digits and back is the identity function.

311)

2

Conversions involving IEC 60559 formats follow all pertinent recommended practice. In
particular, conversion between any supported IEC 60559 format and decimal with

DECIMAL_DIG

or fewer significant digits is correctly rounded (honoring the current

rounding mode), which assures that conversion from the widest supported IEC 60559
format to decimal with

DECIMAL_DIG

digits and back is the identity function.

3

Functions such as

strtod

that convert character sequences to floating types honor the

rounding direction. Hence, if the rounding direction might be upward or downward, the
implementation cannot convert a minus-signed sequence by negating the converted
unsigned sequence.

310) ANSI/IEEE 854, but not IEC 60559 (ANSI/IEEE 754), directly specifies that floating-to-integer

conversions raise the ‘‘inexact’’ floating-point exception for non-integer in-range values. In those
cases where it matters, library functions can be used to effect such conversions with or without raising
the ‘‘inexact’’ floating-point exception. See

rint

,

lrint

,

llrint

, and

nearbyint

in

<math.h>

.

311) If the minimum-width IEC 60559 extended format (64 bits of precision) is supported,

DECIMAL_DIG

shall be at least 21. If IEC 60559 double (53 bits of precision) is the widest

IEC 60559 format supported, then

DECIMAL_DIG

shall be at least 17. (By contrast,

LDBL_DIG

and

DBL_DIG

are 18 and 15, respectively, for these formats.)

§F.5 IEC 60559 floating-point arithmetic

447

F.6 Contracted expressions

1

A contracted expression treats infinities, NaNs, signed zeros, subnormals, and the
rounding directions in a manner consistent with the basic arithmetic operations covered
by IEC 60559.

Recommended practice

2

A contracted expression should raise floating-point exceptions in a manner generally
consistent with the basic arithmetic operations. A contracted expression should deliver
the same value as its uncontracted counterpart, else should be correctly rounded (once).

F.7 Floating-point environment

1

The floating-point environment defined in

<fenv.h>

includes the IEC 60559 floating-

point exception status flags and directed-rounding control modes. It includes also
IEC 60559

dynamic rounding precision and trap enablement modes, if the

implementation supports them.

312)

F.7.1 Environment management

1

IEC 60559 requires that floating-point operations implicitly raise floating-point exception
status flags, and that rounding control modes can be set explicitly to affect result values of
floating-point operations. When the state for the

FENV_ACCESS

pragma (defined in

<fenv.h>

) is ‘‘on’’, these changes to the floating-point state are treated as side effects

which respect sequence points.

313)

F.7.2 Translation

1

During translation the IEC 60559 default modes are in effect:

— The rounding direction mode is rounding to nearest.

— The rounding precision mode (if supported) is set so that results are not shortened.

— Trapping or stopping (if supported) is disabled on all floating-point exceptions.

Recommended practice

2

The implementation should produce a diagnostic message for each translation-time

312) This specification does not require dynamic rounding precision nor trap enablement modes.

313) If the state for the

FENV_ACCESS

pragma is ‘‘off’’, the implementation is free to assume the floating-

point control modes will be the default ones and the floating-point status flags will not be tested,
which allows certain optimizations (see F.8).

448 IEC 60559 floating-point arithmetic

§F.7.2

floating-point exception, other than ‘‘inexact’’;

314)

the implementation should then

proceed with the translation of the program.

F.7.3 Execution

1

At program startup the floating-point environment is initialized as prescribed by
IEC 60559:

— All floating-point exception status flags are cleared.

— The rounding direction mode is rounding to nearest.

— The dynamic rounding precision mode (if supported) is set so that results are not

shortened.

— Trapping or stopping (if supported) is disabled on all floating-point exceptions.

F.7.4 Constant expressions

1

An arithmetic constant expression of floating type, other than one in an initializer for an
object that has static storage duration, is evaluated (as if) during execution; thus, it is
affected by any operative floating-point control modes and raises floating-point
exceptions as required by IEC 60559 (provided the state for the

FENV_ACCESS

pragma

is ‘‘on’’).

315)

2

EXAMPLE

#include <fenv.h>

#pragma STDC FENV_ACCESS ON

void f(void)

{

float w[] = { 0.0/0.0 };

//

raises an exception

static float x = 0.0/0.0; //

does not raise an exception

float y = 0.0/0.0;

//

raises an exception

double z = 0.0/0.0;

//

raises an exception

/*

...

*/

}

3

For the static initialization, the division is done at translation time, raising no (execution-time) floating-
point exceptions. On the other hand, for the three automatic initializations the invalid division occurs at

314) As floating constants are converted to appropriate internal representations at translation time, their

conversion is subject to default rounding modes and raises no execution-time floating-point exceptions
(even where the state of the

FENV_ACCESS

pragma is ‘‘on’’). Library functions, for example

strtod

, provide execution-time conversion of numeric strings.

315) Where the state for the

FENV_ACCESS

pragma is ‘‘on’’, results of inexact expressions like

1.0/3.0

are affected by rounding modes set at execution time, and expressions such as

0.0/0.0

and

1.0/0.0

generate execution-time floating-point exceptions. The programmer can achieve the

efficiency of translation-time evaluation through static initialization, such as

const static double one_third = 1.0/3.0;

§F.7.4 IEC 60559 floating-point arithmetic

449

execution time.

F.7.5 Initialization

1

All computation for automatic initialization is done (as if) at execution time; thus, it is
affected by any operative modes and raises floating-point exceptions as required by
IEC 60559 (provided the state for the

FENV_ACCESS

pragma is ‘‘on’’). All computation

for initialization of objects that have static storage duration is done (as if) at translation
time.

2

EXAMPLE

#include <fenv.h>

#pragma STDC FENV_ACCESS ON

void f(void)

{

float u[] = { 1.1e75 };

//

raises exceptions

static float v = 1.1e75; //

does not raise exceptions

float w = 1.1e75;

//

raises exceptions

double x = 1.1e75;

//

may raise exceptions

float y = 1.1e75f;

//

may raise exceptions

long double z = 1.1e75;

//

does not raise exceptions

/*

...

*/

}

3

The static initialization of

v

raises no (execution-time) floating-point exceptions because its computation is

done at translation time. The automatic initialization of

u

and

w

require an execution-time conversion to

float

of the wider value

1.1e75

, which raises floating-point exceptions. The automatic initializations

of

x

and

y

entail execution-time conversion; however, in some expression evaluation methods, the

conversions is not to a narrower format, in which case no floating-point exception is raised.

316)

The

automatic initialization of

z

entails execution-time conversion, but not to a narrower format, so no floating-

point exception is raised. Note that the conversions of the floating constants

1.1e75

and

1.1e75f

to

their internal representations occur at translation time in all cases.

316) Use of

float_t

and

double_t

variables increases the likelihood of translation-time computation.

For example, the automatic initialization

double_t x = 1.1e75;

could be done at translation time, regardless of the expression evaluation method.

450 IEC 60559 floating-point arithmetic

§F.7.5

F.7.6 Changing the environment

1

Operations defined in 6.5 and functions and macros defined for the standard libraries
change floating-point status flags and control modes just as indicated by their
specifications (including conformance to IEC 60559). They do not change flags or modes
(so as to be detectable by the user) in any other cases.

2

If the argument to the

feraiseexcept

function in

<fenv.h>

represents IEC 60559

valid coincident floating-point exceptions for atomic operations (namely ‘‘overflow’’ and
‘‘inexact’’, or ‘‘underflow’’ and ‘‘inexact’’), then ‘‘overflow’’ or ‘‘underflow’’ is raised
before ‘‘inexact’’.

F.8 Optimization

1

This section identifies code transformations that might subvert IEC 60559-specified
behavior, and others that do not.

F.8.1 Global transformations

1

Floating-point arithmetic operations and external function calls may entail side effects
which optimization shall honor, at least where the state of the

FENV_ACCESS

pragma is

‘‘on’’. The flags and modes in the floating-point environment may be regarded as global
variables; floating-point operations (

+

,

*

, etc.) implicitly read the modes and write the

flags.

2

Concern about side effects may inhibit code motion and removal of seemingly useless
code. For example, in

#include <fenv.h>

#pragma STDC FENV_ACCESS ON

void f(double x)

{

/*

...

*/

for (i = 0; i < n; i++) x + 1;

/*

...

*/

}

x + 1

might raise floating-point exceptions, so cannot be removed. And since the loop

body might not execute (maybe 0

n

),

x + 1

cannot be moved out of the loop. (Of

course these optimizations are valid if the implementation can rule out the nettlesome
cases.)

3

This specification does not require support for trap handlers that maintain information
about the order or count of floating-point exceptions. Therefore, between function calls,
floating-point exceptions need not be precise: the actual order and number of occurrences
of floating-point exceptions (> 1) may vary from what the source code expresses. Thus,
the preceding loop could be treated as

§F.8.1 IEC 60559 floating-point arithmetic

451

if (0 < n) x + 1;

F.8.2 Expression transformations

1

x / 2

x * 0.5

Although similar transformations involving inexact
constants generally do not yield numerically equivalent
expressions, if the constants are exact then such
transformations can be made on IEC 60559 machines
and others that round perfectly.

1 * x

and

x / 1

x

The expressions

1 * x

,

x / 1

, and

x

are equivalent

(on IEC 60559 machines, among others).

317)

x / x

1.0

The expressions

x / x

and

1.0

are not equivalent if

x

can be zero, infinite, or NaN.

x − y

x + (−y)

The expressions

x − y

,

x +   (−y)

, and

(−y) + x

are equivalent (on IEC 60559 machines, among others).

x − y

−(y − x)

The expressions

x

y

and

−(y − x)

are not

equivalent because 1 − 1 is +0 but −(1 − 1) is −0 (in the
default rounding direction).

318)

x − x

0.0

The expressions

x − x

and

0.0

are not equivalent if

x

is a NaN or infinite.

0 * x

0.0

The expressions

0 * x

and

0.0

are not equivalent if

x

is a NaN, infinite, or −0.

x + 0

x

The expressions

x + 0

and

x

are not equivalent if

x

is

−0, because (−0) + (+0) yields +0 (in the default
rounding direction), not −0.

x − 0

x

(+0) − (+0) yields −0 when rounding is downward
(toward

), but +0 otherwise, and (−0) − (+0) always

yields −0; so, if the state of the

FENV_ACCESS

pragma

is ‘‘off’’, promising default rounding, then the
implementation can replace

x

0

by

x

, even if

x

317) Strict support for signaling NaNs — not required by this specification — would invalidate these and

other transformations that remove arithmetic operators.

318) IEC 60559 prescribes a signed zero to preserve mathematical identities across certain discontinuities.

Examples include:

1/(1/

±

) is

±

and

conj(csqrt(z)) is csqrt(conj(z)),

for complex z.

452 IEC 60559 floating-point arithmetic

§F.8.2

might be zero.

−x

0 − x

The expressions

−x

and

0 − x

are not equivalent if

x

is +0, because −(+0) yields −0, but 0 − (+0) yields +0
(unless rounding is downward).

F.8.3 Relational operators

1

x != x

false The statement

x != x

is true if

x

is a NaN.

x == x

true The statement

x == x

is false if

x

is a NaN.

x < y

isless(x,y)

(and similarly for

<=

,

>

,

>=

) Though numerically

equal, these expressions are not equivalent because of
side effects when

x

or

y

is a NaN and the state of the

FENV_ACCESS

pragma is ‘‘on’’. This transformation,

which would be desirable if extra code were required to
cause the ‘‘invalid’’

floating-point exception for

unordered cases, could be performed provided the state
of the

FENV_ACCESS

pragma is ‘‘off’’.

The sense of relational operators shall be maintained. This includes handling unordered
cases as expressed by the source code.

2

EXAMPLE

//

calls

g

and raises ‘‘invalid’’ if

a

and

b

are unordered

if (a < b)

f();

else

g();

is not equivalent to

//

calls

f

and raises ‘‘invalid’’ if

a

and

b

are unordered

if (a >= b)

g();

else

f();

nor to

//

calls f without raising ‘‘invalid’’ if a and b are unordered

if (isgreaterequal(a,b))

g();

else

f();

nor, unless the state of the

FENV_ACCESS

pragma is ‘‘off’’, to

§F.8.3 IEC 60559 floating-point arithmetic

453

//

calls

g

without raising ‘‘invalid’’ if

a

and

b

are unordered

if (isless(a,b))

f();

else

g();

but is equivalent to

if (!(a < b))

g();

else

f();

F.8.4 Constant arithmetic

1

The implementation shall honor floating-point exceptions raised by execution-time
constant arithmetic wherever the state of the

FENV_ACCESS

pragma is ‘‘on’’. (See F.7.4

and F.7.5.) An operation on constants that raises no floating-point exception can be
folded during translation, except, if the state of the

FENV_ACCESS

pragma is ‘‘on’’, a

further check is required to assure that changing the rounding direction to downward does
not alter the sign of the result,

319)

and implementations that support dynamic rounding

precision modes shall assure further that the result of the operation raises no floating-
point exception when converted to the semantic type of the operation.

F.9 Mathematics

<math.h>

1

This subclause contains specifications of

<math.h>

facilities that are particularly suited

for IEC 60559 implementations.

2

The Standard C macro

HUGE_VAL

and its

float

and

long double

analogs,

HUGE_VALF

and

HUGE_VALL

, expand to expressions whose values are positive

infinities.

3

Special cases for functions in

<math.h>

are covered directly or indirectly by

IEC 60559. The functions that IEC 60559 specifies directly are identified in F.3. The
other functions in

<math.h>

treat infinities, NaNs, signed zeros, subnormals, and

(provided the state of the

FENV_ACCESS

pragma is ‘‘on’’) the floating-point status flags

in a manner consistent with the basic arithmetic operations covered by IEC 60559.

4

The expression

math_errhandling & MATH_ERREXCEPT

shall evaluate to a

nonzero value.

5

The ‘‘invalid’’ and ‘‘divide-by-zero’’ floating-point exceptions are raised as specified in
subsequent subclauses of this annex.

6

The ‘‘overflow’’ floating-point exception is raised whenever an infinity — or, because of
rounding direction, a maximal-magnitude finite number — is returned in lieu of a value

319) 0 − 0 yields −0 instead of +0 just when the rounding direction is downward.

454 IEC 60559 floating-point arithmetic

§F.9

whose magnitude is too large.

7

The ‘‘underflow’’ floating-point exception is raised whenever a result is tiny (essentially
subnormal or zero) and suffers loss of accuracy.

320)

8

Whether or when library functions raise the ‘‘inexact’’ floating-point exception is
unspecified, unless explicitly specified otherwise.

9

Whether or when library functions raise an undeserved ‘‘underflow’’ floating-point
exception is unspecified.

321)

Otherwise, as implied by F.7.6, the

<math.h>

functions do

not raise spurious floating-point exceptions (detectable by the user), other than the
‘‘inexact’’ floating-point exception.

10

Whether the functions honor the rounding direction mode is implementation-defined,
unless explicitly specified otherwise.

11

Functions with a NaN argument return a NaN result and raise no floating-point exception,
except where stated otherwise.

12

The specifications in the following subclauses append to the definitions in

<math.h>

.

For families of functions, the specifications apply to all of the functions even though only
the principal function is shown. Unless otherwise specified, where the symbol ‘‘

±

’’

occurs in both an argument and the result, the result has the same sign as the argument.

Recommended practice

13

If a function with one or more NaN arguments returns a NaN result, the result should be
the same as one of the NaN arguments (after possible type conversion), except perhaps
for the sign.

F.9.1 Trigonometric functions

F.9.1.1 The

acos

functions

1

acos(

1

)

returns +0.

acos(

x

)

returns a NaN and raises the ‘‘invalid’’ floating-point exception for

| > 1.

320) IEC 60559 allows different definitions of underflow. They all result in the same values, but differ on

when the floating-point exception is raised.

321) It is intended that undeserved ‘‘underflow’’ and ‘‘inexact’’ floating-point exceptions are raised only if

avoiding them would be too costly.

§F.9.1.1 IEC 60559 floating-point arithmetic

455

F.9.1.2 The

asin

functions

1

asin(

±

0

)

returns

±

0.

asin(

x

)

returns a NaN and raises the ‘‘invalid’’ floating-point exception for

| > 1.

F.9.1.3 The

atan

functions

1

atan(

±

0

)

returns

±

0.

atan(

±

)

returns

±

π

/2.

F.9.1.4 The

atan2

functions

1

atan2(

±

0

,

0

)

returns

±

π

.

322)

atan2(

±

0

,

+

0

)

returns

±

0.

atan2(

±

0

,

x

)

returns

±

π

for < 0.

atan2(

±

0

,

x

)

returns

±

0 for > 0.

atan2(

y

,

±

0

)

returns

π

/2 for < 0.

atan2(

y

,

±

0

)

returns

π

/2 for > 0.

atan2(

±

y

,

)

returns

±

π

for finite > 0.

atan2(

±

y

,

+

)

returns

±

0 for finite > 0.

atan2(

±

,

x

)

returns

±

π

/2 for finite x.

atan2(

±

,

)

returns

±

3

π

/4.

atan2(

±

,

+

)

returns

±

π

/4.

F.9.1.5 The

cos

functions

1

cos(

±

0

)

returns 1.

cos(

±

)

returns a NaN and raises the ‘‘invalid’’ floating-point exception.

F.9.1.6 The

sin

functions

1

sin(

±

0

)

returns

±

0.

sin(

±

)

returns a NaN and raises the ‘‘invalid’’ floating-point exception.

322)

atan2(

0

,

0

)

does not raise the ‘‘invalid’’ floating-point exception, nor does

atan2(

y

,

0

)

raise

the ‘‘divide-by-zero’’ floating-point exception.

456 IEC 60559 floating-point arithmetic

§F.9.1.6

F.9.1.7 The

tan

functions

1

tan(

±

0

)

returns

±

0.

tan(

±

)

returns a NaN and raises the ‘‘invalid’’ floating-point exception.

F.9.2 Hyperbolic functions

F.9.2.1 The

acosh

functions

1

acosh(

1

)

returns

+

0.

acosh(

x

)

returns a NaN and raises the ‘‘invalid’’ floating-point exception for < 1.

acosh(

+

)

returns

+

.

F.9.2.2 The

asinh

functions

1

asinh(

±

0

)

returns

±

0.

asinh(

±

)

returns

±

.

F.9.2.3 The

atanh

functions

1

atanh(

±

0

)

returns

±

0.

atanh(

±

1

)

returns

±

and raises the ‘‘divide-by-zero’’ floating-point exception.

atanh(

x

)

returns a NaN and raises the ‘‘invalid’’ floating-point exception for

| > 1.

F.9.2.4 The

cosh

functions

1

cosh(

±

0

)

returns 1.

cosh(

±

)

returns

+

.

F.9.2.5 The

sinh

functions

1

sinh(

±

0

)

returns

±

0.

sinh(

±

)

returns

±

.

F.9.2.6 The

tanh

functions

1

tanh(

±

0

)

returns

±

0.

tanh(

±

)

returns

±

1.

§F.9.2.6 IEC 60559 floating-point arithmetic

457

F.9.3 Exponential and logarithmic functions

F.9.3.1 The

exp

functions

1

exp(

±

0

)

returns 1.

exp(

)

returns

+

0.

exp(

+

)

returns

+

.

F.9.3.2 The

exp2

functions

1

exp2(

±

0

)

returns 1.

exp2(

)

returns

+

0.

exp2(

+

)

returns

+

.

F.9.3.3 The

expm1

functions

1

expm1(

±

0

)

returns

±

0.

expm1(

)

returns

1.

expm1(

+

)

returns

+

.

F.9.3.4 The

frexp

functions

1

frexp(

±

0

, exp)

returns

±

0, and stores 0 in the object pointed to by

exp

.

frexp(

±

,

exp)

returns

±

, and stores an unspecified value in the object

pointed to by

exp

.

frexp(

NaN

, exp)

stores an unspecified value in the object pointed to by

exp

(and returns a NaN).

2

frexp

raises no floating-point exceptions.

3

On a binary system, the body of the

frexp

function might be

{

*exp = (value == 0) ? 0 : (int)(1 + logb(value));

return scalbn(value, -(*exp));

}

F.9.3.5 The

ilogb

functions

1

If the correct result is outside the range of the return type, the numeric result is
unspecified and the ‘‘invalid’’ floating-point exception is raised.

458 IEC 60559 floating-point arithmetic

§F.9.3.5

F.9.3.6 The

ldexp

functions

1

On a binary system,

ldexp(x, exp)

is equivalent to

scalbn(x, exp)

.

F.9.3.7 The

log

functions

1

log(

±

0

)

returns

and raises the ‘‘divide-by-zero’’ floating-point exception.

log(

1

)

returns +0.

log(

x

)

returns a NaN and raises the ‘‘invalid’’ floating-point exception for < 0.

log(

+

)

returns

+

.

F.9.3.8 The

log10

functions

1

log10(

±

0

)

returns

and raises the ‘‘divide-by-zero’’ floating-point exception.

log10(

1

)

returns

+

0.

log10(

x

)

returns a NaN and raises the ‘‘invalid’’ floating-point exception for < 0.

log10(

+

)

returns

+

.

F.9.3.9 The

log1p

functions

1

log1p(

±

0

)

returns

±

0.

log1p(

1

)

returns

and raises the ‘‘divide-by-zero’’ floating-point exception.

log1p(

x

)

returns a NaN and raises the ‘‘invalid’’ floating-point exception for

<

1.

log1p(

+

)

returns

+

.

F.9.3.10 The

log2

functions

1

log2(

±

0

)

returns

and raises the ‘‘divide-by-zero’’ floating-point exception.

log2(

1

)

returns

+

0.

log2(

x

)

returns a NaN and raises the ‘‘invalid’’ floating-point exception for < 0.

log2(

+

)

returns

+

.

F.9.3.11 The

logb

functions

1

logb(

±

0

)

returns

and raises the ‘‘divide-by-zero’’ floating-point exception.

logb(

±

)

returns

+

.

§F.9.3.11 IEC 60559 floating-point arithmetic

459

F.9.3.12 The

modf

functions

1

modf(

±

x

, iptr)

returns a result with the same sign as x.

modf(

±

, iptr)

returns

±

0 and stores

±

in the object pointed to by

iptr

.

modf(

NaN

, iptr)

stores a NaN in the object pointed to by

iptr

(and returns a

NaN).

2

modf

behaves as though implemented by

#include <math.h>

#include <fenv.h>

#pragma STDC FENV_ACCESS ON

double modf(double value, double *iptr)

{

int save_round = fegetround();

fesetround(FE_TOWARDZERO);

*iptr = nearbyint(value);

fesetround(save_round);

return copysign(

isinf(value) ? 0.0 :

value - (*iptr), value);

}

F.9.3.13 The

scalbn

and

scalbln

functions

1

scalbn(

±

0

,

n

)

returns

±

0.

scalbn(

x

,

0

)

returns x.

scalbn(

±

,

n

)

returns

±

.

F.9.4 Power and absolute value functions

F.9.4.1 The

cbrt

functions

1

cbrt(

±

0

)

returns

±

0.

cbrt(

±

)

returns

±

.

F.9.4.2 The

fabs

functions

1

fabs(

±

0

)

returns

+

0.

fabs(

±

)

returns

+

.

460 IEC 60559 floating-point arithmetic

§F.9.4.2

F.9.4.3 The

hypot

functions

1

hypot(

x

,

y

)

,

hypot(

y

,

x

)

, and

hypot(

x

,

y

)

are equivalent.

hypot(

x

,

±

0

)

is equivalent to

fabs(

x

)

.

hypot(

±

,

y

)

returns

+

, even if is a NaN.

F.9.4.4 The

pow

functions

1

pow(

±

0

,

y

)

returns

±

and raises the ‘‘divide-by-zero’’ floating-point exception

for an odd integer < 0.

pow(

±

0

,

y

)

returns

+

and raises the ‘‘divide-by-zero’’ floating-point exception

for < 0 and not an odd integer.

pow(

±

0

,

y

)

returns

±

0 for an odd integer > 0.

pow(

±

0

,

y

)

returns

+

0 for > 0 and not an odd integer.

pow(

1

,

±

)

returns 1.

pow(

+

1

,

y

)

returns 1 for any y, even a NaN.

pow(

x

,

±

0

)

returns 1 for any x, even a NaN.

pow(

x

,

y

)

returns a NaN and raises the ‘‘invalid’’ floating-point exception for

finite < 0 and finite non-integer y.

pow(

x

,

)

returns

+

for | | < 1.

pow(

x

,

)

returns

+

0 for | | > 1.

pow(

x

,

+

)

returns

+

0 for | | < 1.

pow(

x

,

+

)

returns

+

for | | > 1.

pow(

,

y

)

returns

0 for an odd integer < 0.

pow(

,

y

)

returns

+

0 for < 0 and not an odd integer.

pow(

,

y

)

returns

for an odd integer > 0.

pow(

,

y

)

returns

+

for > 0 and not an odd integer.

pow(

+

,

y

)

returns

+

0 for < 0.

pow(

+

,

y

)

returns

+

for > 0.

§F.9.4.4 IEC 60559 floating-point arithmetic

461

F.9.4.5 The

sqrt

functions

1

sqrt

is fully specified as a basic arithmetic operation in IEC 60559.

F.9.5 Error and gamma functions

F.9.5.1 The

erf

functions

1

erf(

±

0

)

returns

±

0.

erf(

±

)

returns

±

1.

F.9.5.2 The

erfc

functions

1

erfc(

)

returns 2.

erfc(

+

)

returns

+

0.

F.9.5.3 The

lgamma

functions

1

lgamma(

1

)

returns +0.

lgamma(

2

)

returns +0.

lgamma(

x

)

returns

+

and raises the ‘‘divide-by-zero’’ floating-point exception for

a neg ative integer or zero.

lgamma(

)

returns

+

.

lgamma(

+

)

returns

+

.

F.9.5.4 The

tgamma

functions

1

tgamma(

±

0

)

returns

±

and raises the ‘‘divide-by-zero’’ floating-point exception.

tgamma(

x

)

returns a NaN and raises the ‘‘invalid’’ floating-point exception for a

negative integer.

tgamma(

)

returns a NaN and raises the ‘‘invalid’’ floating-point exception.

tgamma(

+

)

returns

+

.

F.9.6 Nearest integer functions

F.9.6.1 The

ceil

functions

1

ceil(

±

0

)

returns

±

0.

ceil(

±

)

returns

±

.

2

The

double

version of

ceil

behaves as though implemented by

462 IEC 60559 floating-point arithmetic

§F.9.6.1

#include <math.h>

#include <fenv.h>

#pragma STDC FENV_ACCESS ON

double ceil(double x)

{

double result;

int save_round = fegetround();

fesetround(FE_UPWARD);

result = rint(x); //

or

nearbyint

instead of

rint

fesetround(save_round);

return result;

}

F.9.6.2 The

floor

functions

1

floor(

±

0

)

returns

±

0.

floor(

±

)

returns

±

.

2

See the sample implementation for

ceil

in F.9.6.1.

F.9.6.3 The

nearbyint

functions

1

The

nearbyint

functions use IEC 60559 rounding according to the current rounding

direction. They do not raise the ‘‘inexact’’ floating-point exception if the result differs in
value from the argument.

nearbyint(

±

0

)

returns

±

0 (for all rounding directions).

nearbyint(

±

)

returns

±

(for all rounding directions).

F.9.6.4 The

rint

functions

1

The

rint

functions differ from the

nearbyint

functions only in that they do raise the

‘‘inexact’’ floating-point exception if the result differs in value from the argument.

F.9.6.5 The

lrint

and

llrint

functions

1

The

lrint

and

llrint

functions provide floating-to-integer conversion as prescribed

by IEC 60559. They round according to the current rounding direction. If the rounded
value is outside the range of the return type, the numeric result is unspecified and the
‘‘invalid’’ floating-point exception is raised. When they raise no other floating-point
exception and the result differs from the argument, they raise the ‘‘inexact’’ floating-point
exception.

§F.9.6.5 IEC 60559 floating-point arithmetic

463

F.9.6.6 The

round

functions

1

round(

±

0

)

returns

±

0.

round(

±

)

returns

±

.

2

The

double

version of

round

behaves as though implemented by

#include <math.h>

#include <fenv.h>

#pragma STDC FENV_ACCESS ON

double round(double x)

{

double result;

fenv_t save_env;

feholdexcept(&save_env);

result = rint(x);

if (fetestexcept(FE_INEXACT)) {

fesetround(FE_TOWARDZERO);

result = rint(copysign(0.5 + fabs(x), x));

}

feupdateenv(&save_env);

return result;

}

The

round

functions may, but are not required to, raise the ‘‘inexact’’ floating-point

exception for non-integer numeric arguments, as this implementation does.

F.9.6.7 The

lround

and

llround

functions

1

The

lround

and

llround

functions differ from the

lrint

and

llrint

functions

with the default rounding direction just in that the

lround

and

llround

functions

round halfway cases away from zero and need not raise the ‘‘inexact’’ floating-point
exception for non-integer arguments that round to within the range of the return type.

F.9.6.8 The

trunc

functions

1

The

trunc

functions use IEC 60559 rounding toward zero (regardless of the current

rounding direction).

trunc(

±

0

)

returns

±

0.

trunc(

±

)

returns

±

.

464 IEC 60559 floating-point arithmetic

§F.9.6.8

F.9.7 Remainder functions

F.9.7.1 The

fmod

functions

1

fmod(

±

0

,

y

)

returns

±

0 for not zero.

fmod(

x

,

y

)

returns a NaN and raises the ‘‘invalid’’ floating-point exception for x

infinite or zero.

fmod(

x

,

±

)

returns for not infinite.

2

The

double

version of

fmod

behaves as though implemented by

#include <math.h>

#include <fenv.h>

#pragma STDC FENV_ACCESS ON

double fmod(double x, double y)

{

double result;

result = remainder(fabs(x), (y = fabs(y)));

if (signbit(result)) result += y;

return copysign(result, x);

}

F.9.7.2 The

remainder

functions

1

The

remainder

functions are fully specified as a basic arithmetic operation in

IEC 60559.

F.9.7.3 The

remquo

functions

1

The

remquo

functions follow the specifications for the

remainder

functions. They

have no further specifications special to IEC 60559 implementations.

F.9.8 Manipulation functions

F.9.8.1 The

copysign

functions

1

copysign

is specified in the Appendix to IEC 60559.

F.9.8.2 The

nan

functions

1

All IEC 60559 implementations support quiet NaNs, in all floating formats.

§F.9.8.2 IEC 60559 floating-point arithmetic

465

F.9.8.3 The

nextafter

functions

1

nextafter(

x

,

y

)

raises the ‘‘overflow’’ and ‘‘inexact’’ floating-point exceptions

for finite and the function value infinite.

nextafter(

x

,

y

)

raises the ‘‘underflow’’ and ‘‘inexact’’ floating-point

exceptions for the function value subnormal or zero and x

y.

F.9.8.4 The

nexttoward

functions

1

No additional requirements beyond those on

nextafter

.

F.9.9 Maximum, minimum, and positive difference functions

F.9.9.1 The

fdim

functions

1

No additional requirements.

F.9.9.2 The

fmax

functions

1

If just one argument is a NaN, the

fmax

functions return the other argument (if both

arguments are NaNs, the functions return a NaN).

2

The body of the

fmax

function might be

323)

{ return (isgreaterequal(x, y) ||

isnan(y)) ? x : y; }

F.9.9.3 The

fmin

functions

1

The

fmin

functions are analogous to the

fmax

functions (see F.9.9.2).

F.9.10 Floating multiply-add

F.9.10.1 The

fma

functions

1

fma(

x

,

y

,

z

)

computes xy

+

z, correctly rounded once.

fma(

x

,

y

,

z

)

returns a NaN and optionally raises the ‘‘invalid’’ floating-point

exception if one of and is infinite, the other is zero, and is a NaN.

fma(

x

,

y

,

z

)

returns a NaN and raises the ‘‘invalid’’ floating-point exception if

one of and is infinite, the other is zero, and is not a NaN.

fma(

x

,

y

,

z

)

returns a NaN and raises the ‘‘invalid’’ floating-point exception if x

times is an exact infinity and is also an infinity but with the opposite sign.

323) Ideally,

fmax

would be sensitive to the sign of zero, for example

fmax(

0. 0

,

+

0. 0

)

would

return

+

0

; howev er, implementation in software might be impractical.

466 IEC 60559 floating-point arithmetic

§F.9.10.1

 

 

 

 

 

 

 

Content      ..     11      12      13      14     ..