Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Double precision numbers.

Reply
Thread Tools

Double precision numbers.

 
 
Allagappan
Guest
Posts: n/a
 
      05-20-2005
Hello everyone,
Just check this code:

#include<stdio.h>
int main()
{
double a,b;
scanf("%lf",&a);
scanf("%lf",&b);
printf("\nNumber a: %0.16lf",a);
printf("\nNumber b: %0.16lf",b);
printf("\nDifference:%0.16lf\n",(a-b));
return 0;
}

Output
------
[alagu@localhost decimal]$ ./decimal
12.3333
11.1111

Number a: 12.3332999999999995
Number b: 11.1111000000000004
Difference:1.2221999999999991
-----------------------------------
We wanted a clean number like 12.3333 and not
12.3332999.What is the reason for this
and is there anyway to avoid this?We want decimal
points to be exact.How to do it?

Thanks in advance,
Regards,
Allagappan M

 
Reply With Quote
 
 
 
 
bhuwan.chopra@gmail.com
Guest
Posts: n/a
 
      05-20-2005
Actually with very double number there is a precision attached. long
double will have more precision while float will have a less precision.
To avoid the problem you facing you got to device your own data
structure to represent precision you require.

Regards,
Bhuwan Chopra

 
Reply With Quote
 
 
 
 
Chris Croughton
Guest
Posts: n/a
 
      05-20-2005
On 20 May 2005 03:05:41 -0700, Allagappan
<(E-Mail Removed)> wrote:

> Hello everyone,
> Just check this code:
>
> #include<stdio.h>
> int main()
> {
> double a,b;
> scanf("%lf",&a);
> scanf("%lf",&b);
> printf("\nNumber a: %0.16lf",a);
> printf("\nNumber b: %0.16lf",b);
> printf("\nDifference:%0.16lf\n",(a-b));
> return 0;
> }
>
> Output
> ------
> [alagu@localhost decimal]$ ./decimal
> 12.3333
> 11.1111
>
> Number a: 12.3332999999999995
> Number b: 11.1111000000000004
> Difference:1.2221999999999991
> -----------------------------------
> We wanted a clean number like 12.3333 and not
> 12.3332999.What is the reason for this
> and is there anyway to avoid this?We want decimal
> points to be exact.How to do it?


Use COBOL or another language which supports exact fixed point decimal
numbers. Or see the FAQ, it's the first item in the section on floating
point numbers. Since numbers are held (in most machines) as binary,
precision is not infinite. Just as 1/3 is not representable exactly in
decimal (or binary or any other base not a multiple of 3), so 1/10 is
not representable in binary (or ternary or any other base not a multiple
of both 2 and 5).

It's easy to demonstrate with a much simpler program:

#include <stdio.h>

int main(void)
{
double x = 0.1;
printf("%0.16lf\n", x);
return 0;
}

You could always use a lower precision for your output, like %0.12lf,
which would make it look correct even though internally the number is
incorrect in the last bit or so.

Chris C
 
Reply With Quote
 
Michael
Guest
Posts: n/a
 
      05-20-2005
> [alagu@localhost decimal]$ ./decimal
> 12.3333
> 11.1111
>
> Number a: 12.3332999999999995
> Number b: 11.1111000000000004
> Difference:1.2221999999999991
> -----------------------------------
> We wanted a clean number like 12.3333 and not
> 12.3332999.What is the reason for this
> and is there anyway to avoid this?We want decimal
> points to be exact.How to do it?

Hi,
because of the binary system numbers that doesn't
end with '5' (or .0) cannot be stored correctly.
Its not a matter of precision. You can store 12.3333
as 123333 and keep in mind that your number has to
be divided by 10,000.

Michael

 
Reply With Quote
 
Grumble
Guest
Posts: n/a
 
      05-20-2005
Allagappan wrote:

> #include<stdio.h>
> int main()
> {
> double a,b;
> scanf("%lf",&a);
> scanf("%lf",&b);
> printf("\nNumber a: %0.16lf",a);
> printf("\nNumber b: %0.16lf",b);
> printf("\nDifference:%0.16lf\n",(a-b));
> return 0;
> }
>
> Output
> ------
> [alagu@localhost decimal]$ ./decimal
> 12.3333
> 11.1111
>
> Number a: 12.3332999999999995
> Number b: 11.1111000000000004
> Difference:1.2221999999999991
> -----------------------------------
> We wanted a clean number like 12.3333 and not
> 12.3332999.What is the reason for this
> and is there anyway to avoid this?We want decimal
> points to be exact.How to do it?


