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

Help: datatypes...

I am writing a program where I have a subroutine which adds up a number,
basically something like this;

int myNumber;

void add(int i){
myNumber=myNumber+i;
}

So everytime "add" is called myNumber increases.

My problem is that myNumber is "reset" or is definet as "infinity", even
though the number is not too large for the datatype used.
I have tried with a couple of different datatypes.
First I defined all my numbers as "double" since I don't only add integers.
But when myNumber is about 6000 it is suddenly set to #INF the next time it
increases.
I have also tried to define the numbers as pure integers ("long"), but now
the number is reset to 0 a little after it passes 600,000.

Both the 'double' and the 'long' datatypes should be able to handle larger
numbers than this. I am using Microsoft Developer Studio 97, on a Pentium 4
PC.

Can somebody tell me what my problem is and what I should do to be able to
add up more numbers?

Thanks...
Nov 4 '06 #1
5 1332
Dala Dahlgren wrote:
I am writing a program where I have a subroutine which adds up a number,
basically something like this;

int myNumber;

void add(int i){
myNumber=myNumber+i;
}

So everytime "add" is called myNumber increases.

My problem is that myNumber is "reset" or is definet as "infinity", even
though the number is not too large for the datatype used.
I have tried with a couple of different datatypes.
First I defined all my numbers as "double" since I don't only add integers.
But when myNumber is about 6000 it is suddenly set to #INF the next time it
increases.
I have also tried to define the numbers as pure integers ("long"), but now
the number is reset to 0 a little after it passes 600,000.

Both the 'double' and the 'long' datatypes should be able to handle larger
numbers than this. I am using Microsoft Developer Studio 97, on a Pentium 4
PC.

Can somebody tell me what my problem is and what I should do to be able to
add up more numbers?
Only if you post a small compilable example that demonstrates your
problem. Otherwise all people can do is guess. Building the example
code may show you the cause.

--
Ian Collins.
Nov 4 '06 #2

Dala Dahlgren wrote:
I am writing a program where I have a subroutine which adds up a number,
basically something like this;

int myNumber;

void add(int i){
myNumber=myNumber+i;
}

So everytime "add" is called myNumber increases.

My problem is that myNumber is "reset" or is definet as "infinity", even
though the number is not too large for the datatype used.
I'm not getting the same behaviour. Can you maybe show how you are
calling the function. We can only guess at what value is passed to the
function. Are you sure you aren't invoking a factorial?
Also, is myNumber initialized before use. See below.
I have tried with a couple of different datatypes.
First I defined all my numbers as "double" since I don't only add integers.
But when myNumber is about 6000 it is suddenly set to #INF the next time it
increases.
I have also tried to define the numbers as pure integers ("long"), but now
the number is reset to 0 a little after it passes 600,000.

Both the 'double' and the 'long' datatypes should be able to handle larger
numbers than this. I am using Microsoft Developer Studio 97, on a Pentium 4
PC.

Can somebody tell me what my problem is and what I should do to be able to
add up more numbers?

Thanks...
/*main.cpp*/
#include <iostream>
#include <limits>

int myNumber(0);

void add(int i)
{
myNumber += i;
}

int main()
{
// study numeric limits
std::cout << "max integer = " << std::numeric_limits< int >::max()
<< std::endl;
std::cout << "max long = " << std::numeric_limits< long >::max() <<
std::endl;
std::cout << "max double = " << std::numeric_limits< double >::max()
<< std::endl;

for(int i = 0; i < std::numeric_limits< int >::max(); ++i)
{
add(1); // this will take a while
}

std::cout << "myNumber = " << myNumber << std::endl;
return 0;
}

/*
max integer = 2147483647
max long = 9223372036854775807
max double = 1.79769e+308
myNumber = 2147483647 // <- result concurs with an integer's limits
*/

Nov 4 '06 #3

Salt_Peter wrote in message ...
>
/*main.cpp*/
#include <iostream>
#include <limits>

int myNumber(0);
void add(int i){ myNumber += i;}

int main(){ // study numeric limits
std::cout << "max integer = " << std::numeric_limits< int >::max()
<< std::endl;
std::cout << "max long = " << std::numeric_limits< long >::max() <<
std::endl;
std::cout << "max double = " << std::numeric_limits< double >::max()
<< std::endl;

for(int i = 0; i < std::numeric_limits< int >::max(); ++i){
add(1); // this will take a while
}

std::cout << "myNumber = " << myNumber << std::endl;
return 0;
}

/*
max integer = 2147483647
max long = 9223372036854775807
max double = 1.79769e+308
myNumber = 2147483647 // <- result concurs with an integer's limits
*/
<amusement>

