Problem with accuracy in DEV C++

Discussion in 'C Programming' started by Hatzigiannakis Nikos, Aug 5, 2008.

  1. 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, Aug 5, 2008
    #1
    1. Advertising

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

    ? "Richard Heathfield" <> ?????? ??? ??????
    news:...
    > 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@
    > Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    > "Usenet is a strange place" - dmr 29 July 1999
    Hatzigiannakis Nikos, Aug 5, 2008
    #2
    1. Advertising

  3. On Tue, 05 Aug 2008 03:22:40 -0400, Martin Ambuhl
    <> 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
    Barry Schwarz, Aug 5, 2008
    #3
  4. Hatzigiannakis Nikos

    Bartc Guest

    "Hatzigiannakis Nikos" <> wrote in message
    news:g78thd$plj$...
    > 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
    Bartc, Aug 5, 2008
    #4
  5. Hatzigiannakis Nikos

    CBFalconer Guest

    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>
    Try the download section.
    CBFalconer, Aug 5, 2008
    #5
  6. Hatzigiannakis Nikos

    Richard Bos Guest

    "Hatzigiannakis Nikos" <> wrote:

    [ Please don't top-post. ]

    > ? "Richard Heathfield" <> ?????? ??? ??????
    > > 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
    Richard Bos, Aug 5, 2008
    #6
  7. On Tue, 05 Aug 2008 13:32:44 GMT, (Richard
    Bos) wrote:

    >"Hatzigiannakis Nikos" <> wrote:
    >
    >[ Please don't top-post. ]
    >
    >> ? "Richard Heathfield" <> ?????? ??? ??????
    >> > 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
    Barry Schwarz, Aug 5, 2008
    #7
  8. Hatzigiannakis Nikos

    soscpd Guest

    Hello Nikos, List

    My 2 cents.

    On Aug 5, 2:50 am, "Hatzigiannakis Nikos" <> 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
    soscpd, Aug 6, 2008
    #8
  9. Hatzigiannakis Nikos

    rahul Guest

    On Aug 6, 12:27 am, "Dann Corbit" <> 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.

    Can you be specific about your surprises? IMO decimal arithmetic
    models the way we think about numbers. Whereas floating poing does
    some very weird things(viz. 0.1 + 0.2 != 0.3).
    rahul, Aug 6, 2008
    #9
  10. Hatzigiannakis Nikos

    CBFalconer Guest

    rahul wrote:
    > "Dann Corbit" <> 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.

    >
    > Can you be specific about your surprises? IMO decimal arithmetic
    > 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>
    Try the download section.
    CBFalconer, Aug 6, 2008
    #10
  11. In article <211a7$48994aaa$> "Dann Corbit" <> writes:
    ....
    > If you have a decimal value with format DECIMAL(15,2) and perform the
    > following operation:
    >
    > 0.25 * 0.25 = .06


    You are assuming fixed point decimal...

    > On the other hand, decimal values do have the very pleasant property of
    > adding to the same value when you sum then forwards and backwards, whereas
    > binary floating point does not have this property.


    Wrong if you consider floating point decimal. Also wrong if you consider
    fixed point decimal and fixed point binary. And it is not even true in
    fixed point. In one direction it is possible to have an intermediate
    overflow that does not occur in the other direction.
    --
    dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
    Dik T. Winter, Aug 6, 2008
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Q2hhcmxlc0E=?=

    Accuracy and CSS

    =?Utf-8?B?Q2hhcmxlc0E=?=, Jan 16, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    669
  2. Eric Wilhelm

    accuracy problem

    Eric Wilhelm, Nov 15, 2003, in forum: Perl Misc
    Replies:
    12
    Views:
    188
    Tassilo v. Parseval
    Nov 17, 2003
  3. Debashish Saha

    accuracy problem in calculation

    Debashish Saha, Nov 8, 2012, in forum: Python
    Replies:
    0
    Views:
    142
    Debashish Saha
    Nov 8, 2012
  4. Chris Angelico

    Re: accuracy problem in calculation

    Chris Angelico, Nov 8, 2012, in forum: Python
    Replies:
    1
    Views:
    162
    Grant Edwards
    Nov 8, 2012
  5. Dave Angel

    Re: accuracy problem in calculation

    Dave Angel, Nov 8, 2012, in forum: Python
    Replies:
    0
    Views:
    164
    Dave Angel
    Nov 8, 2012
Loading...

Share This Page