Do you understand how binary floating-point works?

http://www2.hursley.ibm.com/decimal/decifaq1.html

http://www2.hursley.ibm.com/decimal/
 
Reply With Quote
 
Dik T. Winter
Guest
Posts: n/a
 
      05-20-2005
In article <(E-Mail Removed). com> "Allagappan" <(E-Mail Removed)> writes:
....
> double a,b;
> scanf("%lf",&a);
> scanf("%lf",&b);
> printf("\nNumber a: %0.16lf",a);
> printf("\nNumber b: %0.16lf",b);
> printf("\nDifference:%0.16lf\n",(a-b));

....
> [alagu@localhost decimal]$ ./decimal
> 12.3333
> 11.1111
>
> Number a: 12.3332999999999995
> Number b: 11.1111000000000004
> Difference:1.2221999999999991
> -----------------------------------
> We wanted a clean number like 12.3333 and not
> 12.3332999.What is the reason for this
> and is there anyway to avoid this?We want decimal
> points to be exact.How to do it?


See the FAQ. The bottom line is that 12.3333 can not be represented
exactly in a double. If you want to be exact you need scaled
integers.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      05-20-2005
Chris Croughton wrote:
>
> It's easy to demonstrate with a much simpler program:
>
> #include <stdio.h>
>
> int main(void)
> {
> double x = 0.1;
> printf("%0.16lf\n", x);
> return 0;
> }
>


Easy to demonstrate undefined behaviour (in C89 anyway).
%f (not %lf) is the correct printf specifier for 'double'.

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      05-20-2005
Allagappan wrote:
>

.... snip ...
>
> Number a: 12.3332999999999995
> Number b: 11.1111000000000004
> Difference:1.2221999999999991
> -----------------------------------
> We wanted a clean number like 12.3333 and not
> 12.3332999.What is the reason for this
> and is there anyway to avoid this?We want decimal
> points to be exact.How to do it?


Use integers.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson


 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      05-20-2005
Allagappan wrote:

> Hello everyone,
> Just check this code:
>
> #include<stdio.h>
> int main()
> {
> double a,b;
> scanf("%lf",&a);
> scanf("%lf",&b);
> printf("\nNumber a: %0.16lf",a);
> printf("\nNumber b: %0.16lf",b);
> printf("\nDifference:%0.16lf\n",(a-b));
> return 0;
> }
>
> Output
> ------
> [alagu@localhost decimal]$ ./decimal
> 12.3333
> 11.1111
>
> Number a: 12.3332999999999995
> Number b: 11.1111000000000004
> Difference:1.2221999999999991
> -----------------------------------
> We wanted a clean number like 12.3333 and not
> 12.3332999.What is the reason for this
> and is there anyway to avoid this?We want decimal
> points to be exact.How to do it?
>
> Thanks in advance,
> Regards,
> Allagappan M
>


Try using a "%0.4f" specifier in the printf.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
Reply With Quote
 
Tim Prince
Guest
Posts: n/a
 
      05-20-2005
Allagappan wrote:

> Hello everyone,
> Just check this code:
>
> #include<stdio.h>
> int main()
> {
> double a,b;
> scanf("%lf",&a);
> scanf("%lf",&b);
> printf("\nNumber a: %0.16lf",a);
> printf("\nNumber b: %0.16lf",b);
> printf("\nDifference:%0.16lf\n",(a-b));
> return 0;
> }
>
> Output
> ------
> [alagu@localhost decimal]$ ./decimal
> 12.3333
> 11.1111
>
> Number a: 12.3332999999999995
> Number b: 11.1111000000000004
> Difference:1.2221999999999991
> -----------------------------------
> We wanted a clean number like 12.3333 and not
> 12.3332999.What is the reason for this
> and is there anyway to avoid this?We want decimal
> points to be exact.How to do it?
>

%.17g format is sufficient to express the full precision of IEEE 64-bit
double. Anything more is nearly guaranteed to produce "unclean" output.
Frequently, you may have to go to %.16g to force rounding. In general,
those decimal digits limits may be calculated from information present in
<float.h>.

--
Tim Prince
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java double precision info@censnet.it Java 5 06-10-2009 11:10 AM
double precision vs. integers Andersen Java 11 12-20-2005 11:02 AM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM
Bug in double precision computing? Daniel Geisenhoff ASP .Net 1 07-29-2004 01:58 PM
high precision Math with 'double' (underflow problem) Wolfgang Java 4 02-24-2004 03:41 AM



Advertisments