Ever try to 'print' that double max() on windows, in 'fixed' format?
[ this is on win98, MinGW(GCC 3.3.1 ]

// int main(){
std::string DblMax("");
{
std::ostringstream out;
out.precision( 400 );
out.setf( std::ios_base::fixed );
// cout<<"out.precision()="<<out.precision()<<std::en dl; // =190
// note output: out.precision()=190

// out<<std::numeric_limits<double>::max(); // GNU/Linux OK

// - biggest I could get to 'print' -
double tmp( std::numeric_limits<double>::max() / 1.0e+276);
// max() / 1.0e+275==boom

out<<(tmp);
DblMax=out.str();
}
std::cout <<"DblMax=out.str() = "<<DblMax<<std::endl;
// } // main()
/* -- output --
DblMax=out.str() =
179769313486231570000000000000000.0000000000000000 0
*/

On GNU/Linux, it prints the whole freakin' max() number.

</amusement>

Different compiler? Try it. Or, am I just missing something?

Obviously ms-window$ was not involved to get us to the moon! <G>
--
Bob R
POVrookie
Nov 4 '06 #4

BobR wrote:
Salt_Peter wrote in message ...

/*main.cpp*/
#include <iostream>
#include <limits>

int myNumber(0);
void add(int i){ myNumber += i;}

int main(){ // study numeric limits
std::cout << "max integer = " << std::numeric_limits< int >::max()
<< std::endl;
std::cout << "max long = " << std::numeric_limits< long >::max() <<
std::endl;
std::cout << "max double = " << std::numeric_limits< double >::max()
<< std::endl;

for(int i = 0; i < std::numeric_limits< int >::max(); ++i){
add(1); // this will take a while
}

std::cout << "myNumber = " << myNumber << std::endl;
return 0;
}

/*
max integer = 2147483647
max long = 9223372036854775807
max double = 1.79769e+308
myNumber = 2147483647 // <- result concurs with an integer's limits
*/

<amusement>

Ever try to 'print' that double max() on windows, in 'fixed' format?
[ this is on win98, MinGW(GCC 3.3.1 ]

// int main(){
std::string DblMax("");
{
std::ostringstream out;
out.precision( 400 );
out.setf( std::ios_base::fixed );
// cout<<"out.precision()="<<out.precision()<<std::en dl; // =190
// note output: out.precision()=190

// out<<std::numeric_limits<double>::max(); // GNU/Linux OK

// - biggest I could get to 'print' -
double tmp( std::numeric_limits<double>::max() / 1.0e+276);
// max() / 1.0e+275==boom

out<<(tmp);
DblMax=out.str();
}
std::cout <<"DblMax=out.str() = "<<DblMax<<std::endl;
// } // main()
/* -- output --
DblMax=out.str() =
179769313486231570000000000000000.0000000000000000 0
*/

On GNU/Linux, it prints the whole freakin' max() number.

</amusement>

Different compiler? Try it. Or, am I just missing something?

Obviously ms-window$ was not involved to get us to the moon! <G>
--
Bob R
POVrookie
lol
Sorry, no windows here. linux FC5, gcc 4.1.1 x86_64, FX dual-core.
Which explains the output above.
And yes, it prints the whole thing. And i do mean the *whole* thing.
If you don't mind, i'll spare the newsgroup.

Nov 4 '06 #5
Dala Dahlgren <da**@broadpark.nowrote:
>First I defined all my numbers as "double" since I don't only add integers.
But when myNumber is about 6000 it is suddenly set to #INF the next time it
increases.
I have also tried to define the numbers as pure integers ("long"), but now
the number is reset to 0 a little after it passes 600,000.

Both the 'double' and the 'long' datatypes should be able to handle larger
numbers than this.
Have you tried "long long"? I've seen systems where "long"
is no longer than "int", but "long long" is twice as long.
I think this is for historical reasons.

Steve
Nov 5 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
8
by: Adam Greifer | last post by:
Hi! I'm a newbie at DB2 but have 13 years of SQL Server. I need to convert over 100 SQL Server procs to DB2. I haven't had much luck with the IBM Integration Toolkit and want to avoid ER/win...
3
by: Mark | last post by:
What are the best .NET datatypes to handle SQL Server's Float and Real datatypes? I'd like to avoid using the SQL Server specific datatypes like SqlInt32 or similar. Thanks in advance. -Mark
2
by: phillip.s.powell | last post by:
I have to migrate data from one database table to another table in another database where the fields do not match, not even in the same order, and even if they do match, on occasions the datatypes...
2
by: circuit_breaker | last post by:
Hi, Is there's a built-in object inside of mySQL that hold its various datatypes? As an example, I'd like to populate an array with the following values: "BOOL",...
3
by: juliehg | last post by:
Hi, I'm currently using MS Access 2000 to keep all my actors measurements, resumes and personal details on a database. To upload their details onto our website, I have built an event so when I...
5
by: Sam | last post by:
Hi, I have one table like : MyTable {field1, field2, startdate, enddate} I want to have the count of field1 between startdate and enddate, and the count of field2 where field2 = 1 between...
13
by: Jack B | last post by:
I'm using Access 2002 to create a database for a small opera company that my wife is involved in, and I'm more than a bit rusty because I haven't created a new Access database since about 1999. ...
17
by: pyramid | last post by:
Hello I am working on one of my lab for this week, which calculates the approximate value of pi. Listed below is the actual problem, which I am posting here, so that you can see the different...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.