help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Precision error in double


From: Robert Heller
Subject: Re: Precision error in double
Date: Wed, 14 Aug 2013 17:23:45 -0500

At Wed, 14 Aug 2013 16:56:03 -0500 Robert Miles <milesrf@Usenet-News.net> wrote:

> 
> On 8/7/2013 6:38 AM, coolhuds@gmail.com wrote:
> > Hi,
> >
> > A large double value gets changed when printed with %Lf
> > Values upto the following combination gives proper results
> > 9 digits before decimal / 6 digits after decimal
> > e.g. of a value with 9 digits before decimal printed with %Lf
> > Input : 3435537287.32
> > Output : 3435537287.320000
> >
> > Once I increase the digits before decimal to 10, the values printed with 
> > %Lf adds garbage value.
> > e.g. of a value with 10 digits before decimal printed with %Lf
> > Input : 34355372871.3487
> > Output : 34355372871.348701
> > As you can see from the above output the input value is changed.
> >
> > Is there any compile time option for g++ that can be used so that the value 
> > is'nt changed?
> >
> > ::Code Snippet::
> > double d2 =  34355372871.3487;
> > double d4 =  3435537287.3487;
> >
> > printf("d2 = %lf\n", d2);
> > printf("d4 = %lf\n", d4);
> >
> >
> > Thanks,
> > Hudson
> 
> For this, you'd need an option to do all calculations in decimal
> instead of the usual binary, in order to avoid the roundoff
> errors in converting from decimal to binary and back.  Perhaps
> 30 years ago, some computers supported an option to do this.
> I haven't seen any sign that a significant number of more recent
> computers do.

The 'decimal' datatype (eg numbers represented in hardware as BCD) is pretty
much a Cobol thing. And yes, that datatype pretty much died with old-school
mainframes (I have no clue if System 390s bother to implement it anymore). I'm
guessing Cobol compilers for 'modern' processors (eg x86 processors) probably
implement the 'decimal' datatype with software emulation, since the datatype is
rarely used for intense computation. Such a datatype is not natively available
in C or C++, although it should be possible to implement an indefinite
precision number type (eg bignums with or without a fixed decimal point),
using long or long long ints as the internal representation.

> 
>                             

-- 
Robert Heller             -- 978-544-6933 / heller@deepsoft.com
Deepwoods Software        -- http://www.deepsoft.com/
()  ascii ribbon campaign -- against html e-mail
/\  www.asciiribbon.org   -- against proprietary attachments


                        


reply via email to

[Prev in Thread] Current Thread [Next in Thread]