Thread: Calculating : high numbers

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    31

    Question Calculating : high numbers

    Hello, I have written a small application in Borland C++ Builder 6.0 and I would like to ask whether you know about some way of calculating with numbers higher than when calculating with unsigned int. Thank you...

    ( I was actually calculating Pascal's triangle nad I could have got beyond the line of about 34 because of this limit... )

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    It's machine specific.

    C++ implementation states that ints are no less than 16 bits and longs no less then 32 bits. Most 32 bit machines have both int and long defined as 32 bits.

    You can easily test this...

    Code:
        int integer;
        long longer;
    
        cout << "\n" << sizeof(integer) << "\t" << sizeof(longer) << endl;
    Most probably you will have a "4 4" output. Which means they are both 4 bytes long, or 32 Bits (1 byte equals 8 bits), also known as word.

    As such you are left with the floating point types. float, double and long double.

    float is bad for what you want. It is also 4 bytes long and as such offers only 6 significant digits (just like an int).

    double is what people most usually fall in love with. It offers 10 precision digits. so you can have a number like 4,567,653,421.

    long double is not supported on most 32 bit machines. It's sizeof() on my machine is the same as double. 8 bytes (or 64bits, or 2 words).

    So, to say what I should have said in the very beginning and have spared you all this nonsense, go with double.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Use a library, eg
    http://www.swox.com/gmp/
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    ( I was actually calculating Pascal's triangle nad I could have got beyond the line of about 34 because of this limit... )
    If this is the case, then you're only using addition, and it would be relatively easy to store a number in a string and perform addition yourself, digit by digit.
    double ... offers 10 precision digits.
    Actually, a float must be at least 4 bytes (which is about 7 digits), and a double must be at least as large as a float (but it doesn't have to be any larger). Most 32-bit machines use an 8 bytes double (which is about 15 digits). I don't know where you got 10 from.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    >> Most 32-bit machines use an 8 bytes double (which is about 15 digits). I don't know where you got 10 from.
    Maybe a double on his machine is not 8 bytes long. Simple as that, though it might have been bad for Mario to impose his system's specifics as standards.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    To get 10 digits a variable would have to be from 30 to 33 bits for an integral number. I don't know how big a floating-point variable would be; maybe about 6 bytes. Perhaps on his machine a double is 10 digits, but I doubt it.

    Either way, you're right, his computer should not be taken as the standard.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    No, it's not. It's the typical 8 bytes. It was my inferior math skills that failed on me

    I apologize for the mistake.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You can try unsigned long long; in some implementations it's a double int size, which will reach to 20 digits in your typical 32-bit machine.

    But yes, Salem is right. If you want HUGE numbers, Google up "arbitrary precision C integer library".
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    31

    Thank all of you...

    All of you have my thanks

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    Code:
    Posted by Mario.F
    Which means they are both 4 bytes long
     or 32 Bits (1 byte equals 8 bits), also known as word
    No,one word is equal to 2 bytes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Outputting numbers in arrays
    By rachael033 in forum C++ Programming
    Replies: 10
    Last Post: 05-29-2007, 02:56 AM
  2. random numbers
    By ballmonkey in forum C++ Programming
    Replies: 3
    Last Post: 01-18-2005, 02:16 PM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM