Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Varying Floating-Point Precision


July 1993/Varying Floating-Point Precision/Listing 1

Listing 1 real_t.h — contains all the needed preprocessor gymnastics to make different precision versions of the same application

/* REAL_T.H */

#if !defined ( REAL_T_INCLUDED )
#define REAL_T_INCLUDED

/* Minimum numbers before sqrt()fails */
#define FLT_SQRT_MIN  1.0e-19F
#define DBL_SQRT_MIN  1.0e-154
#define LDBL_SQRT_MIN 1.0e-2466L

#if defined ( REAL_FLT )
   /* Type and constant macro for float */
   typedef float real_t;
   #define RC( x ) ( x##F )

   /* float.h constants */
   #define REAL_DIG        FLT_DIG
   #define REAL_EPSILON    FLT_EPSILON
   #define REAL_GUARD      FLT_GUARD
   #define REAL_MANT_DIG   FLT_MANT_DIG
   #define REAL_MAX        FLT_MAX
   #define REAL_MAX_10_EXP FLT_MAX_10_EXP
   #define REAL_MAX_EXP    FLT_MAX_EXP
   #define REAL_MIN        FLT_MIN
   #define REAL_MIN_10_EXP FLT_MIN_10_EXP
   #define REAL_MIN_EXP    FLT_MIN_EXP
   #define REAL_NORMALIZE  FLT_NORMALIZE
   #define REAL_RADIX      FLT_RADIX
   #define REAL_ROUNDS     FLT_ROUNDS
   #define REAL_SQRT_MIN   FLT_SQRT_MIN
   #if !defined ( REAL_MP )
   #undef DBL_DIG
   #undef DBL_EPSILON
   #undef DBL_MANT_DIG
   #undef DBL_MAX
   #undef DBL_MAX_10_EXP
   #undef DBL_MAX_EXP
   #undef DBL_MIN
   #undef DBL_MIN_10_EXP
   #undef DBL_MIN_EXP
   #undef DBL_RADIX
   #undef DBL_ROUNDS
   #undef DBL_SQRT_MIN
   #undef LDBL_DIG
   #undef LDBL_EPSILON
   #undef LDBL_MANT_DIG
   #undef LDBL_MAX
   #undef LDBL_MAX_10_EXP
   #under LDBL_MAX_EXP
   #undef LDBL_MIN
   #undef LDBL_MIN_10_EXP
   #undef LDBL_MIN_EXP
   #undef LDBL_RADIX
   #undef LDBL_ROUNDS
   #undef LDBL_SQRT_MIN
   #define DBL_DIG         REAL_DIG
   #define DBL_EPSILON     REAL_EPSILON
   #define DBL_MANT_DIG    REAL_MANT_DIG
   #define DBL_MAX         REAL_MAX
   #define DBL_MAX_10_EXP  REAL_MAX_10_EXP
   #define DBL_MAX_EXP     REAL_MAX_EXP
   #define DBL_MIN         REAL_MIN

   #define DBL_MIN_10_ EXP REAL_MIN_10_EXP
   #define DBL_MIN_EXP     REAL_MIN_EXP
   #define DBL_RADIX       REAL_RADIX
   #define DBL_ROUNDS      REAL_ROUNDS
   #define DBL_SQRT_MIN    REAL_SQRT_MIN
   #define LDBL_DIG        REAL_DIG
   #define LDBL_EPSILON    REAL_EPSILON
   #define LDBL_MANT_DIG   REAL_MANT_DIG
   #define LDBL_MAX        REAL_ MAX
   #define LDBL_MAX_10_EXP REAL_MAX_10_EXP
   #define LDBL_MAX_EXP    REAL_MAX_EXP
   #define LDBL_MIN        REAL_MIN
   #define LDBL_MIN_10_EXP REAL_MIN_10_EXP
   #define LDBL_MIN_EXP    REAL_MIN_EXP
   #define LDBL_RADIX      REAL_RADIX
   #define LDBL_ROUNDS     REAL_ROUNDS
   #define LDBL_SQRT_MIN   REAL_SQRT_MIN
   typedef long double long_double;
   #define long_double real_t
   #define double real_t
   #endif   /* #if !defined ( REAL_MP ) */

   /* math.h functions */
   #define acos( x ) ( (float)acos((double)(x) ) )
   #define asin( x ) ( (float)asin((double)(x) ) )
   #define atan( x ) ( (float)atan((double)(x) ) )
   #define atan2( y, x ) ( (float)atan2((double)(y), \
        (double)(x) ) )
   #define ceil( x ) ( (float)ceil( (double)(x) ) )
   #define cos( x ) ( (float)cos( (double)(x) ) )
   #define cosh( x ) ( (float)cosh( (double)(x) ) )
   #define exp( x ) ( (float)exp( (double)(x) ) )
   #define fabs( x ) ( (float)fabs( (double)(x) ) )
   #define floor( x ) ( (float)floor( (double)(x) ) )
   #define fmod( x, y ) ( (float)fmod( (double)(x), \
        (double)(y) ) )
   #define sin( x ) ( (float)sin( (double)(x) ) )
   #define sinh( x ) ( (float)sinh( (double)(x) ) )
   #define tan( x ) ( (float)tan( (double)(x) ) )
   #define tanh( x ) ( (float)tanh( (double)(x) ) )
   #if defined ( REAL_SAFE )
   #define atof( x )\
        ( ( (x) == NULL ) ? \
        RC( 0.0 ) : (float)atof( (x) ) )
   #define log( x ) \
        ( ( fabs( (x) ) ) < REAL_MIN ? \
        (float)log( (double)REAL_MIN) : \
        (float)log( (double)fabs( (x) ) ) )
   #define log10( x ) \
        ( ( fabs( x ) ) < REAL_MIN ? \
        (float)log10((double)REAL_MIN) : \
        (float)log10( (double)fabs( (x) ) ) )
   #define pow( x, y ) \
        ( fabs( (y) ) < REAL_MIN ? \
        RC ( 1.0 ) : \
        ( fabs( (x) ) < REAL MIN ? \
        ( (y)<RC(0.0) ? \
        REAL_MAX : RC( 0.0 ) ) : \
        (float)pow( (double)(x), (double)(y) ) ) )
   #define sqrt( x ) \
        ( fabs( (x) ) < REAL_SQRT_MIN ? \
        RC( 0.0 ) : (float)sqrt( double)fabs( (x) ) ) )
   #else
   #define atof( x ) ( (float)atof( (x) ) )
   #define log( x ) ( (float)log( (double)(x) ) )
   #define 1og10( x ) ( (float)log10( (double)(x) ) )
   #define pow( x, y ) ( (float)pow( (double)(x), \
        (double)(y) ) )
   #define sqrt( x ) ( (float)sqrt( (double)(x) ) )
   #endif   /* #if defined ( REAL_SAFE ) */

