# Large number in C

Discussion in 'C Programming' started by KC, Apr 17, 2004.

1. ### KCGuest

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

Thanks!

KC, Apr 17, 2004

2. ### Alexander BartolichGuest

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/

--
Für Google, Tux und GPL!

Alexander Bartolich, Apr 17, 2004

3. ### Barry SchwarzGuest

On 17 Apr 2004 06:44:31 -0700, (KC) wrote:

>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>>

Barry Schwarz, Apr 17, 2004
4. ### Martin AmbuhlGuest

KC wrote:
> 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.

Martin Ambuhl, Apr 17, 2004
5. ### Rakesh KumarGuest

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.

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

--

--
Rakesh Kumar
** Remove nospamplz from my email address for my real email **

Rakesh Kumar, Apr 20, 2004