473,549 Members | 2,455 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11165
"Ioannis Vranos" <jo**@no.spamwr ote in message
news:fm******** *@ulysses.noc.n tua.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*********@ul ysses.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*********@ul ysses.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
1970
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"; Now to my question. Depending upon the status of my program, I return either EXIT_SUCCESS or EXIT_FAILURE from main(). Thinking that these...
0
3043
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 many of the top/height/left settings. I noticed that some of the constants were defined by using other constant values, and I was impressed that...
4
51158
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 a wise idea to create a clsCommonApp and let all other classes to be derived from that class? and define all constants in that base class? Any...
1
1971
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> namespace exceptions { class out_of_range : public std::exception
34
3353
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 long double const_pi=0.0; lines to // rodata
6
3115
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 database. In times recently past, I would have done this with Borland Delphi. So, that's my perspective and I have my old Delphi code to crib from. ...
17
2053
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, keycode_PageDown. Calls to Glk functions are thus ugly and tedious. scriptref = glk.fileref_create_by_prompt( glk.fileusage_Transcript |...
2
2504
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 this: template<bool DoublePrecision> struct Constants { typedef double SampleType; static const double pi;
54
3558
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 file writing: const double PI = 3.14; Every time using it, include the header file.
0
7467
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7500
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7827
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5385
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5110
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3494
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1961
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1079
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.