On Mon, Sep 9, 2013 at 1:41 PM, Balamurugan <emailstorb...@gmail.com> wrote:
> Hi,
>
> I have an issue in printing a large number in a c program. Please find below
> the code snippet :
>
> #include <math.h>
>
> int main()
> {
>         double temp = 0.0;

temp is a double. In your ordinary environment, doubles are stored in
64 bits. But that's 64 bits total, and you have to have some room for
the exponent. In some environments, you have 128 bits for doubles, but
that's not the usual case.

Checking the foat.h header should give you some idea how large a
number you can store in a double. Except I don't see any sign of
float.h in /usr/include. Hmm.

Checking my copy of the old C89 standard, doubles are only guaranteed
to have more than 10 digits accuracy with a maximum storeable value of
at least 10 to the 37th power.

>         temp = pow(2, 2000);

2 to the 2000 power is, at any rate, at least 2000 bits long. That's
way more than 128 bits even.

Counting the digits of output from Python, that's way more than 37
decimal digits.

>         printf("The value of temp is %lf\n", temp);

>         return 0;
> }
>
> I compiled and ran as below:
>
> [balamurugan@balamurugan C_Programs]$ gcc test.c -o test
> [balamurugan@balamurugan C_Programs]$ ./test
> The value of temp is inf


inf, borrowed to invoke the idea of infinity, indicates that the
number is too large to store.

> But for the same expression, I am able to get the value from python,
>
> [balamurugan@balamurugan C_Programs]$ python
> Python 2.6.6 (r266:84292, Feb 22 2013, 00:00:18)
> [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> pow(2,2000)
> 114813069527425452423283320117768198402231770208869520047764273682576626139237031385665948631650626991844596463898746277344711896086305533142593135616665318539129989145312280000688779148240044871428926990063486244781615463646388363947317026040466353970904996558162398808944629605623311649536164221970332681344168908984458505602379484807914058900934776500429002716706625830522008132236281291761267883317206598995396418127021779858404042159853183251540889433902091920554957783589672039160081957216630582755380425583726015528348786419432054508915275783882625175435528800822842770817965453762184851149029376L
>>>>
>
>
> I know that in gcc, there is an option for getting this done. Can any body
> help with that option?

Not so much an option as a library, that is not part of the standard.
Several libraries, in fact. I don't remember their names right
off-hand, don't seem to have them loaded in my system, either. Looking
up BigInt led me to them in the past.

> Thanks and Regards,
> Balamurugan R

There is a C newsgroup that would likely be more helpful.

--
Joel Rees


--
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Archive: 
http://lists.debian.org/caar43in8lha-8ihc642pwybxpqdyfw39dchapbua+hqvsbv...@mail.gmail.com

Reply via email to