473,320 Members | 1,691 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,320 software developers and data experts.

Data type of numers typed into code...

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
7 1504
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: D Witherspoon | last post by:
I am developing a Windows Forms application in VB.NET that will use .NET remoting to access the data tier classes. A very simple way I have come up with is by creating typed (.xsd) datasets. For...
5
by: Kevin C | last post by:
I was curious to know what some developers out in the industry are doing when it comes to exposing Data access logic, specifically persistence. This is assuming that your not using an O/R framework...
3
by: snowweb | last post by:
I'm creating my first web application in PHP/MySQL, however, when I go to view the database contents, the data has been stored in the wrong columns. I've gone through my insertion code with a fine...
1
by: Optimus | last post by:
Hi everyone, I currently develop an application in vs.net 2005 with vb.net. I was trying to use typed dataset and I've got in trouble for converting untyped dataset into Typed DataSet. I don't...
10
by: John Salerno | last post by:
I wrote some pretty basic socket programming again, but I'm still confused about what's happening with the buffer_size variable. Here are the server and client programs: -------------- from...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.