linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* printf and long double
@ 2010-10-03 22:56 Nanakos Chrysostomos
  2010-10-04  0:43 ` Gedare Bloom
  2010-10-04  1:33 ` Glynn Clements
  0 siblings, 2 replies; 8+ messages in thread
From: Nanakos Chrysostomos @ 2010-10-03 22:56 UTC (permalink / raw)
  To: linux-c-programming

Hi all,
I have the above C code snippet. I am trying to get a resonable result in a fixed form with printf for
a long double value but with no luck. What am I doing wrong?

Thanks in advance.

long.c
-------------------------------------------------------------
#include <stdio.h>
#include <math.h>

int main(void)
{
	long double c = powl(10.0L,30.0L);

	printf("%llf %lle\n",c,c);


	return 0;
}

------------------------------------------------------------

# gcc -DDOUBLE -msse2 -mfpmath=sse -m64 -m128bit-long-double  long.c -lm
#./a.out

1000000000000000000024696061952.000000 1.000000e+30

The second result is right but how can I get a fixed right result for the first one?
The fixed precision should be a lot bigger than this according to the IEEE-754 standard.
I am using a 64-bit machine with Debian Squeeze x86-64 version.
-----------------------------------------------------------------------------------------
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.4.4-8' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --with-arch-32=i586 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.4.5 20100728 (prerelease) (Debian 4.4.4-8) 
-----------------------------------------------------------------------------------------
# gcc -dM -E -xc /dev/null | grep __LDBL_MAX__
#define __LDBL_MAX__ 1.18973149535723176502e+4932L


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: printf and long double
  2010-10-03 22:56 printf and long double Nanakos Chrysostomos
@ 2010-10-04  0:43 ` Gedare Bloom
  2010-10-04  1:44   ` Glynn Clements
  2010-10-04  1:33 ` Glynn Clements
  1 sibling, 1 reply; 8+ messages in thread
From: Gedare Bloom @ 2010-10-04  0:43 UTC (permalink / raw)
  To: Nanakos Chrysostomos; +Cc: linux-c-programming

On Sun, Oct 3, 2010 at 6:56 PM, Nanakos Chrysostomos
<nanakos@wired-net.gr> wrote:
> Hi all,
> I have the above C code snippet. I am trying to get a resonable result in a fixed form with printf for
> a long double value but with no luck. What am I doing wrong?
>
> Thanks in advance.
>
> long.c
> -------------------------------------------------------------
> #include <stdio.h>
> #include <math.h>
>
> int main(void)
> {
>        long double c = powl(10.0L,30.0L);
>
>        printf("%llf %lle\n",c,c);
>
I think you want %llg instead of %llf.  g is for double-precision, f
is for single.
>
>        return 0;
> }
>
> ------------------------------------------------------------
>
> # gcc -DDOUBLE -msse2 -mfpmath=sse -m64 -m128bit-long-double  long.c -lm
> #./a.out
>
> 1000000000000000000024696061952.000000 1.000000e+30
>
> The second result is right but how can I get a fixed right result for the first one?
> The fixed precision should be a lot bigger than this according to the IEEE-754 standard.
> I am using a 64-bit machine with Debian Squeeze x86-64 version.
> -----------------------------------------------------------------------------------------
> Using built-in specs.
> Target: x86_64-linux-gnu
> Configured with: ../src/configure -v --with-pkgversion='Debian 4.4.4-8' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --with-arch-32=i586 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
> Thread model: posix
> gcc version 4.4.5 20100728 (prerelease) (Debian 4.4.4-8)
> -----------------------------------------------------------------------------------------
> # gcc -dM -E -xc /dev/null | grep __LDBL_MAX__
> #define __LDBL_MAX__ 1.18973149535723176502e+4932L
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: printf and long double
  2010-10-03 22:56 printf and long double Nanakos Chrysostomos
  2010-10-04  0:43 ` Gedare Bloom
@ 2010-10-04  1:33 ` Glynn Clements
  2010-10-04 11:54   ` Chrysostomos Nanakos
  1 sibling, 1 reply; 8+ messages in thread
From: Glynn Clements @ 2010-10-04  1:33 UTC (permalink / raw)
  To: Nanakos Chrysostomos; +Cc: linux-c-programming


Nanakos Chrysostomos wrote:

> I have the above C code snippet. I am trying to get a resonable
> result in a fixed form with printf for a long double value but with
> no luck. What am I doing wrong?

> 	long double c = powl(10.0L,30.0L);
> 
> 	printf("%llf %lle\n",c,c);

> # gcc -DDOUBLE -msse2 -mfpmath=sse -m64 -m128bit-long-double  long.c -lm
> #./a.out
> 
> 1000000000000000000024696061952.000000 1.000000e+30
> 
> The second result is right but how can I get a fixed right result for the first one?
> The fixed precision should be a lot bigger than this according to the IEEE-754 standard.
> I am using a 64-bit machine with Debian Squeeze x86-64 version.

