By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,214 Members | 2,099 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,214 IT Pros & Developers. It's quick & easy.

Data type of numers typed into code...

P: n/a
I am writing a fraction class and I was testing my addition operator to
find out how big the numerator and denominator can be before an
overflow occurs. I was doing it like this:

fraction frac1(10001, 10000); //Creates 10001/10000
cout << frac1 << " * 2 = " << frac1 + frac1 << endl;

I then proceeded to add zero's to the first line and kept executing the
program and looking for an obviously wrong result as an indication of
overflow. A curious thing happened. This test:

fraction frac1(1000000001, 1000000000);
cout << frac1 << " * 2 = " << frac1 + frac1 << endl;

compiles fine and outputs a correct result. This test:

fraction frac1(10000000001, 10000000000);
cout << frac1 << " * 2 = " << frac1 + frac1 << endl;

which has 1 extra 0 added to the both the numerator and denominator,
doesn't compile. It gives me error C2668: 'fraction::fraction' :
ambiguous call to overloaded function. Now my fraction constructors
can handle int and long int which can be either signed or unsigned and
the numerator doesn't have to be the same type as the denominator, you
can mix and match. I'm running VC++ 6.0 and normally when I just type
numbers into the source code (as I've done here) and those numbers have
no decimal, they are interpreted as type int. So when the numbers get
large enough are they interpreted as a different type?? If so how do I
find out what type that is?

Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Starx wrote:
I am writing a fraction class and I was testing my addition operator to
find out how big the numerator and denominator can be before an
overflow occurs. I was doing it like this:

fraction frac1(10001, 10000); //Creates 10001/10000
cout << frac1 << " * 2 = " << frac1 + frac1 << endl;

I then proceeded to add zero's to the first line and kept executing the
program and looking for an obviously wrong result as an indication of
overflow. A curious thing happened. This test:

fraction frac1(1000000001, 1000000000);
cout << frac1 << " * 2 = " << frac1 + frac1 << endl;

compiles fine and outputs a correct result. This test:

fraction frac1(10000000001, 10000000000);
cout << frac1 << " * 2 = " << frac1 + frac1 << endl;

which has 1 extra 0 added to the both the numerator and denominator,
doesn't compile. It gives me error C2668: 'fraction::fraction' :
ambiguous call to overloaded function. Now my fraction constructors
can handle int and long int which can be either signed or unsigned and
the numerator doesn't have to be the same type as the denominator, you
can mix and match. I'm running VC++ 6.0 and normally when I just type
numbers into the source code (as I've done here) and those numbers have
no decimal, they are interpreted as type int. So when the numbers get
large enough are they interpreted as a different type?? If so how do I
find out what type that is?


If an integer literal starts with a 0 (eg, 0123, 0532, it is interpreted
as an octal number. If it starts with 0x (eg. 0xF3) is it interpreted
as hexidecimal. Beyond that, here is what the standard has to say about it.

2.13.1.2:
The type of an integer literal depends on its form, value, and suffix.
If it is decimal and has no suffix, it has the first of these types in
which its value can be represented: int, long int; if the value cannot
be represented as a long int, the behavior is undefined. If it is octal
or hexadecimal and has no suffix, it has the first of these types in
which its value can be represented: int, unsigned int, long int,
unsigned long int. If it is suffixed by u or U, its type is the first of
these types in which its value can be represented: unsigned int,
unsigned long int. If it is suffixed by l or L, its type is the first of
these types in which its value can be represented: long int, unsigned
long int. If it is suffixed by ul, lu, uL, Lu, Ul, lU, UL, or LU, its
type is unsigned long int.
Jul 23 '05 #2

P: n/a
Starx wrote:
I am writing a fraction class and I was testing my addition operator to
find out how big the numerator and denominator can be before an
overflow occurs. I was doing it like this:

fraction frac1(10001, 10000); //Creates 10001/10000
cout << frac1 << " * 2 = " << frac1 + frac1 << endl;

I then proceeded to add zero's to the first line and kept executing the
program and looking for an obviously wrong result as an indication of
overflow. A curious thing happened. This test:

fraction frac1(1000000001, 1000000000);
cout << frac1 << " * 2 = " << frac1 + frac1 << endl;

compiles fine and outputs a correct result. This test:

fraction frac1(10000000001, 10000000000);
cout << frac1 << " * 2 = " << frac1 + frac1 << endl;

which has 1 extra 0 added to the both the numerator and denominator,
doesn't compile. It gives me error C2668: 'fraction::fraction' :
ambiguous call to overloaded function. Now my fraction constructors
can handle int and long int which can be either signed or unsigned and
the numerator doesn't have to be the same type as the denominator, you
can mix and match. I'm running VC++ 6.0 and normally when I just type
numbers into the source code (as I've done here) and those numbers have
no decimal, they are interpreted as type int. So when the numbers get
large enough are they interpreted as a different type?? If so how do I
find out what type that is?


You might find this useful:
http://msdn.microsoft.com/library/de...ype_ranges.asp

Basically you'll be moving from a "long" to a "long long".

2^32-1 = 4294967295 (largest unsigned 32bit value)
2^31-1 = 2147483647 (largest signed 32bit value)
first = 1000000001 (fits into long)
second = 10000000001 (must be "long long" or "float")

Perhaps thats the problem?

You could consider using a templated fraction class, and/or performing bounds checking.

Ben
--
A7N8X FAQ: www.ben.pope.name/a7n8x_faq.html
Questions by email will likely be ignored, please use the newsgroups.
I'm not just a number. To many, I'm known as a String...
Jul 23 '05 #3

P: n/a
Alan Johnson wrote:
Starx wrote:
[...] So when the numbers get
large enough are they interpreted as a different type?? If so how do I
find out what type that is?


2.13.1.2:
The type of an integer literal depends on its form, value, and suffix.
If it is decimal and has no suffix, it has the first of these types in
which its value can be represented: int, long int; [...]


So, there is no way to know, really. And on a system where 'int' and
'long int' have the same size, anything that doesn't fit in an 'int'
causes undefined behaviour of the compiler. It may choose to give it
some implementation-specific type, or it may send a dirty e-mail to your
boss.

V
Jul 23 '05 #4

P: n/a
Ben Pope wrote:
[...]
Basically you'll be moving from a "long" to a "long long".
There is no such type in C++.
[...]


V
Jul 23 '05 #5

P: n/a
Starx wrote:
I am writing a fraction class and I was testing my addition operator to
find out how big the numerator and denominator can be before an
overflow occurs. I was doing it like this:

fraction frac1(10001, 10000); //Creates 10001/10000
cout << frac1 << " * 2 = " << frac1 + frac1 << endl;

I then proceeded to add zero's to the first line and kept executing the
program and looking for an obviously wrong result as an indication of
overflow. A curious thing happened. This test:

fraction frac1(1000000001, 1000000000);
cout << frac1 << " * 2 = " << frac1 + frac1 << endl;

compiles fine and outputs a correct result. This test:

fraction frac1(10000000001, 10000000000);
cout << frac1 << " * 2 = " << frac1 + frac1 << endl;

which has 1 extra 0 added to the both the numerator and denominator,
doesn't compile. It gives me error C2668: 'fraction::fraction' :
ambiguous call to overloaded function. Now my fraction constructors
can handle int and long int which can be either signed or unsigned and
the numerator doesn't have to be the same type as the denominator, you
can mix and match. I'm running VC++ 6.0 and normally when I just type
numbers into the source code (as I've done here) and those numbers have
no decimal, they are interpreted as type int. So when the numbers get
large enough are they interpreted as a different type?? If so how do I
find out what type that is?


You might find this useful:
http://msdn.microsoft.com/library/de...ype_ranges.asp

Basically you'll be moving from a "long" to a "long long".

2^32-1 = 4294967295 (largest unsigned 32bit value)
first = 1000000001 (fits into long)
second = 10000000001 (must be "long long" or "float")

Perhaps thats the problem?

You could consider using a templated fraction class, and/or performing bounds checking.

Ben
--
A7N8X FAQ: www.ben.pope.name/a7n8x_faq.html
Questions by email will likely be ignored, please use the newsgroups.
I'm not just a number. To many, I'm known as a String...
Jul 23 '05 #6

P: n/a
Victor Bazarov wrote:
Ben Pope wrote:
[...]
Basically you'll be moving from a "long" to a "long long".

There is no such type in C++.


Technically that's true, but in practice it's not. And soon it will be
less technically true <g>, when the Standard's Committee approves the
proposal to add long long, which will almost certainly happen in
October. Of course, that change won't be part of the approved Standard
until the new Standard is approved, a few years from now.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #7

P: n/a
Victor Bazarov wrote:
Ben Pope wrote:
[...]
Basically you'll be moving from a "long" to a "long long".

There is no such type in C++.


Perhaps the OP is on the wrong place as he uses VC6 :-p

Ben
--
A7N8X FAQ: www.ben.pope.name/a7n8x_faq.html
Questions by email will likely be ignored, please use the newsgroups.
I'm not just a number. To many, I'm known as a String...
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.