Using type prefixes with floating point constants

Discussion in 'C++' started by Ioannis Vranos, Mar 26, 2009.

  1. ISO/IEC 9899:1990/1995 says (from K&R2):

    “A6.4

    When a less precise floating value is converted to an equally or more
    precise floating type, the value is unchanged.

    ==> When a more precise floating value is converted to a less precise
    floating type, and the value is within representable range, the result
    may be either the next higher or the next lower representable value.

    If the result is out of range, the behavior is undefined”.


    Question: Does the above mean that it is a good practice or *always*
    needed to use the appropriate type suffixes with floating point constants?


    An example of this:


    #include <iostream>


    int main(void)
    {
    using namespace std;

    float f1 = 0.33439F;

    float f2= 0.33439f;

    cout<< "\nf1= "<< f1<<", f2= "<< f2<< endl;


    double d1= 0.33439;

    double d2= 0.33439;

    cout<< "\nd1= "<< d1<<", d2= "<< d2<< endl;


    // It doesn't work with MINGW, compiler is broken regarding
    // long double.
    long double ld1= 0.33439L;

    long double ld2= 0.33439l; // 'l' is the lower case 'L'.

    cout<< "\nld1= "<< ld1<<", ld2= "<< ld2<< endl;


    return 0;
    }
    Ioannis Vranos, Mar 26, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ioannis Vranos
    Replies:
    2
    Views:
    273
    Ioannis Vranos
    Mar 27, 2009
  2. Ioannis Vranos

    Using type prefixes with floating point constants

    Ioannis Vranos, Mar 26, 2009, in forum: C Programming
    Replies:
    4
    Views:
    316
    Keith Thompson
    Mar 27, 2009
  3. Ioannis Vranos
    Replies:
    8
    Views:
    321
    James Kuyper
    Mar 28, 2009
  4. Ioannis Vranos
    Replies:
    10
    Views:
    528
    Victor Bazarov
    Mar 31, 2009
  5. Saraswati lakki
    Replies:
    0
    Views:
    1,298
    Saraswati lakki
    Jan 6, 2012
Loading...

Share This Page