Velocity Reviews > Large number in C

# Large number in C

KC
Guest
Posts: n/a

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

Thanks!

Alexander Bartolich
Guest
Posts: n/a

 04-17-2004
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
The built-in data types of C are not enough for your problem.

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

--

Barry Schwarz
Guest
Posts: n/a

 04-17-2004
On 17 Apr 2004 06:44:31 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (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>>

Martin Ambuhl
Guest
Posts: n/a

 04-17-2004
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. 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.

Rakesh Kumar
Guest
Posts: n/a

 04-20-2004
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 **