Re: Reg: gcc option for printing large number (large double)
On Mon, Sep 09, 2013 at 10:11:21AM +0530, Balamurugan wrote:
> 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 = pow(2, 2000);
>
> 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_
>
> 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?
This is outside the precision of double-precision floating point. Have
a look into multi-precision arithmetic libraries such as libgmp
(libgmp-dev). Or if you also do C++, see Boost.Multiprecision, which
wraps the GMP types nicely so you can use them as regular primitives.
Regards,
Roger
--
.''`. Roger Leigh
: :' : Debian GNU/Linux http://people.debian.org/~rleigh/
`. `' schroot and sbuild http://alioth.debian.org/projects/buildd-tools
`- GPG Public Key F33D 281D 470A B443 6756 147C 07B3 C8BC 4083 E800
Reply to: