This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: GCC's Decimal Floating Point extension problem


On 2012-09-11 11:34:58 -0700, H.J. Lu wrote:
> On Tue, Sep 11, 2012 at 11:31 AM, Mohamed Abou Samra
> <my_abousamra@yahoo.com> wrote:
> >  Hi All,
> >
> > I'm trying to write a small program to check the decimal floating
> > point gcc extension but I encountered some problems
> >
> > The program just converts a _Decimal64 number to double to print
> > it and I used the function (double __bid_truncdddf (_Decimal64 a)
> > as the gnu online docs show)

If you cast the decimal64 value to double (whatever the method),
the printed value may be incorrect (see below).

> > #include <stdio.h>
> >
> > int main ()
> > {
> >        _Decimal64 d = 12.5DD;
> >         printf ("%lf\n",__bid_truncdddf(d) );
> >
> > return 0;
> > }
> >
> > $ gcc test.c -Wall -g
> > test.c: In function âmainâ:
> > test.c:23: warning: implicit declaration of function â__bid_truncdddfâ
> > test.c:23: warning: format â%lfâ expects type âdoubleâ, but argument 2 has type âintâ
> >
> > $ ./a.out
> > 0.000000
> >
> > I don't know why the result is zero and why the second warning
> > appears although I wrote the function properly!
> 
> ,__bid_truncdddf is a libgcc internal function.  Don't ever use it
> in user programs.  Just cast DFP to double.

A double has 53 bits in general (always with GCC, AFAIK), so that
it doesn't have enough precision to guarantee that the output in
decimal (with the exact precision of _Decimal64) will be correct.
There has recently been a related discussion in the MPFR list.
See:

  https://sympa.inria.fr/sympa/arc/mpfr/2012-08/msg00027.html
  https://sympa.inria.fr/sympa/arc/mpfr/2012-08/msg00028.html
  https://sympa.inria.fr/sympa/arc/mpfr/2012-09/msg00011.html

So, if long double has at least 55 bits, a solution would be to
cast the _Decimal64 to long double.

-- 
Vincent LefÃvre <vincent@vinc17.net> - Web: <http://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]