Data type of numers typed into code...

S

Starx

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?
 
B

Ben Pope

Starx said:
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/d...-us/vclang/html/_langref_data_type_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
 
B

Ben Pope

Starx said:
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/d...-us/vclang/html/_langref_data_type_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
 
A

Alan Johnson

Starx said:
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.
 
P

Pete Becker

Victor said:
Ben said:
[...]
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.
 

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

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top