printf and long double
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
Quoted 11 lines · Nanakos Chrysostomos
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>
Quoted 6 lines
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.
Quoted 21 lines
return 0;
}------------------------------------------------------------
# gcc -DDOUBLE -msse2 -mfpmath=sse -m64 -m128bit-long-double long.c -lm #./a.out1000000000000000000024696061952.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)
-----------------------------------------------------------------------------------------
Quoted 8 lines
# 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 [email protected]
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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 8 lines · Nanakos Chrysostomos
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);
Quoted 8 lines
# gcc -DDOUBLE -msse2 -mfpmath=sse -m64 -m128bit-long-double long.c -lm #./a.out1000000000000000000024696061952.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.
Signature · 1 line
Glynn Clements <[email protected]>
Quoted 4 lines · Gedare Bloom
Quoted 1 line
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.
Signature · 5 lines
Glynn Clements <[email protected]> -- To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 9 lines · Glynn Clements
Quoted 6 lines · Nanakos Chrysostomos
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?
Quoted 7 lines
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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 12 lines · 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);
--------------------------------------------------
Quoted 4 lines
Can't get more than 15 digits precision....-:(
Is this a compiler problem or libc's problem? Inx86_64machines 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
Quoted 23 lines · Bogdan Cristea
Quoted 12 lines · 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);
--------------------------------------------------Quoted 4 lines
Can't get more than 15 digits precision....-:(
Is this a compiler problem or libc's problem? Inx86_64machines 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);
Quoted 6 lines
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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: printf and long double [SOLVED]
In reply to Bogdan CristeaProblem 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.
Quoted 30 lines · Bogdan Cristea
Quoted 12 lines · 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);
--------------------------------------------------Quoted 4 lines
Can't get more than 15 digits precision....-:(
Is this a compiler problem or libc's problem? Inx86_64machines 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