Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Using type prefixes with floating point constants

Reply
Thread Tools

Using type prefixes with floating point constants

 
 
Ioannis Vranos
Guest
Posts: n/a
 
      03-26-2009
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;
}
 
Reply With Quote
 
 
 
 
Bart van Ingen Schenau
Guest
Posts: n/a
 
      03-27-2009
On Mar 26, 2:54*pm, Ioannis Vranos <(E-Mail Removed)>
wrote:
> 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?
>

I don't know about best practice---I don't use floating point often
enough for that---, but it certainly is not needed to always specify
the suffixes.

First of all, the majority of floating point constants are not exactly
representable in any of the floating point types, so you get the
conversion to the next higher or next lower representable value
anyway.
If float and double don't have the same range and precision, then a
compiler must be really perverse to get different results for the
expressions '(float)0.33439' and '0.33439F'.

The only suffix with more than documentary value is L (or l), if you
need the extra precision or range that long double might give you.

Bart v Ingen Schenau
 
Reply With Quote
 
 
 
 
Ioannis Vranos
Guest
Posts: n/a
 
      03-27-2009
Bart van Ingen Schenau wrote:
> On Mar 26, 2:54 pm, Ioannis Vranos <(E-Mail Removed)>
> wrote:
>> 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?
>>

> I don't know about best practice---I don't use floating point often
> enough for that---, but it certainly is not needed to always specify
> the suffixes.
>
> First of all, the majority of floating point constants are not exactly
> representable in any of the floating point types, so you get the
> conversion to the next higher or next lower representable value
> anyway.
> If float and double don't have the same range and precision, then a
> compiler must be really perverse to get different results for the
> expressions '(float)0.33439' and '0.33439F'.
>
> The only suffix with more than documentary value is L (or l), if you
> need the extra precision or range that long double might give you.
>
> Bart v Ingen Schenau



Thank you for your answer, the discussion is continued in thread:

"Corrected: Using type suffixes with floating point constants"

where I will forward your answer.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Corrected: Using type suffixes with floating point constants Ioannis Vranos C++ 10 03-31-2009 12:35 PM
Corrected: Using type suffixes with floating point constants Ioannis Vranos C Programming 8 03-28-2009 10:35 AM
Using type prefixes with floating point constants Ioannis Vranos C Programming 4 03-27-2009 04:12 PM
Using type prefixes with floating point constants Ioannis Vranos C++ 0 03-26-2009 01:57 PM
Prefixes _ and __ for object and class constants proposed Xeno Campanoli Ruby 2 01-25-2008 07:53 PM



Advertisments