Just finished going through C in easy steps and reading C in Jensens and now I am going on to C++ and as I am a slow learner and I need to understand before I can learn or do things, it suddenly hit me while reading and doing Schildt C++ from the ground up I didn't understand what the largest or even the smallest number was capable of or in how it worked. In 32 bit the largest number is 4 billion 294 million 967 thousand 295. I did some programming years ago in a data base environment and the questions of numbers didn't come up but in my last job in accounting I found that many peoples programs could not do the millions quantity that was required when selling fuel in litres to large tugs and we didn't do large ships so I do not know how that was handled but does this 4 billion refer to the numbers in quantity or the amount of numbers I can put on a line or what? Something in my head does not compute so any help would be appreciated. I could not find a thread here nor an answer in any of the books I have got.
Thank you

What are you asking? How to handle large numbers your compiler won't? If that's the case then most people use a large number library.

Maybe i'm unclear of the question.. but a number is just a number - you can use it to represent whatever you want. There's nothing particulary special about your 32 bit max value.. some systems can handle more, other systems can handle less, although, if you get hold of a "big number" library for C++, there's theoretically no maximum number. (C++ unfortunately has no built-in support for numbers of unlimited size, and relies on 3rd party libraries - other programming languages do however)

Incidentally, in C++, you can find the largest and smallest numbers that a type can hold using a feature of the standard library called numeric_limits. If you use different types inbetween the < and > - you'll see that max/min values of types varies wildly.

#include <iostream>
#include <limits>

int main()
{
    int max = std::numeric_limits<int>::max();
    int min = std::numeric_limits<int>::min();

    std::cout << "Maximum value for an int is: " << max << std::endl;
    std::cout << "Mainimum value for an int is: " << min << std::endl;
}

Of course, systems do have hard limits somewhere. on a 32-bit platform, 4-billion(ish) is the maximum number of addressable memory locations, because address values are only 32 bits wide. Is this what you were referring to?


