Velocity Reviews > Problem with accuracy in DEV C++

# Problem with accuracy in DEV C++

Hatzigiannakis Nikos
Guest
Posts: n/a

 08-05-2008
In the following code

main()
{
double x;
for (x=0.0;x<=1;x=x+0.05)
{
printf("x=%6.3f \n",x);
}
}

I receive numbers from 0 to 0.950. 1 is not included. I understand that it
is an accuracy problem but how can I solve it ?

Hatzigiannakis Nikos
Guest
Posts: n/a

 08-05-2008
OK thanks but I hope that trere is an other more scientific way !

? "Richard Heathfield" <(E-Mail Removed)> ?????? ??? ??????
news:(E-Mail Removed)...
> Hatzigiannakis Nikos said:
>
>> In the following code
>>
>> main()
>> {
>> double x;
>> for (x=0.0;x<=1;x=x+0.05)
>> {
>> printf("x=%6.3f \n",x);
>> }
>> }
>>
>> I receive numbers from 0 to 0.950. 1 is not included. I understand that
>> it
>> is an accuracy problem but how can I solve it ?

>
> for (x = 0.0; x <= 1.04; x = x + 0.05)
>
> --
> Richard Heathfield <http://www.cpax.org.uk>
> Email: -http://www. +rjh@
> "Usenet is a strange place" - dmr 29 July 1999

Barry Schwarz
Guest
Posts: n/a

 08-05-2008
On Tue, 05 Aug 2008 03:22:40 -0400, Martin Ambuhl
<(E-Mail Removed)> wrote:

>Hatzigiannakis Nikos wrote:
>> In the following code

>
>> main()
>> {
>> double x;
>> for (x=0.0;x<=1;x=x+0.05)
>> {
>> printf("x=%6.3f \n",x);
>> }
>> }

>
>> I receive numbers from 0 to 0.950. 1 is not included. I understand that it
>> is an accuracy problem but how can I solve it ?

>
>#include <stdio.h> /* note */
>
>int main(void) /* note */
>{
> int i;
> for (i = 0; i <= 20; i++)
> printf("x=%6.3f\n", x/20.);

You meant i/20. perhaps.

> return 0; /* note */
>}

--
Remove del for email

Bartc
Guest
Posts: n/a

 08-05-2008

"Hatzigiannakis Nikos" <(E-Mail Removed)> wrote in message
news:g78thd\$plj\$(E-Mail Removed)...
> In the following code
>
> main()
> {
> double x;
> for (x=0.0;x<=1;x=x+0.05)
> {
> printf("x=%6.3f \n",x);
> }
> }
>
> I receive numbers from 0 to 0.950. 1 is not included. I understand that it
> is an accuracy problem but how can I solve it ?

Try changing double to float. And the 6.3 to something like 6.10. Also
temporarily change the x<=1 to x<=2:

float x;
for (x=0.0;x<=2;x=x+0.05)
{
printf("x=%6.10f\n",x);
}

Then hopefully you will see what the numerical problem is and can see a way
of fixing it.

You might see that when x gets to near 1.0, it will actually be slightly
more than 1.0, so that x<=1 will fail.

--
Bartc

CBFalconer
Guest
Posts: n/a

 08-05-2008
Hatzigiannakis Nikos wrote:
>
> In the following code
>
> main() {
> double x;
> for (x=0.0;x<=1;x=x+0.05) {
> printf("x=%6.3f \n",x);
> }
> }
>
> I receive numbers from 0 to 0.950. 1 is not included. I understand
> that it is an accuracy problem but how can I solve it ?

#include <stdio.h>
int main(void) {
double x;
int i;

for (i = 0; i <= 20; i++) {
printf("x=%6.3f \n", x = 0.05 * i);
}
return 0;
}

Note the corrections to main and #include.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>

Richard Bos
Guest
Posts: n/a

 08-05-2008
"Hatzigiannakis Nikos" <(E-Mail Removed)> wrote:

> ? "Richard Heathfield" <(E-Mail Removed)> ?????? ??? ??????
> > Hatzigiannakis Nikos said:

> >> double x;
> >> for (x=0.0;x<=1;x=x+0.05)
> >> {
> >> printf("x=%6.3f \n",x);
> >> }

