just curious : printing big numbers - how?

L

lallous

Without getting too mathematical : how do you print/handle big numbers?

for ( int i = 1 ; i < 10 ; i ++)
cout << (123456789 << i) << endl;

246913578
493827156
987654312
1975308624
-344350048
-688700096
-1377400192
1540166912
-1214633472

Hello

You can:
1. use 3rd party big numbers libraries (such as MIRACL)
2. use a bigger data type (like int64)
3. write your own functions that manipulate and display these big numbers.

Regards,
Elias
 
D

drj0nson

Without getting too mathematical : how do you print/handle big numbers?

for ( int i = 1 ; i < 10 ; i ++)
cout << (123456789 << i) << endl;

246913578
493827156
987654312
1975308624
-344350048
-688700096
-1377400192
1540166912
-1214633472
 
F

Ferdi Smit

Without getting too mathematical : how do you print/handle big numbers?

for ( int i = 1 ; i < 10 ; i ++)
cout << (123456789 << i) << endl;

246913578
493827156
987654312
1975308624
-344350048
-688700096
-1377400192
1540166912
-1214633472

Generally by keeping an array of n digits. It can't be done with
build-ins. Printing it becomes a trivial matter of an O(n) loop over the
array. The funny thing is that this way you can view a (big) number as a
signal or a formal polynomial and the other way around. Therefore
multiplication can be done as polynomial multiplication (convolution),
which can be done by point-wise multiplication in the frequency/fourier
domain. Cute, no? ;)

--
Regards,

Ferdi Smit (M.Sc.)
Email: (e-mail address removed)
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands
 
D

drj0nson

... funny thing is that this way you can view a (big) number as a
signal or a formal polynomial and the other way around. Therefore
multiplication can be done as polynomial multiplication (convolution),
which can be done by point-wise multiplication in the frequency/fourier
domain. Cute, no? ;)
Regards, Ferdi Smit (M.Sc.)
</SNIP>

Thanks guys. But you know I thought I was going to be shown some tiny
piece of code : an example of how to do this using oh I don't know logs
and strings. I appreciate the help Ferdi but I'm afraid I haven't the
foggiest idea of what you are writing about.

Surely this can be done without the need for third party libraries. Can
anyone provide a neat, simple, short code example?
 
G

Geo

</SNIP>

Thanks guys. But you know I thought I was going to be shown some tiny
piece of code : an example of how to do this using oh I don't know logs
and strings. I appreciate the help Ferdi but I'm afraid I haven't the
foggiest idea of what you are writing about.

Surely this can be done without the need for third party libraries. Can
anyone provide a neat, simple, short code example?

The problem is that your question is too vague, if I wanted to just
'print' a big number I'd put it in a string

std::cout << "12345678901234567890";
 
D

drj0nson

The problem is that your question is too vague, if I wanted to just
'print' a big number I'd put it in a string

std::cout << "12345678901234567890";

Yes point taken. Does this help : see question in code comment below.

#include <math.h>
#include <string>
#include <errno.h>
#include <iostream.h>

int main()
{

printf("Max of unsigned long long....= %llu \n",ULONG_LONG_MAX);
printf("Max of long long is .........= %lld \n",LONG_LONG_MAX);
printf("Max of long is ..............= %ld \n", LONG_MAX);

printf("Max of unsigned long long....= %llX hex\n",ULONG_LONG_MAX);
printf("Max of long long is .........= %llX hex\n",LONG_LONG_MAX);
printf("Max of long is ..............= %lX hex\n", LONG_MAX);

printf("size of unsigned long long....= %d bytes
\n",sizeof(ULONG_LONG_MAX));
printf("size of long long is .........= %d bytes
\n",sizeof(LONG_LONG_MAX));
printf("size of long is ..............= %d bytes \n",
sizeof(LONG_MAX));


//Ambiguous overloaded function call
// commented out does not compile says: can only find unsigned short...
// cout << ULONG_LONG_MAX << endl;
// cout << LONG_LONG_MAX << endl;
cout << LONG_MAX << endl;

// ok how do you output this number, in any manner : a string would do
// this produces FFFFFFFFFFFFFFFE which is 1 less than ULONG_LONG_MAX
printf("Max of unsigned long long....= %llX \n",2*ULONG_LONG_MAX);

/*
output

Max of unsigned long long....= 18446744073709551615
Max of long long is .........= 9223372036854775807
Max of long is ..............= 2147483647
Max of unsigned long long....= FFFFFFFFFFFFFFFF hex
Max of long long is .........= 7FFFFFFFFFFFFFFF hex
Max of long is ..............= 7FFFFFFF hex
size of unsigned long long....= 8 bytes
size of long long is .........= 8 bytes
size of long is ..............= 4 bytes
2147483647
Max of unsigned long long....= FFFFFFFFFFFFFFFE

*/
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top