On Mon, Sep 9, 2013 at 2:40 PM, Joel Rees <joel.r...@gmail.com> wrote:
> 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.

You can check these with a short C program. See below.

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

Here's the program:

-------------------------------------
#include <stdio.h>
#include <stdlib.h>

#include <float.h>
#include <math.h>

int main( int argc, char * argv[] )
{
   double temp;
   long double ltemp;

   printf( "max double digits: %d, double exponent %d\n", DBL_DIG,
DBL_MAX_10_EXP );
   printf( "max double value: %Lf\n", (long double) DBL_MAX );

   ltemp = pow( 2, 2000 );
   temp = pow( 2, 2000 );
   printf( "2^2000 as double: %f\n", temp );
   printf( "2^2000 as long double: %Lf\n", ltemp );

   return EXIT_SUCCESS;
}
-------------------------------------

Not sure why neither man -k nor whereis can find float.h, but it compiles okay.

Here's the output on my machine (Intel AMD64 running 64 bit today):

-------------------------------------
jwr@fun:~/work/mathgames$ cc -o float_h -Wall float_h.c
jwr@fun:~/work/mathgames$ ./float_h
max double digits: 15, double exponent 308
max double value:
179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000
2^2000 as double: inf
2^2000 as long double: inf
-------------------------------------

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

As you can see there are just a few too many digits in 2^2000.

Python has unlimited precision math (as do ruby and bc, among others).

Some architectures will provide more bits for long double in C, and
maybe even some for double. But those are the exception.

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

comp.lang.c is the newsgroup.

Searching google on "c big float" pulled up this page in stackoverflow:

http://stackoverflow.com/questions/6145329/c-efficient-big-floats

and reminded me of one of the libraries I've seen, gmp. There is a
link into wikipedia with more information and other libraries.
Unlimited precision is not as straightforward as one would like.

HTH

--
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/caar43imcrjv2gg4yqxumtqehoocqiw5yb6b8x+9e_ew-b_w...@mail.gmail.com

Reply via email to