#else
#if defined ( REAL_LDBL )
   /* Type and constant macro for long double */
   typedef long double real_t;
   typedef long double long_double;
   #define RC( x ) ( x##L )

   /* float.h constants */
   #define REAL_DIG        LDBL_DIG
   #define REAL_EPSILON    LDBL_EPSILON
   #define REAL_MANT_DIG   LDBL_MANT_DIG
   #define REAL_MAX        LDBL_MAX
   #define REAL_MAX_10_EXP LDBL_MAX_10_EXP
   #define REAL_MAX_EXP    LDBL_MAX_EXP
   #define REAL_MIN        LDBL_MIN
   #define REAL_MIN_10_EXP LDBL_MIN_10_EXP
   #define REAL_MIN_EXP    LDBL_MIN_EXP
   #define REAL_RADIX      LDBL_RADIX
   #define REAL_ROUNDS     LDBL_ROUNDS
   #define REAL_SQRT_MIN   LDBL_SQRT_MIN
   #if !defined ( REAL_MP )
   #undef FLT_DIG
   #undef FLT_EPSILON
   #undef FLT_MANT_DIG
   #undef FLT_MAX
   #undef FLT_MAX_10_EXP
   #undef FLT_MAX_EXP
   #undef FLT_MIN
   #undef FLT_MIN_10_EXP
   #undef FLT_MIN_EXP
   #undef FLT_RADIX
   #undef FLT_ROUNDS
   #undef FLT_SQRT_MIN
   #undef DBL_DIG
   #undef DBL_EPSILON
   #undef DBL_MANT_DIG
   #undef DBL_MAX
   #undef DBL_MAX_10_EXP
   #undef DBL_MAX_EXP
   #undef DBL_MIN
   #undef DBL_MIN_10_EXP
   #undef DBL_MIN_EXP
   #undef DBL_RADIX
   #undef DBL_ROUNDS
   #undef DBL_SQRT_MIN
   #define DBL_DIG          REAL_DIG
   #define DBL_EPSILON      REAL_EPSILON
   #define DBL_MANT_DIG     REAL_MANT_DIG
   #define DBL_MAX          REAL_MAX
   #define DBL-MAX_10_EXP  REAL_MAX_10_EXP
   #define DBL_MAX_EXP     REAL_MAX_EXP
   #define DBL_MIN         REAL_MIN
   #define DBL_MIN_10_EXP  REAL_MIN_10_EXP
   #define DBL_MIN_EXP     REAL_MIN_EXP
   #define DBL_RADIX       REAL_RADIX
   #define DBL_ROUNDS      REAL_ROUNDS
   #define DBL_SQRT_MIN    REAL_SQRT_MIN
   #define FLT_DIG         REAL_DIG
   #define FLT_EPSILON     REAL_EPSILON
   #define FLT_MANT_DIG    REAL_MANT_DIG
   #define FLT_MAX         REAL_MAX
   #define FLT_MAX_10_EXP  REAL_MAX_10_EXP
   #define FLT_MAX_EXP     REAL_MAX_EXP
   #define FLT_MIN         REAL_MIN
   #define FLT_MIN_10_EXP  REAL_MIN_10_EXP
   #define FLT_MIN_EXP     REAL_MIN_EXP
   #define FLT_RADIX       REAL_RADIX
   #define FLT_ROUNDS      REAL_ROUNDS
   #define FLT_SQRT_MIN    REAL_SQRT_MIN
    #define float real_t
   #define double real_t
   #endif   /* #if !defined ( REAL_MP ) */

   /* math.h functions */
   #define acos acosl
   #define asin asinl

   #define atan atanl
   #define atan2 atan2l
   #define ceil ceill
   #define cos cosl
   #define cosh coshl
   #define exp expl
   #define fabs fabsl
   #define floor floorl
   #define fmod fmodl
   #define sin sinl
   #define sinh sinhl
   #define tan tanl
   #define tanh tanhl
   #if defined ( REAL_SAFE )
   #define atof( x ) \
        ( ((x)  == NULL ) ? \
        RC( 0.0 ) : (float)atofl( (x) ) )
   #define log( x ) \
        ( ( fabsl( (x) ) ) < REAL_MIN ? \
        logl( REAL_MIN ) : logl( fabsl( (x) ) ) )
   #define log10( x ) \
        ( ( fabsl( x ) ) < REAL_MIN ? \
        log10l( REAL_MIN ) : log10l( fabs( x ) ) )
   #define pow( x, y ) \
        ( fabsl( (y) ) < REAL_MIN ? \
        RC( 1.0 ) : \
        ( fabsl( (x) ) < REAL_MIN ? \
        ( (y) < RC( 0.0 ) ) : \
        REAL_MAX : RC( 0.0 ) ) : \
        (float)powl( (x), (y) ) ) )
   #define sqrt( x ) \
        ( fabsl( (x) ) < REAL_SQRT_MIN ? \
        RC( 0.0 ) : sqrtl( fabsl( (x) ) ) )
   #else
   #define atof atofl
   #define log logl
   #define log10 log101
   #define pow powl
   #define sqrt sqrtl
   #endif   /* #if defined ( REAL_SAFE ) */

