Thursday, May 22, 2008
Wednesday, May 21, 2008
Unix Incompatibility Notes: Byte Order
Some processors (PowerPC, MIPS, DEC Alpha) can be either big-endian or little-endian depending on software settings.
Network byte order is the standard used in packets sent over the internet. It is big-endian (except that technically it refers to the order in which bytes are transmitted, not the order in which they are stored). If you are going to chose an arbitrary order to standardize on, network-byte order is a sensible choice.
The unix functions htonl(), htons(), ntohl(), and ntohs() convert longs and shorts back and forth between the host byte order and network byte order. However, though they are widely available, they are not universally available.
run time test:
int am_big_endian()
{
long one= 1;
return !(*((char *)(&one)));
//if *(char *)(&one) is 1, means the 1 is store is stored in the first byte, then it is little endian;
//if *(char *)(&one) is 0, means the 1 is store is not stored in the first byte, then it is big endian;
}
Wednesday, May 07, 2008
cfloat (float.h) - C Reference
cfloat (float.h) | header |
Characteristics of floating-point types
This header describes the characteristics of floating types for the specific system and compiler implemetation used.
A floating-point number is composed of four elements:
- a sign: either negative or non-negative
- a base (or radix): which expresses the amount of quantities that can be represented with a single digit (2 for binary, 10 for decimal, 16 for hexadecimal, and so on...)
- a significand (or mantissa): which is a series of digits of the abovementioned base. The number of digits in this series is what is known as precision.
- an exponent (also knon as characteristic, or scale): which represents the offset of the significand, which affects the value in the following way:
value of floating-point = significand x baseexponent, with its corresponding sign.
The following panel shows the name of the different values defined in this header and their minimal magnitudes (positive numbers may be greater in value, and negative number may be less in value). Any particular implementation may have characteristics with greater magnitudes than those shown here:
*Except for FLT_RADIX, names beginning with FLT apply to the float type, those with DBL to double and those with LDBL to long double.
name | min. | stands for | expresses |
---|---|---|---|
FLT_RADIX | 2 | RADIX | Base for all floating-point types (float, double and long double). |
FLT_MANT_DIG DBL_MANT_DIG LDBL_MANT_DIG | MANTissa DIGits | Precission of significand, i.e. the number of digits that conform the significand. | |
FLT_DIG DBL_DIG LDBL_DIG | 6 10 10 | DIGits | Number of decimal digits that can be rounded into a floating-point and back without change in the number of decimal digits. |
FLT_MIN_EXP DBL_MIN_EXP LDBL_MIN_EXP | MINimum EXPonent | Minimum negative integer value for the exponent that generates a normalized floating-point number. | |
FLT_MIN_10_EXP DBL_MIN_10_EXP LDBL_MIN_10_EXP | -37 -37 -37 | MINimum base-10 EXPonent | Minimum negative integer value for the exponent of a base-10 expression that whould generate a normalized floating-point number. |
FLT_MAX_EXP DBL_MAX_EXP LDBL_MAX_EXP | MAXimum EXPonent | Maximum integer value for the exponent that generates a normalized floating-point number. | |
FLT_MAX_10_EXP DBL_MAX_10_EXP LDBL_MAX_10_EXP | 37 37 37 | MAXimum base-10 EXPonent | Maximum integer value for the exponent of a base-10 expression that whould generate a normalized floating-point number. |
FLT_MAX DBL_MAX LDBL_MAX | 1E+37 1E+37 1E+37 | MAXimum | Maximum finite representable floating-point number. |
FLT_EPSILON DBL_EPSILON LDBL_EPSILON | 1E-5 1E-9 1E-9 | EPSILON | Difference between 1 and the least value greater than 1 that is representable. |
FLT_MIN DBL_MIN LDBL_MIN | 1E-37 1E-37 1E-37 | MINimum | Minimum representable floating-point number. |
See also
climits | Sizes of integral types (header) |