Large number in C

A

Alexander Bartolich

begin followup to KC:
How can I represent a 22-digit integer in C?

Is that 22 digits in decimal radix?

$ echo 'scale=3; l(10)/l(2)*22' | bc -ql
73.062

Requires significantly more than 64 bits so 'long long' does not help.
float is not large enough.

float is typically 32 bits for both mantissa and exponent giving
about 7 decimal digits. double extends that to about 15.
The built-in data types of C are not enough for your problem.

http://www.swox.com/gmp/
 
B

Barry Schwarz

How can I represent a 22-digit integer in C? float is not large enough.

Standard C does not have a type guaranteed to satisfy you on all
conforming implementations.

It is possible that you could find an implementation that runs on
your system with 128 bit long long that would work.

More likely, you can use one of the numerous "bignum"
implementations which use only standard features and are therefore
extremely portable. I have played with CLINT from Richard Heathfield
at www.rjgh.co.uk. It is free and I think it will do what you want.


<<Remove the del for email>>
 
M

Martin Ambuhl

KC said:
How can I represent a 22-digit integer in C? float is not large enough.

Since the range of 22-digit integers in approximately pow(2,69.8) to
pow(2,73.1), find an implementation that has a type with 70+ bits to
represent *some* 22-digit integers, or 74+ bits to represent *all* the
22-digit integers.

You could join together two or more shorter integer types in a struct,
or, if you don't want to do that, find a free open-source bignum library
on the web. There are some googlehints in that last clause.
 
R

Rakesh Kumar

You can -

Try a linked list implementation, with possibly a long integer as a
member, if you want to learn. It would be fun.
- Or -
Use any of the suggested libraries in the prev. replies if you want the
job done right away.

All the best.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top