top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

GCC option for printing large number (large double)

+1 vote
415 views

I have an issue in printing a large number in a c program. Please find below the code snippet :

int main()
{
 double temp = 0.0;
 temp = pow(2, 2000);
 printf("The value of temp is %lfn", temp);
 return 0;
}

I compiled and ran as below:

$ gcc test.c -o test
$ ./test
The value of temp is inf

But for the same expression, I am able to get the value from python,

$ python
>>> pow(2,2000)
1148130..........................49029376L
>>>

Can any body help me with that option?

posted Sep 13, 2013 by Bob Wise

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

There is no GCC option that will help you here. Python and C are two different languages and they represent numbers in different ways. To do this kind of thing in C you will need to use some sort of bignum library, such as libmpfr.

answer Sep 13, 2013 by Kumar Mitrasen
+1 vote

I would suggest you have a look at the Gnu MP library, which deals with big integers, floats and gives you lots of things that you can normally find in languages like Python, Go or Java.

An example would be:

 mpz_t a;
 mpz_init(a);
 mpz_set_str(a, "10000000000000000000000000000", 10);
 gmp_printf("%Zd", a);
 mpz_clear(a);

which will print this number back to you, and %Zx will print the Hex version of it.

answer Sep 13, 2013 by Garima Jain
Similar Questions
+1 vote

I am trying to compile a simple c/c++ code using g++. The binary carried more information(symbols). Then I used the option -O3 to get a more optimized code but still there seems to be some information(symbols) not mandatory for the binary execution. I need to use 'strip' command to remove those information.

I felt there must be some gcc/g++ option to get this stripping as part of compilation & linking itself. Can anyone help me with this?

0 votes

is it possible to add a private extension to the core language (C/C++) by writing a gcc plugin?

The extension in mind is something like this

[variable_definitions;]

Later I want this be possible also inside statement headers, for example

for ([double d = 1.0; bool f = false;] size_t i = 0; i < vec.size(); ++i)
 ...

The scope of the so-defined variables shall be the same scope they are in, ie. in the for-loop case just the scope of the for-loop itself, much like the case with i.

0 votes

Can I use gcc as well as java compiler on a single desktop?

+1 vote

I don't really understand this option. I enabled it but it warns for essentially all of my classes which have any virtual methods. For
example if I have Foo.h:

 class Foo {
 virtual bool bar();
 ...
 };

then Foo.cpp which implements Foo. And then in another header somewhere else I inherit from Foo and implement bar() differently (as a virtual function). However when GCC compiles Foo.cpp it can't see the other header, so I guess it assumes no one inherits from Foo and suggests it should be marked final:

Declaring type 'class Foo' final would enable devirtualization of N calls

I mean sure, it would, but that would defeat the entire point (as I understand it).

I don't really understand how/where this option is meant to be used.
Ditto for -Wsuggest-final-methods.

...