Re: Reg: gcc option for printing large number (large double)

2013-10-08 Thread Roger Leigh
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)
 11481306952742545242328332011776819840223177020886952004776427368257662613923703138566594863165062699184459646389874627734471189608630553314259313561666531853912998914531228688779148240044871428926990063486244781615463646388363947317026040466353970904996558162398808944629605623311649536164221970332681344168908984458505602379484807914058900934776500429002716706625830522008132236281291761267883317206598995396418127021779858404042159853183251540889433902091920554957783589672039160081957216630582755380425583726015528348786419432054508915275783882625175435528800822842770817965453762184851149029376L
 
 
 
 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/Linuxhttp://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


-- 
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/20131008214330.gq4...@codelibre.net



Re: Reg: gcc option for printing large number (large double)

2013-09-09 Thread Joel Rees
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.00
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)
 11481306952742545242328332011776819840223177020886952004776427368257662613923703138566594863165062699184459646389874627734471189608630553314259313561666531853912998914531228688779148240044871428926990063486244781615463646388363947317026040466353970904996558162398808944629605623311649536164221970332681344168908984458505602379484807914058900934776500429002716706625830522008132236281291761267883317206598995396418127021779858404042159853183251540889433902091920554957783589672039160081957216630582755380425583726015528348786419432054508915275783882625175435528800822842770817965453762184851149029376L


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: 

Re: Reg: gcc option for printing large number (large double)

2013-09-09 Thread Dom

On 09/09/13 07:21, Joel Rees wrote:
snip code and stuff


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


dom@oz:~$ locate float.h
/usr/lib/gcc/i486-linux-gnu/4.6/include/float.h
/usr/lib/gcc/i486-linux-gnu/4.7/include/float.h
/usr/lib/pymodules/python2.6/numpy/core/include/numpy/halffloat.h
/usr/lib/pymodules/python2.7/numpy/core/include/numpy/halffloat.h
/usr/share/pyshared/numpy/core/include/numpy/halffloat.h

whereis looks on the PATH for a command. float.h isn't a command. It 
does also look for manpages, but...


It does have a manpage:
http://manpages.debian.net/cgi-bin/man.cgi?query=float.h
but perhaps you don't have it installed. I certainly don't.

--
Dom


--
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/522d7707.7090...@rpdom.net



Re: Reg: gcc option for printing large number (large double)

2013-09-09 Thread Chris Bannister
On Mon, Sep 09, 2013 at 08:21:43AM +0100, Dom wrote:
 
 It does have a manpage:
 http://manpages.debian.net/cgi-bin/man.cgi?query=float.h

root@tal:~# apt-cache show manpages-dev

 but perhaps you don't have it installed. I certainly don't.

root@tal:~# apt-cache policy manpages-dev
-- 
If you're not careful, the newspapers will have you hating the people
who are being oppressed, and loving the people who are doing the 
oppressing. --- Malcolm X


-- 
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/20130909120058.GE25870@tal



Re: Reg: gcc option for printing large number (large double)

2013-09-09 Thread Curt
On 2013-09-09, Joel Rees joel.r...@gmail.com wrote:

 It does have a manpage:
 http://manpages.debian.net/cgi-bin/man.cgi?query=float.h

 It's funny, I was sure I had gotten that particular man page before.

 root@tal:~# apt-cache show manpages-dev


You don't need to be root, by the way, to apt-cache your way around.

Just a side note for anyone who might be reading the thread and thinking
otherwise by example.


-- 
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/slrnl2s4l9.3op.cu...@einstein.electron.org



Re: Reg: gcc option for printing large number (large double)

2013-09-09 Thread Joel Rees
On Mon, Sep 9, 2013 at 4:21 PM, Dom to...@rpdom.net wrote:
 On 09/09/13 07:21, Joel Rees wrote:
 snip code and stuff


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


 dom@oz:~$ locate float.h
 /usr/lib/gcc/i486-linux-gnu/4.6/include/float.h
 /usr/lib/gcc/i486-linux-gnu/4.7/include/float.h
 /usr/lib/pymodules/python2.6/numpy/core/include/numpy/halffloat.h
 /usr/lib/pymodules/python2.7/numpy/core/include/numpy/halffloat.h
 /usr/share/pyshared/numpy/core/include/numpy/halffloat.h

 whereis looks on the PATH for a command. float.h isn't a command. It does
 also look for manpages, but...

