Question about numeric literals postfixes

I

Ioannis Vranos

OK we have

long x= 12345L;

and it make sense because the default is int and would be truncated if
the value was larger than numeric_limits<long>::max().


However what is the need of F/f in the following:


float x= 4.1F;


since the default double conversion would not be surpassed by any float
value.
 
J

James Kanze

OK we have
long x= 12345L;
and it make sense because the default is int and would be truncated if
the value was larger than numeric_limits<long>::max().

I'm not sure I understand (even after having seen your
correction). Without the L, the literal will have type int if
it will fit into an int, long otherwise (and the code is illegal
if the literal isn't representable in a long). With the L, the
literal has type long.

In this particular case, the only purpose the L serves is
documentation. And habit---in other cases, the type can make a
difference, and it's good to get into the habit of using the L
anytime you want a long.
However what is the need of F/f in the following:
float x= 4.1F;
since the default double conversion would not be surpassed by
any float value.

I suppose it could conceivably make a difference in the way the
value was rounded, although I doubt it. It definitely would
make a difference in cases where other operations follow: 4.1F
is NOT the same value, internally, as 4.1. (Since an
implementation is allowed to use extended precision internally,
in all cases where a literal may appear, they might behave as
the same value. At least with g++ and Sun CC, on a Sparc, they
don't, however.)

Again, it's a good habit to get into, even if it doesn't make a
difference here (except that I can't off hand think of a case
where you would necessarily want a float, rather than a double).
 
J

Jerry Coffin

[ ... ]
However what is the need of F/f in the following:

float x= 4.1F;

since the default double conversion would not be surpassed by any float
value.

I'm not sure what you're saying about "surpassed", but mostly it
prevents the compiler from giving you a warning about converting a
double to a float. Some compilers look at the value when it's present,
and suppress the warning if it fits in a float. Others look only at the
types, and give a warning for any narrowing conversion.
 

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
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top