473,387 Members | 1,664 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

numeric constants

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?
Jan 12 '08 #1
4 11126
"Ioannis Vranos" <jo**@no.spamwrote in message
news:fm*********@ulysses.noc.ntua.gr...
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.
Jan 12 '08 #2
In article <fm*********@ulysses.noc.ntua.gr>, jo**@no.spam says...

[ ... ]
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jan 12 '08 #3
On 2008-01-12 11:59:43 -0500, Jerry Coffin <jc*****@taeus.comsaid:
In article <fm*********@ulysses.noc.ntua.gr>, jo**@no.spam says...

[ ... ]
>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

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jan 12 '08 #4
Pete Becker wrote:
>
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.
Jan 12 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: William Payne | last post by:
Hello, I am starting to steer away from the practice of using "using namespace std;" in my code. Instead I am qualifying each name in the source when I use them, for example: std::cout << "Hello";...
0
by: David W. Fenton | last post by:
Today I was working on a hideous old app that I created a long time ago that does a lot of showing/hiding/resizing of fields on one of the forms. I had used constants to store reference values for...
4
by: Amadelle | last post by:
Hi all and thanks again in advance, What is the best way of defining global constants in a C# application? (A windows application with no windows forms - basically a set of classes). Would it be...
1
by: Vijai Kalyan | last post by:
Oops, maybe that is the standard. I don't have a copy unfortunately, but here goes. I was experimenting and wrote the following code: #include <limits.h> #include <stdexcept> #include <sstream>...
34
by: newsposter0123 | last post by:
The code block below initialized a r/w variable (usually .bss) to the value of pi. One, of many, problem is any linked compilation unit may change the global variable. Adjusting // rodata const...
6
by: PC | last post by:
Gentlesofts, Forgive me. I'm an abject newbie in your world, using VB 2005 with the dot-Net wonderfulness. So, I'm writing a wonderful class or two to interface with a solemnly ancient...
17
by: Neil Cerutti | last post by:
The Glk API (which I'm implementing in native Python code) defines 120 or so constants that users must use. The constants already have fairly long names, e.g., gestalt_Version, evtype_Timer,...
2
by: Leslie Sanford | last post by:
I want to define a set of floating point constants using templates insteand of macros. I'd like to determine whether these constants are floats or doubles at compile time. In my header file, I have...
54
by: shuisheng | last post by:
Dear All, I am always confused in using constants in multiple files. For global constants, I got some clues from http://msdn.microsoft.com/en-us/library/0d45ty2d(VS.80).aspx So in header...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.