> >> I receive numbers from 0 to 0.950. 1 is not included. I understand that it
> >> is an accuracy problem but how can I solve it ?

> >
> > for (x = 0.0; x <= 1.04; x = x + 0.05)

> OK thanks but I hope that trere is an other more scientific way !

There isn't. The problem is the inherent inexactitude of floating-point
numbers, and there's nothing much you can do about that. As the FAQ (the
section on FP is at <http://c-faq.com/fp/index.html>) explains, checking
FP numbers exactly is rarely if ever a good idea, and you're usually
better off allowing for a certain error.

Richard

Barry Schwarz
Guest
Posts: n/a

 08-05-2008
On Tue, 05 Aug 2008 13:32:44 GMT, http://www.velocityreviews.com/forums/(E-Mail Removed) (Richard
Bos) wrote:

>"Hatzigiannakis Nikos" <(E-Mail Removed)> wrote:
>
>
>> ? "Richard Heathfield" <(E-Mail Removed)> ?????? ??? ??????
>> > Hatzigiannakis Nikos said:

>
>> >> double x;
>> >> for (x=0.0;x<=1;x=x+0.05)
>> >> {
>> >> printf("x=%6.3f \n",x);
>> >> }

>
>> >> I receive numbers from 0 to 0.950. 1 is not included. I understand that it
>> >> is an accuracy problem but how can I solve it ?
>> >
>> > for (x = 0.0; x <= 1.04; x = x + 0.05)

>
>> OK thanks but I hope that trere is an other more scientific way !

>
>There isn't. The problem is the inherent inexactitude of floating-point
>numbers, and there's nothing much you can do about that. As the FAQ (the
>section on FP is at <http://c-faq.com/fp/index.html>) explains, checking
>FP numbers exactly is rarely if ever a good idea, and you're usually
>better off allowing for a certain error.

There are systems that support decimal floating point. I think there
is even an IEEE standard for it. While it doesn't change the nature
of the problem, it does change its appearance sufficiently to satisfy
people who think strictly in decimal. (If the OP's system used this,
it is unlikely he would ever have posted the question. Of course, he
would be confused as hell when it didn't work after being ported to a
binary system.)

--
Remove del for email

soscpd
Guest
Posts: n/a

 08-06-2008
Hello Nikos, List

My 2 cents.

On Aug 5, 2:50 am, "Hatzigiannakis Nikos" <(E-Mail Removed)> wrote:

> for (x=0.0;x<=1;x=x+0.05)
> {
> printf("x=%6.3f \n",x);
> } /*

/str/dev/tmp/trash_it_out_00173.c
*/

/**/
#include <stdlib.h>
#include <stdio.h>

/**/
int main(void)
{
double x;
for (x=0.0;x<=100;x=x+5)
{
printf("x=%6.3f \n",x/100);
}
return EXIT_SUCCESS;
}

/*eof*/

Rafael

rahul
Guest
Posts: n/a

 08-06-2008
On Aug 6, 12:27*am, "Dann Corbit" <(E-Mail Removed)> wrote:

> Of course, decimal arithmetic contains just as many surprises as floating
> point. *Multiply two small quantities and get an itty-bitty one that gets
> truncated. *Multiply this tiny thing which has lost all of its precision by
> a huge thing and get a catastrophically wrong answer. *But at least it will
> look nice.

models the way we think about numbers. Whereas floating poing does
some very weird things(viz. 0.1 + 0.2 != 0.3).

CBFalconer
Guest
Posts: n/a

 08-06-2008
rahul wrote:
> "Dann Corbit" <(E-Mail Removed)> wrote:
>
>> Of course, decimal arithmetic contains just as many surprises as
>> floating point. Multiply two small quantities and get an itty-
>> bitty one that gets truncated. Multiply this tiny thing which
>> has lost all of its precision by a huge thing and get a
>> catastrophically wrong answer. But at least it will look nice.

>
> models the way we think about numbers. Whereas floating poing does
> some very weird things(viz. 0.1 + 0.2 != 0.3).

Well, try such things as (1/3) * 3. You can substitute any prime
not a factor of the base for 3. Then think about countability.
Integers are countable. Reals are not.

Countability simply means put in one to one correspondence with the
integers.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>