#else    /* REAL_DBL is default */
   /* Type and constant macro for double */
   typedef double real_t;
   #define RC( x ) ( x )

   /* float.h constants */
   #define REAL_DIG        DBL_DIG
   #define REAL_EPSILON    DBL_EPSILON
   #define REAL_MANT_DIG   DBL_MANT_DIG
   #define REAL_MAX        DBL_MAX
   #define REAL_MAX_10_EXP DBL_MAX_10_EXP
   #define REAL_MAX_EXP    DBL_MAX_EXP
   #define REAL_MIN        DBL_MIN
   #define REAL_MIN_10_EXP DBL_MIN_10_EXP
   #define REAL_MIN_EXP    DBL_MIN_EXP
   #define REAL_RADIX      DBL_RADIX
   #define REAL_ROUNDS     DBL_ROUNDS
   #define REAL_SQRT_MIN   DBL_SQRT_MIN
   #if !defined ( REAL_MP )
   #undef FLT_DIG
   #undef FLT_EPSILON
   #undef FLT_MANT_DIG
   #undef FLT_MAX
   #undef FLT_MAX_10_EXP
   #undef FLT_MAX_EXP
   #undef FLT_MIN
   #undef FLT_MIN_10_EXP
   #undef FLT_MIN_EXP
   #undef FLT_RADIX
   #undef FLT_ROUNDS
   #undef FLT_SQRT_MIN
   #undef LDBL_DIG
   #undef LDBL_EPSILON
   #undef LDBL_MANT_DIG
   #undef LDBL_MAX
   #undef LDBL_MAX_10_EXP
   #undef LDBL_MAX_EXP
   #undef LDBL_MIN
   #undef LDBL_MIN_10_EXP
   #undef LDBL_MIN_EXP
   #undef LDBL_RADIX
   #undef LDBL_ROUNDS
   #undef LDBL_SQRT_MIN
   #define FLT_DIG         REAL_DIG
   #define FLT_EPSILON     REAL_EPSILON
   #define FLT_MANT_DIG    REAL_MANT_DIG
   #define FLT_MAX         REAL_MAX
   #define FLT_MAX_10_EXP  REAL_MAX_10_EXP
   #define FLT_MAX_EXP     REAL_MAX_EXP
   #define FLT_MIN         REAL_MIN
   #define FLT_MIN_10_EXP  REAL_MIN_10_EXP
   #define FLT_MIN_EXP     REAL_MIN_EXP
   #define FLT_RADIX       REAL_RADIX
   #define FLT_ROUNDS      REAL_ROUNDS
   #define LDBL_DIG        REAL_DIG
   #define LDBL_EPSILON    REAL_EPSILON
   #define LDBL_MANT_DIG   REAL_MANT_DIG
   #define LDBL_MAX        REAL_MAX
   #define LDBL_MAX_10_EXP REAL_MAX_10_EXP
   #define LDBL_MAX_EXP    REAL_MAX_EXP
   #define LDBL_MIN        REAL_MIN
   #define LDBL_MIN_10_EXP REAL_MIN_10_EXP

   #define LDBL_MIN_EXP    REAL_MIN_EXP
   #define LDBL_RADIX      REAL_RADIX
   #define LDBL_ROUNDS     REAL_ROUNDS
   typedef long double long_double;
   #define long_double real_t
   #define float real_t
   #endif   /* #if !defined ( REAL_MP ) */

   /* math.h functions */
   #if defined ( REAL_SAFE )
   #define atof( x ) \
        ( ( (x) == NULL ) ? \
        RC( 0.0 ) : (float)atof( (x) ) )
   #define log( x ) \
        ( ( fabs( (x) ) ) < REAL_MIN ? \
        log( REAL_MIN ) : log( fabs( (x) ) ) )
   #define log10( x ) \
        ( ( fabs( x ) ) < REAL_MIN ? \
        log10( REAL_MIN ) : log10( fabs( x ) ) )
   #define pow( x, y ) \
        ( fabs( (y) ) < REAL_MIN ? \
        RC( 1.0 ) : \
        ( fabs( (x) ) < REAL_MIN ? \
        ( (y) < RC( 0.0 ) ? \
        REAL_MAX : RC( 0.0 ) ) : \
        (float)pow( (x), (y) ) ) )
   #define sqrt( x ) \
        ( fabs( (x) ) < REAL_SQRT_MIN ? \
        RC( 0.0 ) : sqrtl( fabs( (x) ) ) )
   #endif   /* #if defined ( REAL_SAFE ) */

#endif   /* #if defined ( REAL_LDBL ) */
#endif   /* #if defined ( REAL_FLT ) */

/* kludge for long double */
#if defined ( REAL_NL ) && !defined ( REAL_MP )
/define long
#endif

#endif   /* #if defined ( REAL_T_INCLUDED ) */

/* End of File */

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.