Also, I suggest you stay away from any book written by Herbert Schildt. Every review of his books i've ever seen suggests that he hasn't got a clue about the languages he attempts to teach - his books are reportedly full of sloppy errors and a great deal of misinformation.
(such as two linked here.. http://ma.rtij.nl/acllc-c++.FAQ.html#q6.4 )


For some better books, check the 'sticky' thread for C++ books at the top of the forum, or this link here http://www.rafb.net/efnet_cpp/books/ :)

Comments
Rightly Said About Herbert Schildt

you can also find the maximum values in the header file limits.h -- and most 32-bit compilers today support a 64-bit integers, such as __int64 (Microsoft compilers) or long long other compilers.

>>does this 4 billion refer to the numbers in quantity or the amount of numbers I can put on a line or what
It refers to the largest value that one 32-bit unsigned integer can hold (because it max value in hex is 0xffffffff). a 64-bit unsigned integer would be twice as wide, or 0xffffffffffffffff, or 18,446,744,073,709,551,615, a number large enough to hold even Bill Gates' and Donald Trump's checkbooks.

I see that the number is large enough but my mind still hasn't grasped how this number system works, is there some place I can read about it at my level to get my mind into how this works.
Thanks

In some respects, you don't even need to know the actual upper limits to write safe code. For example,

#include <stdio.h>
#include <limits.h>

int main(void)
{
   int x;
   for ( x = 1000; x < INT_MAX / 2; x *= 10 )
   {
      printf("x = %d\n", x);

   }
   return 0;
}

/* my output
x = 1000
x = 10000
x = 100000
x = 1000000
x = 10000000
x = 100000000
x = 1000000000
*/

INT_MAX is defined by the implementation and made available when you #include <limits.h> .

[edit]Or the numeric_limits stuff as already pointed out. (I tend to operate in C mode.)

I see that the number is large enough but my mind still hasn't grasped how this number system works, is there some place I can read about it at my level to get my mind into how this works.
Thanks

A signed integer can hold any value between INT_MIN and INT_MAX which are defined in limits.h header file. If the number you try to store in the variable is outside that range then the result will be undefined, meaning that its up to the compiler what to do with the excess digits. Microsoft VC++ 2005 Express makes it negative (INT_MIN - 100). Other compilers may do something else.

An unsigned integer (one that does not allow negative numbers) can hold a value of UINT_MAX (see limits.h for definition).

There isn't anything more to it -- if you still can't grasp it maybe you are tring to make the concept harder than it really is. A measuring cup can only hold a cup of water -- if you try to put more water in it the rest will spill out all over the sink. Same itea with integers.

>> ...my mind still hasn't grasped how this number system works, is there some place I can read about it...
http://en.wikipedia.org/wiki/Signed_number_representations
http://en.wikipedia.org/wiki/Two%27s_complement

the c++ standard library header <limits> has a class template numeric_limits<> with specializations for each builtin numeric type. for integral types, the interesting members of std::numeric_limits<> are:

static const bool is_specialized; // will be true for integers
   static T min() throw(); // minimum value
   static T max() throw(); // maximum value
   static const int radix ; // the base of the representation; almost always 2
   static const int digits; // for integers, # value bits
   static const int digits10; // max number of decimal digits
   static const bool is_signed; //
   static const bool is_integer; // will be true for integers

for most uses, these are sufficient. here is an example of using numeric_limits<>.

#include <limits>
#include <iostream>
#include <bitset>
#include <cassert>
using namespace std ;

template< typename NUMERIC_TYPE > void show_info( NUMERIC_TYPE n )
{
   typedef numeric_limits<NUMERIC_TYPE> limits_type ;
   const int radix = limits_type::radix ;
   assert( radix == 2 ) ; // two's complement
   const int digits = limits_type::digits ;
   const int nbits = limits_type::is_signed ? digits+1 : digits ;
   cout << "radix: " << radix << "  digits: " << digits
        << "  nbits: " << nbits << '\n' ;
   NUMERIC_TYPE min_v = limits_type::min() ;
   NUMERIC_TYPE max_v = limits_type::max() ;
   cout << hex << showbase << "min value: " << min_v
        << "  max value: " << max_v << " hexadecimal\n" ;
   cout << oct << showbase << "min value: " << min_v
        << "  max value: " << max_v << " octal\n" ;
   cout << dec << showbase << "min value: " << min_v
        << "  max value: " << max_v << " decimal\n" ;
   cout << "min value: " << bitset<nbits>(min_v)
        << "  max value: " << bitset<nbits>(max_v) << " binary\n" ;
   cout << "decimal " << n << " is represented as binary "
        << bitset<nbits>(n) << '\n' ;
}

int main()
{
   int i = -123456789 ;
   unsigned int ui = -123456789U ;
   cout << "\n*** int ***\n" ;
   show_info(i) ;
   cout << "\n*** unsigned int ***\n" ;
   show_info(ui) ;
}
/**
>g++ -Wall -std=c++98 num_rep.cpp && ./a.out
*** int ***
radix: 2  digits: 31  nbits: 32
min value: 0x80000000  max value: 0x7fffffff hexadecimal
min value: 020000000000  max value: 017777777777 octal
min value: -2147483648  max value: 2147483647 decimal
min value: 10000000000000000000000000000000
max value: 01111111111111111111111111111111 binary
decimal -123456789 is represented as binary 11111000101001000011001011101011

*** unsigned int ***
radix: 2  digits: 32  nbits: 32
min value: 0  max value: 0xffffffff hexadecimal
min value: 0  max value: 037777777777 octal
min value: 0  max value: 4294967295 decimal
min value: 00000000000000000000000000000000
max value: 11111111111111111111111111111111 binary
decimal 4171510507 is represented as binary 11111000101001000011001011101011
*/

the values shown here are typical for 32 bit architectures.
the actual byte ordering in memory is governed by endianness. http://en.wikipedia.org/wiki/Endianness

Comments
Helpful

I see that the number is large enough but my mind still hasn't grasped how this number system works, is there some place I can read about it at my level to get my mind into how this works.
Thanks

That depends, this isn't really a programming question, but more of a discrete/pure mathematics one - How comfortable are you with numbers in bases other than 10? - particularly binary (Base 2), which is the only numbering system that the computer understands (All other numbering systems are representations of base 2)

If you've never touched binary arithmetic before, then spend some time going back to the beginning - how different number bases work, powers of two, counting in other bases, etc. There's a fair bit of background information to consider.

have you considered a residual number system? i think most libraries written for large numbers use this system. write if you want me to go into detail but related subjects would be modular arithmatic, chinese remainer theorem, congruencies, continued fractions. might be totally off the subject but maybe can help.

It is my mind that needs fixing - always looking for something complicated when it is really basic and easy - did a small c++ program and watched what happened when using int, short int, float and found that the numbers were the numbers - so in the past when people programmed with 8 bit they would have problems putting in large numbers - now with double on 64bit one can use 308 decimal points which I wouldn't use unless I took a space ship and went at 1,000,000 miles per second and wanted to find how far I had gone in a year.
Thanks for the different ideas - sometimes it takes me awhile to think.

This article has been dead for over six months. Start a new discussion instead.