On x86, long double is 80 bits, which is roughly 24 decimal digits.

The -m128bit-long-double flag only changes the alignment, not the
accuracy of the calculations.

The only reason why you're getting different results for %f and %e is
that the default precision of 6 refers to 6 decimal places for %f but
to 6 significant digits for %e.

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: printf and long double
  2010-10-04  0:43 ` Gedare Bloom
@ 2010-10-04  1:44   ` Glynn Clements
  0 siblings, 0 replies; 8+ messages in thread
From: Glynn Clements @ 2010-10-04  1:44 UTC (permalink / raw)
  To: Gedare Bloom; +Cc: Nanakos Chrysostomos, linux-c-programming


Gedare Bloom wrote:

> >        printf("%llf %lle\n",c,c);
> 
> I think you want %llg instead of %llf.  g is for double-precision, f
> is for single.

That is incorrect. The difference between %f, %e and %g is the
notation used.

%f uses normal decimal notation, %e uses scientific (exponential)
notation, %g uses either notation depending upon what is appropriate
for the magnitude of the value, specifically: %e for values whose
exponent is greater than the precision or less than -4, %f otherwise.

It is impossible to pass a single-precision "float" to printf() etc
due to C's implicit conversion rules: passing a "float" as an argument
whose type isn't specified by a function prototype results in the
value being converted to "double".

Also, using the "ll" modifier for floating-point conversions ("%llf"
etc) is a GNU extension. The C99 standard specifies the "L" modifier
for "long double" conversions ("%Lf" etc); the "ll" modifier only
applies to integer conversions.

-- 
Glynn Clements <glynn@gclements.plus.com>
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: printf and long double
  2010-10-04  1:33 ` Glynn Clements
@ 2010-10-04 11:54   ` Chrysostomos Nanakos
  2010-10-04 12:03     ` Bogdan Cristea
  0 siblings, 1 reply; 8+ messages in thread
From: Chrysostomos Nanakos @ 2010-10-04 11:54 UTC (permalink / raw)
  To: Glynn Clements, linux-c-programming

  On 4/10/2010 4:33 πμ, Glynn Clements wrote:
> Nanakos Chrysostomos wrote:
>
>> I have the above C code snippet. I am trying to get a resonable
>> result in a fixed form with printf for a long double value but with
>> no luck. What am I doing wrong?
>> 	long double c = powl(10.0L,30.0L);
>>
>> 	printf("%llf %lle\n",c,c);
> On x86, long double is 80 bits, which is roughly 24 decimal digits.


I wish I could get 24 decimal digits precision with the current long 
double (80-bit). I can't get more than 15 digits even in a simple division.
I think that the extended-precision can't give more than 19 decimal 
digits of precision (log(10,2^64)=19.2). The quadruple precision can 
give at most 34 decimal digits (log(10,2^113)=34).

Please consider the example below:
--------------------------------------------------
long double a = 1.0L/7.0L;
printf("%Lf\n",a);
--------------------------------------------------

Can't get more than 15 digits precision....-:(
Is this a compiler problem or libc's problem? In x86_64 machines the 
long double values supposed to give at least 106-bit precision even when 
implemented in software. Is gcc compliant with the quadruple notation?




> The -m128bit-long-double flag only changes the alignment, not the
> accuracy of the calculations.
>
> The only reason why you're getting different results for %f and %e is
> that the default precision of 6 refers to 6 decimal places for %f but
> to 6 significant digits for %e.
>

--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: printf and long double
  2010-10-04 11:54   ` Chrysostomos Nanakos
@ 2010-10-04 12:03     ` Bogdan Cristea
  2010-10-04 12:12       ` Chrysostomos Nanakos
  2010-10-06 20:18       ` printf and long double [SOLVED] Nanakos Chrysostomos
  0 siblings, 2 replies; 8+ messages in thread
From: Bogdan Cristea @ 2010-10-04 12:03 UTC (permalink / raw)
  To: Chrysostomos Nanakos; +Cc: linux-c-programming

On Monday 04 October 2010 14:54:46 you wrote:
> I wish I could get 24 decimal digits precision with the current long 
> double (80-bit). I can't get more than 15 digits even in a simple division.
> I think that the extended-precision can't give more than 19 decimal 
> digits of precision (log(10,2^64)=19.2). The quadruple precision can 
> give at most 34 decimal digits (log(10,2^113)=34).
> 
> Please consider the example below:
> --------------------------------------------------
> long double a = 1.0L/7.0L;
> printf("%Lf\n",a);
> --------------------------------------------------
> 
> Can't get more than 15 digits precision....-:(
> Is this a compiler problem or libc's problem? In x86_64 machines the 
> long double values supposed to give at least 106-bit precision even when 
> implemented in software. Is gcc compliant with the quadruple notation?

Have you tried to specify manually the number of digits after the decimal 
point ?

	long double a = 1.0L/7.0L;
	printf("%'.100Lf\n",a);

On a 64 bits machine the output is:

0.1428571428571428571409210675491330277964152628555893898010253906250000000000000000000000000000000000

regards
Bogdan

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: printf and long double
  2010-10-04 12:03     ` Bogdan Cristea
