floating point conversions && how to read standards

Discussion in 'C++' started by Stanley Rice, Oct 7, 2011.

  1. Stanley Rice

    Stanley Rice Guest

    Hi all, I got confused while referencing to the 4.8/1 in draft n3242.

    4.8 Floating point conversions
    A prvalue of floating point type can be converted to a prvalue of
    another floating point type. If the source value can be exactly
    represented in the destination type, the result of the conversion is
    that exactly representation. If the source value is between two
    adjacent destination values, the result of the conversion is an
    implementation-defined choice of either of those values. Otherwise,
    the behavior is undefined.

    I do not exactly know what the 'destination type' and 'two adjacent
    destination value' mean. Could you explain to me and show me some
    example if possible.

    What's more, I found reading the standard is quite difficult. There
    are so many new terms that were never heard before. I have to keep
    cross referencing, which in return, I forget where I got started. Is
    there experienced ones who can give me some suggestions about how to
    read standards.
    Stanley Rice, Oct 7, 2011
    #1
    1. Advertising

  2. Stanley Rice <> wrote:
    > Hi all, I got confused while referencing to the 4.8/1 in draft n3242.
    >
    > 4.8 Floating point conversions
    > A prvalue of floating point type can be converted to a prvalue of
    > another floating point type. If the source value can be exactly
    > represented in the destination type, the result of the conversion is
    > that exactly representation. If the source value is between two
    > adjacent destination values, the result of the conversion is an
    > implementation-defined choice of either of those values. Otherwise,
    > the behavior is undefined.
    >
    > I do not exactly know what the 'destination type' and 'two adjacent
    > destination value' mean. Could you explain to me and show me some
    > example if possible.


    A variable of type 'float' is (usually) smaller than one of type 'double'
    (which means that less bits are reserved for the mantissa and for the
    exponent in the former than in the latter; less bits in the mantissa
    means in practice that the values that can be represented are less
    accurate, as not so many digits can be stored in the variable).

    Since a 'float' has less accuracy than a 'double', that means that
    there are many values of type 'double' that cannot be accurately
    represented with a 'float'. The value in question will be between two
    values that can be represented with 'float'.

    Thus if you assign a value of type 'double' to a variable of type
    'float', it's up to the implementation to decide whether to round the
    value up or down to the nearest representable value in the latter.
    Juha Nieminen, Oct 7, 2011
    #2
    1. Advertising

  3. Stanley Rice Wrote:

    > Hi all, I got confused while referencing to the 4.8/1 in draft n3242.
    >
    > 4.8 Floating point conversions
    > A prvalue of floating point type can be converted to a prvalue of
    > another floating point type. If the source value can be exactly
    > represented in the destination type, the result of the conversion is
    > that exactly representation. If the source value is between two
    > adjacent destination values, the result of the conversion is an
    > implementation-defined choice of either of those values. Otherwise,
    > the behavior is undefined.
    >
    > I do not exactly know what the 'destination type' and 'two adjacent
    > destination value' mean. Could you explain to me and show me some
    > example if possible.


    Lets assume, for the purpose of this explanation, that you have a system
    where double can represent 3 decimal digits of precision and float can
    represent 2 decimal digits of precision.
    On such a system, if you have the initialisation

    float pi = 3.14;

    the constrant 3.14 (of type double) has to be converted to type float.
    This makes float the 'destination type' of clause 4.8.
    As float can not represent the value 3.14 exactly (given that float can only
    represent 2 digits), the compiler has to round the value to one that can be
    represented in float. This would be either 3.1f or 3.2f, which are the two
    nearest representable values.

    >
    > What's more, I found reading the standard is quite difficult. There
    > are so many new terms that were never heard before. I have to
    > keep cross referencing, which in return, I forget where I got started.
    > Is there experienced ones who can give me some suggestions
    > about how to read standards.


    Reading standards is not for the faint of heart. They are not meant as a
    teaching aid, but are more like a law-text.
    The only advice I can give is to keep practicing. With practice, the common
    terms and the phraseology become more familiar.

    Bart v Ingen Schenau
    Bart van Ingen Schenau, Oct 7, 2011
    #3
  4. Stanley Rice

    Stanley Rice Guest

    On Oct 7, 4:05 pm, Juha Nieminen <> wrote:
    > Stanley Rice <> wrote:
    > > Hi all, I got confused while referencing to the 4.8/1 in draft n3242.

    >
    > > 4.8 Floating point conversions
    > > A prvalue of floating point type can be converted to a prvalue of
    > > another floating point type. If the source value can be exactly
    > > represented in the destination type, the result of the conversion is
    > > that exactly representation. If the source value is between two
    > > adjacent destination values, the result of the conversion is an
    > > implementation-defined choice of either of those values. Otherwise,
    > > the behavior is undefined.

    >
    > > I do not exactly know what the 'destination type' and 'two adjacent
    > > destination value' mean. Could you explain to me and show me some
    > > example if possible.

    >
    >   A variable of type 'float' is (usually) smaller than one of type 'double'
    > (which means that less bits are reserved for the mantissa and for the
    > exponent in the former than in the latter; less bits in the mantissa
    > means in practice that the values that can be represented are less
    > accurate, as not so many digits can be stored in the variable).
    >
    >   Since a 'float' has less accuracy than a 'double', that means that
    > there are many values of type 'double' that cannot be accurately
    > represented with a 'float'. The value in question will be between two
    > values that can be represented with 'float'.
    >
    >   Thus if you assign a value of type 'double' to a variable of type
    > 'float', it's up to the implementation to decide whether to round the
    > value up or down to the nearest representable value in the latter.


    Thanks for you replying. And I got your idea. I think I was stuck in
    the phase "the floating point types" at first. Does the floating piont
    types means that types which consist of a dicimal point, and it
    includes type *float* and type *double*, right? It it not just the
    type *float*.

    Similarly, "unsigned integer type" includes either of *unsigned char*,
    *unsigned int*, *unsigned long*, etc. And it is not just the type
    *int*, right?

    I am not a native English speaker, and eager to get close to the
    standard, thanks again for your replying.
    Stanley Rice, Oct 8, 2011
    #4
  5. Stanley Rice

    Stanley Rice Guest

    On Oct 7, 5:23 pm, Bart van Ingen Schenau <>
    wrote:
    > Stanley Rice Wrote:
    >
    > > Hi all, I got confused while referencing to the 4.8/1 in draft n3242.

    >
    > > 4.8 Floating point conversions
    > > A prvalue of floating point type can be converted to a prvalue of
    > > another floating point type. If the source value can be exactly
    > > represented in the destination type, the result of the conversion is
    > > that exactly representation. If the source value is between two
    > > adjacent destination values, the result of the conversion is an
    > > implementation-defined choice of either of those values. Otherwise,
    > > the behavior is undefined.

    >
    > > I do not exactly know what the 'destination type' and 'two adjacent
    > > destination value' mean. Could you explain to me and show me some
    > > example if possible.

    >
    > Lets assume, for the purpose of this explanation, that you have a system
    > where double can represent 3 decimal digits of precision and float can
    > represent 2 decimal digits of precision.
    > On such a system, if you have the initialisation
    >
    >   float pi = 3.14;
    >
    > the constrant 3.14 (of type double) has to be converted to type float.
    > This makes float the 'destination type' of clause 4.8.
    > As float can not represent the value 3.14 exactly (given that float can only
    > represent 2 digits), the compiler has to round the value to one that can be
    > represented in float. This would be either 3.1f or 3.2f, which are the two
    > nearest representable values.
    >
    >
    >
    > > What's more, I found reading the standard is quite difficult. There
    > > are so many new terms that were never heard before. I have to
    > > keep cross referencing, which in return, I forget where I got started.
    > > Is there experienced ones who can give me some suggestions
    > > about how to read standards.

    >
    > Reading standards is not for the faint of heart. They are not meant as a
    > teaching aid, but are more like a law-text.
    > The only advice I can give is to keep practicing. With practice, the common
    > terms and the phraseology become more familiar.
    >
    > Bart v Ingen Schenau


    Greatly thanks for your detail explanation. There is no way but to
    keep reading it. Practise makes perfect. And I suppose I would keep
    posting questions here, wish you generous to help me.
    Stanley Rice, Oct 8, 2011
    #5
    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. Kai Koehne
    Replies:
    10
    Views:
    1,154
    Patricia Shanahan
    May 15, 2006
  2. John Fouhy

    Floating point -> string conversions

    John Fouhy, Nov 8, 2004, in forum: Python
    Replies:
    5
    Views:
    332
    John Fouhy
    Nov 19, 2004
  3. Replies:
    10
    Views:
    704
    Jasen Betts
    Aug 5, 2005
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,320
    Saraswati lakki
    Jan 6, 2012
  5. teeshift
    Replies:
    2
    Views:
    249
    Chris Pearl
    Dec 1, 2006
Loading...

Share This Page