Tech and Academic things for Chianshin

Thursday, May 22, 2008

Eddie's Basic Guide to C Programming

Eddie's Basic Guide to C Programming

Wednesday, May 21, 2008

Unix Incompatibility Notes: Byte Order

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) - 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.

namemin.stands forexpresses
FLT_RADIX2RADIXBase for all floating-point types (float, double and long double).
FLT_MANT_DIG
DBL_MANT_DIG
LDBL_MANT_DIG

MANTissa DIGitsPrecission of significand, i.e. the number of digits that conform the significand.
FLT_DIG
DBL_DIG
LDBL_DIG
6
10
10
DIGitsNumber 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 EXPonentMinimum 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 EXPonentMinimum 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 EXPonentMaximum 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 EXPonentMaximum 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
MAXimumMaximum finite representable floating-point number.
FLT_EPSILON
DBL_EPSILON
LDBL_EPSILON
1E-5
1E-9
1E-9
EPSILONDifference between 1 and the least value greater than 1 that is representable.
FLT_MIN
DBL_MIN
LDBL_MIN
1E-37
1E-37
1E-37
MINimumMinimum representable floating-point number.

See also

climits Sizes of integral types (header)