whereis picks up stuff in /usr/include and /usr/lib, as well.

 It does have a manpage:
 http://manpages.debian.net/cgi-bin/man.cgi?query=float.h
 but perhaps you don't have it installed. I certainly don't.

 --
 Dom


I actually do have manpages-dev installed, so I can get a man page on
fgetc, but it still doesn't give me a man page for stuff in float.h.

It's two am here. I'm going to bed.

--
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/CAAr43iNwX-z8=4g+dp-jxyo+ngfftmithsmnmjyfmfmsrma...@mail.gmail.com



Re: Reg: gcc option for printing large number (large double)

2013-09-09 Thread Joel Rees
On Mon, Sep 9, 2013 at 9:00 PM, Chris Bannister
cbannis...@slingshot.co.nz wrote:
 On Mon, Sep 09, 2013 at 08:21:43AM +0100, Dom wrote:

 It does have a manpage:
 http://manpages.debian.net/cgi-bin/man.cgi?query=float.h

It's funny, I was sure I had gotten that particular man page before.

 root@tal:~# apt-cache show manpages-dev

 but perhaps you don't have it installed. I certainly don't.

 root@tal:~# apt-cache policy manpages-dev

manpages-dev is loaded on my machine. But digging in, I see something
about glibc-doc. Using synaptic because I'm lazy, I see that glibc-doc
has been split, and glibc-doc-reference contains most of the
documentation for glibc. Split because of license issues, it says. And
glibc-doc is in the usual repository, but glibc-doc-reference is not.

So I enable contrib and non-free because it's late and I don't care
which one has glibc-doc-reference and now I can load
glibc-doc-reference.

But still no response when I do man float.h or man -k DBL_DIG or
info DBL_DIG.

man fgetc

works. So does

info fgetc

My memory is that it worked with only manpages-dev installed. But even
with glibc-doc-reference installed, nothing on the macros in limits.h
or float.h. Or locale.h. But

man HUGE_VAL

brings up the man page for the constants in math.h. And I think it did
with just manpages-dev.

I wonder what gives here.

 --
 If you're not careful, the newspapers will have you hating the people
 who are being oppressed, and loving the people who are doing the
 oppressing. --- Malcolm X

--
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/CAAr43iNbLjv0=7g3t6-JNK=4preqlqu4hq2h+atcfs4-3br...@mail.gmail.com



Re: Reg: gcc option for printing large number (large double)

2013-09-09 Thread Joel Rees
DEC_EVAL_METHOD
Can't seem to turn this loose.

On Tue, Sep 10, 2013 at 1:51 AM, Joel Rees joel.r...@gmail.com wrote:
 On Mon, Sep 9, 2013 at 4:21 PM, Dom to...@rpdom.net wrote:
 On 09/09/13 07:21, Joel Rees wrote:
 snip code and stuff


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


 dom@oz:~$ locate float.h
 /usr/lib/gcc/i486-linux-gnu/4.6/include/float.h
 /usr/lib/gcc/i486-linux-gnu/4.7/include/float.h
 /usr/lib/pymodules/python2.6/numpy/core/include/numpy/halffloat.h
 /usr/lib/pymodules/python2.7/numpy/core/include/numpy/halffloat.h
 /usr/share/pyshared/numpy/core/include/numpy/halffloat.h

 whereis looks on the PATH for a command. float.h isn't a command. It does
 also look for manpages, but...

 whereis picks up stuff in /usr/include and /usr/lib, as well.

 It does have a manpage:
 http://manpages.debian.net/cgi-bin/man.cgi?query=float.h
 but perhaps you don't have it installed. I certainly don't.

 --
 Dom


 I actually do have manpages-dev installed, so I can get a man page on
 fgetc, but it still doesn't give me a man page for stuff in float.h.

Now that I see that man works for the functions and macros and not for
the header name (But why wouldn't man-k pick that up?), I do a man
pow and notice there is a powl() function that looks like it might
work but maybe doesn't:

---
#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 );

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

   return EXIT_SUCCESS;
}
---

