numeric constants

I

Ioannis Vranos

Consider the following uses of 0 as constants:



float f= 0f; // doesn't seem to work
float f= 0.f // Is this valid?
float f= 0.F // Is this valid?

float f= 0.0f; // It works
float f= 0.0F // Does 'F' mean float here?
float f= 0.0 // is 0.0 a double here?

double d= 0.0d // doesn't seem to work. Is there any double
// specification like 'f' for float?


long double d= 0.0l // Does 'l' mean long double here?
long double d= 0.0L // Does 'L' mean long double here?
// L is also used for wchar_t character constants
// and string literals.


long i= 1L; // Does 'L' mean long?
long i= 1l; // Does 'l' mean long?
 
A

Andrew Koenig

float f= 0f; // doesn't seem to work

Correct -- a floating-point literal must contain a decimal point or an
exponent.
float f= 0.f // Is this valid?
float f= 0.F // Is this valid?

Yes to both.
float f= 0.0f; // It works
Yes.

float f= 0.0F // Does 'F' mean float here?

Yes, F and f mean the same in this context.
float f= 0.0 // is 0.0 a double here?
Yes.

double d= 0.0d // doesn't seem to work. Is there any double
// specification like 'f' for float?

No -- if you omit the suffix, it's double.
long double d= 0.0l // Does 'l' mean long double here?

Yes -- there is no ambiguity because there must be a decimal point or an
exponent.
long double d= 0.0L // Does 'L' mean long double here?
Yes.

// L is also used for wchar_t character constants
// and string literals.
Indeed.

long i= 1L; // Does 'L' mean long?
long i= 1l; // Does 'l' mean long?

Yes.
 
J

Jerry Coffin

[email protected] says... said:
float f= 0f; // doesn't seem to work

Correct -- here's the syntax from the standard ($2.13.3):

floating-literal:
fractional-constant exponent-part[opt]
floating-suffix[opt]
digit-sequence exponent-part floating-suffix[opt]
fractional-constant:
digit-sequence[opt] . digit-sequence
digit-sequence .
[ ... ]
floating-suffix: one of
f l F L

So the "fractional-constant" must have a decimal point and at least one
digit either before or after the decimal point.
float f= 0.f // Is this valid?
float f= 0.F // Is this valid?

Yes to both.
float f= 0.0f; // It works
float f= 0.0F // Does 'F' mean float here?

Yes, in both cases.
float f= 0.0 // is 0.0 a double here?
Yes.

double d= 0.0d // doesn't seem to work. Is there any double
// specification like 'f' for float?

No, as you can see above, the only suffixes are F and L, in either upper
or lower case.
long double d= 0.0l // Does 'l' mean long double here?
long double d= 0.0L // Does 'L' mean long double here?

Yes, it does.
// L is also used for wchar_t character constants
// and string literals.

It's used as a prefix for string and character constants.
long i= 1L; // Does 'L' mean long?
long i= 1l; // Does 'l' mean long?

Yes to both. A suffix of "LL" denotes a long long in C99, and will
undoubtedly mean the same in C++ 0x. For integers, you can also use a
suffix of 'u' or 'U' to indicate an unsigned; this can be combined with
'l' or 'L' to indicate an unsigned long. These are all case insensitive,
so you can mix and match as you see fit, so the following are all
equivalent:

1uL
1UL
1ul
1Ul

Most people recommend that you only use the upper-case 'L' for this
purpose, as the lower-case version can be difficult to distinguish from
the digit one in many fonts.
 
P

Pete Becker

[email protected] says... said:
float f= 0f; // doesn't seem to work

Correct -- here's the syntax from the standard ($2.13.3):

floating-literal:
fractional-constant exponent-part[opt]
floating-suffix[opt]
digit-sequence exponent-part floating-suffix[opt]
fractional-constant:
digit-sequence[opt] . digit-sequence
digit-sequence .
[ ... ]
floating-suffix: one of
f l F L

So the "fractional-constant" must have a decimal point and at least one
digit either before or after the decimal point.

Just to make things more confusing:

float f = 0; // works
 
I

Ioannis Vranos

Pete said:
Just to make things more confusing:

float f = 0; // works

Yes, implicit conversion on assignment, inherited from C. Also this
should not be a problem.

int x= 1.4; style could be a problem.


I have been playing with Turbo Pascal 7.x lately, there, there are the
int(), trunc(), and round() functions that explicitly convert a floating
point value to an integer one, no implicit conversion exists.

C++ could have used this approach for type safety since the beginning,
or we can even have C++0x/1x require a warning for such possible,
implicit truncating conversions, and provide some similar functionality
(or just resort only to static_cast?) for taking the integer part of,
truncating or rounding a value.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top