473,834 Members | 1,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(100000000 1, 1000000000);
cout << frac1 << " * 2 = " << frac1 + frac1 << endl;

compiles fine and outputs a correct result. This test:

fraction frac1(100000000 01, 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::frac tion' :
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 1529
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(100000000 1, 1000000000);
cout << frac1 << " * 2 = " << frac1 + frac1 << endl;

compiles fine and outputs a correct result. This test:

fraction frac1(100000000 01, 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::frac tion' :
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(100000000 1, 1000000000);
cout << frac1 << " * 2 = " << frac1 + frac1 << endl;

compiles fine and outputs a correct result. This test:

fraction frac1(100000000 01, 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::frac tion' :
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(100000000 1, 1000000000);
cout << frac1 << " * 2 = " << frac1 + frac1 << endl;

compiles fine and outputs a correct result. This test:

fraction frac1(100000000 01, 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::frac tion' :
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
3043
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 example dsParts.xsd and including that in the data tier. I then will create a class that looks like this Public Class CPart Inherits dsParts
5
2139
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 or something that completely abstracts you from the persistence details. Are you: 1. Having simple data type interfaces and the data layer know nothing about the domain models. For example: public int SaveCustomer( string fname, string...
3
3280
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 toothcomb and can see no errors which might cause this. I will post the code below, and you will see that I have some diagnostics built in, which show that the information (at least at that point) appears to be about to be stored in the correct...
1
9365
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 know why I cannot perform this casting operation properly. First off, I've got my Typed DataSet named "AuthInfo" declared and I then created an instance of that class. What I'd like to do next is to perform querying through the method...
10
34539
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 socket import * host = '' port = 51567 address = (host, port) buffer_size = 1024
0
9644
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10793
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10547
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10219
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5626
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5793
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4427
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
2
3977
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3081
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.