@ 2010-10-04 12:12       ` Chrysostomos Nanakos
  2010-10-06 20:18       ` printf and long double [SOLVED] Nanakos Chrysostomos
  1 sibling, 0 replies; 8+ messages in thread
From: Chrysostomos Nanakos @ 2010-10-04 12:12 UTC (permalink / raw)
  To: Bogdan Cristea; +Cc: linux-c-programming

  On 4/10/2010 3:03 μμ, Bogdan Cristea wrote:
> On Monday 04 October 2010 14:54:46 you wrote:
>> I wish I could get 24 decimal digits precision with the current long
>> double (80-bit). I can't get more than 15 digits even in a simple division.
>> I think that the extended-precision can't give more than 19 decimal
>> digits of precision (log(10,2^64)=19.2). The quadruple precision can
>> give at most 34 decimal digits (log(10,2^113)=34).
>>
>> Please consider the example below:
>> --------------------------------------------------
>> long double a = 1.0L/7.0L;
>> printf("%Lf\n",a);
>> --------------------------------------------------
>>
>> Can't get more than 15 digits precision....-:(
>> Is this a compiler problem or libc's problem? In x86_64 machines the
>> long double values supposed to give at least 106-bit precision even when
>> implemented in software. Is gcc compliant with the quadruple notation?
> Have you tried to specify manually the number of digits after the decimal
> point ?
>
> 	long double a = 1.0L/7.0L;
> 	printf("%'.100Lf\n",a);
>
> On a 64 bits machine the output is:
>
> 0.1428571428571428571409210675491330277964152628555893898010253906250000000000000000000000000000000000
>
> regards
> Bogdan
Of course I did. But the result you get is the wrong one after the first 
20 decimal point digits. The right result is given below with a high 
precision arithmetic library or with Mathematica or Matlab. And the 
three results are the same, but not the given one above which is the 
same for my machine.

0.14285714285714285714285714285714285714285714285714285714285714285714\
28571428571428571428571428571429
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: printf and long double [SOLVED]
  2010-10-04 12:03     ` Bogdan Cristea
  2010-10-04 12:12       ` Chrysostomos Nanakos
@ 2010-10-06 20:18       ` Nanakos Chrysostomos
  1 sibling, 0 replies; 8+ messages in thread
From: Nanakos Chrysostomos @ 2010-10-06 20:18 UTC (permalink / raw)
  To: Bogdan Cristea; +Cc: linux-c-programming

Problem Solved:

#include <stdio.h>
#include <math.h>

int main(void)
{
		_Decimal128 a = log(8.25123456);

		printf("%.62Lf\n",(long double)a);



		return 0;
}

Thanks for your replies.

On Mon, Oct 04, 2010 at 03:03:27PM +0300, Bogdan Cristea wrote:
> On Monday 04 October 2010 14:54:46 you wrote:
> > I wish I could get 24 decimal digits precision with the current long 
> > double (80-bit). I can't get more than 15 digits even in a simple division.
> > I think that the extended-precision can't give more than 19 decimal 
> > digits of precision (log(10,2^64)=19.2). The quadruple precision can 
> > give at most 34 decimal digits (log(10,2^113)=34).
> > 
> > Please consider the example below:
> > --------------------------------------------------
> > long double a = 1.0L/7.0L;
> > printf("%Lf\n",a);
> > --------------------------------------------------
> > 
> > Can't get more than 15 digits precision....-:(
> > Is this a compiler problem or libc's problem? In x86_64 machines the 
> > long double values supposed to give at least 106-bit precision even when 
> > implemented in software. Is gcc compliant with the quadruple notation?
> 
> Have you tried to specify manually the number of digits after the decimal 
> point ?
> 
> 	long double a = 1.0L/7.0L;
> 	printf("%'.100Lf\n",a);
> 
> On a 64 bits machine the output is:
> 
> 0.1428571428571428571409210675491330277964152628555893898010253906250000000000000000000000000000000000
> 
> regards
> Bogdan

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2010-10-06 20:18 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-10-03 22:56 printf and long double Nanakos Chrysostomos
2010-10-04  0:43 ` Gedare Bloom
2010-10-04  1:44   ` Glynn Clements
2010-10-04  1:33 ` Glynn Clements
2010-10-04 11:54   ` Chrysostomos Nanakos
2010-10-04 12:03     ` Bogdan Cristea
2010-10-04 12:12       ` Chrysostomos Nanakos
2010-10-06 20:18       ` printf and long double [SOLVED] Nanakos Chrysostomos

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).