which prints out the value of 2^2000 okay, but fails to increment it.

Which makes sense, since we have no reason to believe long double ever
gets 2000 bits of significance, but powers of two should be accurately
stored up to the limit of the exponent.

Thus the need for GMP or MPFR  or similar libraries. (Or you can just
do it in python. ;)

http://en.wikipedia.org/wiki/GNU_Multi-Precision_Library

and

http://en.wikipedia.org/wiki/MPFR

I note:

cat /usr/lib/gcc/x86_64-linux-gnu/4.7/include/float.h

a constant, DEC_EVAL_METHOD,

#undef DEC_EVAL_METHOD
#define DEC_EVAL_METHOD2

might force all operations and constants to the range and precision
of the _Decimal128 type, which might be of interest for compiling
environments which do not make long doubles longer than doubles -- a
kind of last chance on those systems. Unless he only needs the
constant, and doesn't need precision/accuracy, the OP is going to want
GMP or MPFR or the like, if python can't do his job.

Since I seem to be convincing myself against my better judgment that I
have time, and since I've always wanted to play with the
multiprecision stuff, I'm going to install libgmp-dev and read the
website

http://gmplib.org/

and risk stealing the OP's opportunity for learning.

-
#include stdio.h
#include stdlib.h

#include float.h
#include math.h

#include gmp.h /* NOT a standard header! */


int main( int argc, char * argv[] )
{
   double temp;
   long double ltemp;
   mpz_t z_result;  /* Doing it with arbitrary precision integers. */
   mpf_t f_result;  /* Doing it with arbitrary precision floats. */
   mpf_t f_2; /* Don't seem to have mpf_ui_pow_ui(). */

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

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

   mpz_init( z_result );
   mpz_ui_pow_ui( z_result, 2, 2000 );
   gmp_printf( \n integer 2^2000 is \n%Zd\n, z_result );
   mpz_add_ui( z_result, z_result, 1 );
   gmp_printf( \n integer 2^2000 + 1 is \n%Zd\n, z_result );
   mpz_clear( z_result );

   mpf_init2( f_result, 2500 ); /* A bit of extra room in the precision. */
   mpf_init_set_ui( f_2, 2 );
   mpf_pow_ui( f_result, f_2, 2000 );
   gmp_printf( \n float 2^2000 is \n%Ff\n, f_result );
   mpf_add_ui( f_result, f_result, 1 );
   gmp_printf( \n float 2^2000 + 1 is \n%Ff\n, f_result );

   mpf_clear( f_result );

   return EXIT_SUCCESS;
}
-

Save it as float_h.c and compile it with

cc -o float_h -lgmp -Wall float_h.c

run it with

./float_h

--
Joel Rees
Look around, the leaves are green,
The grass is high, it's the springtime of my life!
Yeah! FUN!


-- 
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of unsubscribe. Trouble? 

Reg: gcc option for printing large number (large double)

2013-09-08 Thread Balamurugan

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 = 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)
11481306952742545242328332011776819840223177020886952004776427368257662613923703138566594863165062699184459646389874627734471189608630553314259313561666531853912998914531228688779148240044871428926990063486244781615463646388363947317026040466353970904996558162398808944629605623311649536164221970332681344168908984458505602379484807914058900934776500429002716706625830522008132236281291761267883317206598995396418127021779858404042159853183251540889433902091920554957783589672039160081957216630582755380425583726015528348786419432054508915275783882625175435528800822842770817965453762184851149029376L



I know that in gcc, there is an option for getting this done. Can any 
body help with that option?


Thanks and Regards,
Balamurugan R


Re: Reg: gcc option for printing large number (large double)

2013-09-08 Thread Joel Rees
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)
 11481306952742545242328332011776819840223177020886952004776427368257662613923703138566594863165062699184459646389874627734471189608630553314259313561666531853912998914531228688779148240044871428926990063486244781615463646388363947317026040466353970904996558162398808944629605623311649536164221970332681344168908984458505602379484807914058900934776500429002716706625830522008132236281291761267883317206598995396418127021779858404042159853183251540889433902091920554957783589672039160081957216630582755380425583726015528348786419432054508915275783882625175435528800822842770817965453762184851149029376L



 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