Velocity Reviews > Double precision numbers.

# 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?

Regards,
Allagappan M

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

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

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

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/

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/

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'.

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

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?
>
> 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

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