Help: datatypes...

Discussion in 'C++' started by Dala Dahlgren, Nov 4, 2006.

  1. 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...
    Dala Dahlgren, Nov 4, 2006
    #1
    1. Advertising

  2. Dala Dahlgren

    Ian Collins Guest

    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.
    Ian Collins, Nov 4, 2006
    #2
    1. Advertising

  3. Dala Dahlgren

    Salt_Peter Guest

    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
    */
    Salt_Peter, Nov 4, 2006
    #3
  4. Dala Dahlgren

    BobR Guest

    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::eek:stringstream out;
    out.precision( 400 );
    out.setf( std::ios_base::fixed );
    // cout<<"out.precision()="<<out.precision()<<std::endl; // =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.00000000000000000
    */

    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
    BobR, Nov 4, 2006
    #4
  5. Dala Dahlgren

    Salt_Peter Guest

    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::eek:stringstream out;
    > out.precision( 400 );
    > out.setf( std::ios_base::fixed );
    > // cout<<"out.precision()="<<out.precision()<<std::endl; // =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.00000000000000000
    > */
    >
    > 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.
    Salt_Peter, Nov 4, 2006
    #5
  6. Dala Dahlgren

    Steve Pope Guest

    Dala Dahlgren <> wrote:

    >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
    Steve Pope, Nov 5, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    2
    Views:
    535
    Yan-Hong Huang[MSFT]
    Jul 28, 2003
  2. =?Utf-8?B?UGF1cmF2aQ==?=

    Problem while reading excel file with mix datatypes

    =?Utf-8?B?UGF1cmF2aQ==?=, Mar 2, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    571
    Alvin Bruney [MVP]
    Mar 4, 2004
  3. .Net Sports
    Replies:
    1
    Views:
    696
    John Rivers
    Aug 31, 2005
  4. Mark
    Replies:
    2
    Views:
    1,060
  5. Ashish

    complex datatypes in webservices.

    Ashish, Dec 19, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    311
    Joshua Flanagan
    Dec 19, 2005
Loading...

Share This Page