Infinite loop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dmoran21@cox.net

    #1

    Infinite loop

    Hi all, I am a mathematician and I'm trying to write a program to try
    out a formula that I've derived. However, it seems that I've got an
    infinite loop and I don't quite understand why. I was hoping someone
    could point me in the right direction.

    Code:

    #include <stdio.h>
    #include <math.h>

    int main()
    {
    float start = 0;
    float end = 0;
    float step = 0;
    float x = 0;
    float y = 0;

    printf("Enter the starting value\n");
    scanf("%f", &start);
    printf("Enter the ending value\n");
    scanf("%f", &end);
    printf("Enter the step\n");
    scanf("%f", &step);

    for(x=start;x=e nd;x+=step) {
    y = (pow(260,.5)/130)*cos(2*x-1.463) + (67/20)*exp(x) +
    (37/52)*exp(-3*x);
    printf("%3.3f %3.3f\n",x,y);
    }
    }

    Thanks,
    Dave

  • osmium

    #2
    Re: Infinite loop

    <dmoran21@cox.n etwrote:
    Hi all, I am a mathematician and I'm trying to write a program to try
    out a formula that I've derived. However, it seems that I've got an
    infinite loop and I don't quite understand why. I was hoping someone
    could point me in the right direction.
    >
    Code:
    >
    #include <stdio.h>
    #include <math.h>
    >
    int main()
    {
    float start = 0;
    float end = 0;
    float step = 0;
    float x = 0;
    float y = 0;
    >
    printf("Enter the starting value\n");
    scanf("%f", &start);
    printf("Enter the ending value\n");
    scanf("%f", &end);
    printf("Enter the step\n");
    scanf("%f", &step);
    >
    for(x=start;x=e nd;x+=step) {
    Did you mean x==end ?
    y = (pow(260,.5)/130)*cos(2*x-1.463) + (67/20)*exp(x) +
    (37/52)*exp(-3*x);
    printf("%3.3f %3.3f\n",x,y);
    }
    }
    >
    Thanks,
    Dave
    >

    Comment

    • dmoran21@cox.net

      #3
      Re: Infinite loop

      On Mar 17, 8:39 pm, "osmium" <r124c4u...@com cast.netwrote:
      <dmora...@cox.n etwrote:
      Hi all, I am a mathematician and I'm trying to write a program to try
      out a formula that I've derived. However, it seems that I've got an
      infinite loop and I don't quite understand why. I was hoping someone
      could point me in the right direction.
      >
      Code:
      >
      #include <stdio.h>
      #include <math.h>
      >
      int main()
      {
      float start = 0;
      float end = 0;
      float step = 0;
      float x = 0;
      float y = 0;
      >
      printf("Enter the starting value\n");
      scanf("%f", &start);
      printf("Enter the ending value\n");
      scanf("%f", &end);
      printf("Enter the step\n");
      scanf("%f", &step);
      >
      for(x=start;x=e nd;x+=step) {
      >
      Did you mean x==end ?
      >
      y = (pow(260,.5)/130)*cos(2*x-1.463) + (67/20)*exp(x) +
      (37/52)*exp(-3*x);
      printf("%3.3f %3.3f\n",x,y);
      }
      }
      >
      Thanks,
      Dave
      That stopped the infinite loop, but now, it's stopping after the input
      is entered.

      Dave

      Comment

      • santosh

        #4
        Re: Infinite loop

        dmoran21@cox.ne t wrote:
        Hi all, I am a mathematician
        Didn't you start out with this exact same phrase in a post some time
        ago. As I recall, it start a flame thread.
        and I'm trying to write a program to try
        out a formula that I've derived. However, it seems that I've got an
        infinite loop and I don't quite understand why. I was hoping someone
        could point me in the right direction.
        >
        Code:
        >
        #include <stdio.h>
        #include <math.h>
        >
        int main()
        {
        float start = 0;
        float end = 0;
        float step = 0;
        float x = 0;
        float y = 0;
        Unless you've good reasons, you might consider using double for these
        objects. It provides greater default precision.
        printf("Enter the starting value\n");
        scanf("%f", &start);
        printf("Enter the ending value\n");
        scanf("%f", &end);
        printf("Enter the step\n");
        scanf("%f", &step);
        >
        for(x=start;x=e nd;x+=step) {
        The = operator is the assignment operator. You probably meant <= or <
        or some other relational or logical relation. Otherwise the test
        simply assigns end to x and evaluates x. If x is other than zero, the
        loop will continue. So if the user had entered a positive or negative
        value for end, the program will enter an infinite loop.
        y = (pow(260,.5)/130)*cos(2*x-1.463) + (67/20)*exp(x) + (37/52)*exp(-3*x);
        Mismatched parenthesis. If you don't cut and paste the exact code that
        failed to compile or run, then it's guessing all the way.

        You should split up the above statement into multiple steps, with a
        temporary or two. By reordering it you can preserve more accuracy and
        enhance readability.
        printf("%3.3f %3.3f\n",x,y);
        }
        }

        Comment

        • Thad Smith

          #5
          Re: Infinite loop

          dmoran21@cox.ne t wrote:
          for(x=start;x=e nd;x+=step) {
          y = (pow(260,.5)/130)*cos(2*x-1.463) + (67/20)*exp(x) +
          (37/52)*exp(-3*x);
          printf("%3.3f %3.3f\n",x,y);
          }
          Besides the problem with the for loop, the expressions (67/20) and
          (37/52) are integer expressions that yield 3 and 0, respectively. To
          get floating point values, insert a decimal point: (67./27.). The other
          integers in the expression, 260, 130, 2, and 3, are already converted to
          type double because of
          1) use as a parameter of type double and
          2) operation with an expression of type double.

          In the case of (-3*x), the integer 3 is negated, yielding -3 as an
          integer, then converted to double (probably all at compile time) before
          multiplying by x.

          You can add decimal points to all these constants if you don't want to
          bother remembering the rules for when conversion to floating point is done.

          --
          Thad

          Comment

          • osmium

            #6
            Re: Infinite loop

            <dmoran21@cox.n etwrote:
            On Mar 17, 8:39 pm, "osmium" <r124c4u...@com cast.netwrote:
            ><dmora...@cox. netwrote:
            Hi all, I am a mathematician and I'm trying to write a program to try
            out a formula that I've derived. However, it seems that I've got an
            infinite loop and I don't quite understand why. I was hoping someone
            could point me in the right direction.
            >>
            Code:
            >>
            #include <stdio.h>
            #include <math.h>
            >>
            int main()
            {
            float start = 0;
            float end = 0;
            float step = 0;
            float x = 0;
            float y = 0;
            >>
            printf("Enter the starting value\n");
            scanf("%f", &start);
            printf("Enter the ending value\n");
            scanf("%f", &end);
            printf("Enter the step\n");
            scanf("%f", &step);
            >>
            for(x=start;x=e nd;x+=step) {
            >>
            >Did you mean x==end ?
            >>
            y = (pow(260,.5)/130)*cos(2*x-1.463) + (67/20)*exp(x) +
            (37/52)*exp(-3*x);
            printf("%3.3f %3.3f\n",x,y);
            }
            }
            >>
            Thanks,
            Dave
            >
            That stopped the infinite loop, but now, it's stopping after the input
            is entered.
            In general, you should never test any floating point type for equality, such
            numbers are represented as approximations to the real underlying number.
            Testing for x==end is like trying to balance a dime on the rim. Changing to
            x<= end yields a short table of numbers for me.


            Comment

            • dmoran21@cox.net

              #7
              Re: Infinite loop

              On Mar 17, 9:34 pm, "osmium" <r124c4u...@com cast.netwrote:
              <dmora...@cox.n etwrote:
              On Mar 17, 8:39 pm, "osmium" <r124c4u...@com cast.netwrote:
              <dmora...@cox.n etwrote:
              Hi all, I am a mathematician and I'm trying to write a program to try
              out a formula that I've derived. However, it seems that I've got an
              infinite loop and I don't quite understand why. I was hoping someone
              could point me in the right direction.
              >
              Code:
              >
              #include <stdio.h>
              #include <math.h>
              >
              int main()
              {
              float start = 0;
              float end = 0;
              float step = 0;
              float x = 0;
              float y = 0;
              >
              printf("Enter the starting value\n");
              scanf("%f", &start);
              printf("Enter the ending value\n");
              scanf("%f", &end);
              printf("Enter the step\n");
              scanf("%f", &step);
              >
              for(x=start;x=e nd;x+=step) {
              >
              Did you mean x==end ?
              >
              y = (pow(260,.5)/130)*cos(2*x-1.463) + (67/20)*exp(x) +
              (37/52)*exp(-3*x);
              printf("%3.3f %3.3f\n",x,y);
              }
              }
              >
              Thanks,
              Dave
              >
              That stopped the infinite loop, but now, it's stopping after the input
              is entered.
              >
              In general, you should never test any floating point type for equality, such
              numbers are represented as approximations to the real underlying number.
              Testing for x==end is like trying to balance a dime on the rim. Changing to
              x<= end yields a short table of numbers for me.
              Would it be better to use a do loop? I thought that I remember hearing
              something that you should use a for loop only if you are using
              integers to count.

              Dave

              Comment

              • Barry Schwarz

                #8
                Re: Infinite loop

                On 17 Mar 2007 18:45:31 -0700, "dmoran21@cox.n et" <dmoran21@cox.n et>
                wrote:
                >On Mar 17, 8:39 pm, "osmium" <r124c4u...@com cast.netwrote:
                ><dmora...@cox. netwrote:
                Hi all, I am a mathematician and I'm trying to write a program to try
                out a formula that I've derived. However, it seems that I've got an
                infinite loop and I don't quite understand why. I was hoping someone
                could point me in the right direction.
                >>
                Code:
                >>
                #include <stdio.h>
                #include <math.h>
                >>
                int main()
                {
                float start = 0;
                float end = 0;
                float step = 0;
                float x = 0;
                float y = 0;
                >>
                printf("Enter the starting value\n");
                scanf("%f", &start);
                printf("Enter the ending value\n");
                scanf("%f", &end);
                printf("Enter the step\n");
                scanf("%f", &step);
                >>
                for(x=start;x=e nd;x+=step) {
                >>
                >Did you mean x==end ?
                >>
                y = (pow(260,.5)/130)*cos(2*x-1.463) + (67/20)*exp(x) +
                (37/52)*exp(-3*x);
                printf("%3.3f %3.3f\n",x,y);
                }
                }
                >>
                Thanks,
                Dave
                >
                >That stopped the infinite loop, but now, it's stopping after the input
                >is entered.
                >
                The conditional expression is evaluated before the first iteration.
                Since x is not equal to end, the expression evaluates to zero and
                looping is terminated even before the first iteration.


                Remove del for email

                Comment

                • Richard Heathfield

                  #9
                  Re: Infinite loop

                  dmoran21@cox.ne t said:

                  <snip>
                  Would it be better to use a do loop? I thought that I remember hearing
                  something that you should use a for loop only if you are using
                  integers to count.
                  No, there is no such rule or guideline, or at least not amongst those
                  who know the language well!

                  If anything, your for(counter = start; counter < end; counter += step)
                  example is actually a rather good example of when a for-loop is
                  appropriate - but of course a while or a do would get the job done just
                  as well.

                  --
                  Richard Heathfield
                  "Usenet is a strange place" - dmr 29/7/1999

                  email: rjh at the above domain, - www.

                  Comment

                  • Malcolm McLean

                    #10
                    Re: Infinite loop


                    <dmoran21@cox.n etwrote in message
                    #include <stdio.h>
                    #include <math.h>
                    >>
                    int main()
                    {
                    float start = 0;
                    float end = 0;
                    float step = 0;
                    float x = 0;
                    float y = 0;
                    >>
                    printf("Enter the starting value\n");
                    scanf("%f", &start);
                    printf("Enter the ending value\n");
                    scanf("%f", &end);
                    printf("Enter the step\n");
                    scanf("%f", &step);
                    >>
                    for(x=start;x=e nd;x+=step) {
                    >>
                    >Did you mean x==end ?
                    >>
                    y = (pow(260,.5)/130)*cos(2*x-1.463) + (67/20)*exp(x) +
                    (37/52)*exp(-3*x);
                    printf("%3.3f %3.3f\n",x,y);
                    }
                    }
                    >>
                    Thanks,
                    Dave
                    >>
                    That stopped the infinite loop, but now, it's stopping after the input
                    is entered.
                    >>
                    >In general, you should never test any floating point type for equality,
                    >such
                    >numbers are represented as approximations to the real underlying number.
                    >Testing for x==end is like trying to balance a dime on the rim. Changing
                    >to
                    >x<= end yields a short table of numbers for me.
                    >
                    Would it be better to use a do loop? I thought that I remember hearing
                    something that you should use a for loop only if you are using
                    integers to count.
                    >
                    There are certain difficulties in using floats as the counter within a for
                    loop.
                    One is that, if the numbers are not integers, the test x != end may fail
                    because of numerical precision. This can be fixed with x <= end + epsilon.

                    Another problem is that programmers are so used to seeing for loops used to
                    index arrays that it can be confusing to see one used for another purpose.

                    The final problem in mathematics is that the funny-looking E notation used
                    for sums, conventionally, takes integral indices.

                    However if a for() loop most naturally expresses the logic of your
                    calculation, then use one.
                    --
                    Free games and programming goodies.


                    Comment

                    • Chris Johnson

                      #11
                      Re: Infinite loop

                      On Mar 17, 8:45 pm, "dmora...@cox.n et" <dmora...@cox.n etwrote:
                      On Mar 17, 8:39 pm, "osmium" <r124c4u...@com cast.netwrote:
                      >
                      >
                      >
                      <dmora...@cox.n etwrote:
                      Hi all, I am a mathematician and I'm trying to write a program to try
                      out a formula that I've derived. However, it seems that I've got an
                      infinite loop and I don't quite understand why. I was hoping someone
                      could point me in the right direction.
                      >
                      Code:
                      >
                      #include <stdio.h>
                      #include <math.h>
                      >
                      int main()
                      {
                      float start = 0;
                      float end = 0;
                      float step = 0;
                      float x = 0;
                      float y = 0;
                      >
                      printf("Enter the starting value\n");
                      scanf("%f", &start);
                      printf("Enter the ending value\n");
                      scanf("%f", &end);
                      printf("Enter the step\n");
                      scanf("%f", &step);
                      >
                      for(x=start;x=e nd;x+=step) {
                      >
                      Did you mean x==end ?
                      >
                      y = (pow(260,.5)/130)*cos(2*x-1.463) + (67/20)*exp(x) +
                      (37/52)*exp(-3*x);
                      printf("%3.3f %3.3f\n",x,y);
                      }
                      }
                      >
                      Thanks,
                      Dave
                      >
                      That stopped the infinite loop, but now, it's stopping after the input
                      is entered.
                      I think you're expecting the second part of the for loop to be an end
                      condition, i.e. You want to stop when x is equal to end. But the
                      second part is a boolean condition, i.e. the loop will continue until
                      that condition is false. You probably want x < end instead of x ==
                      end, so the loop will stop when x >= end.

                      Comment

                      • Mark L Pappin

                        #12
                        Re: Infinite loop

                        "Malcolm McLean" <regniztar@btin ternet.comwrite s:
                        in mathematics ... the funny-looking E notation used for sums
                        <OT>
                        By "funny-looking E" would you mean "Sigma", perchance?
                        </OT>

                        mlp

                        Comment

                        • Old Wolf

                          #13
                          Re: Infinite loop

                          On Mar 18, 1:32 pm, "dmora...@cox.n et" <dmora...@cox.n etwrote:
                          Hi all, I am a mathematician and I'm trying to write a program to try
                          out a formula that I've derived.
                          >
                          int main()
                          {
                          printf("Enter the starting value\n");
                          scanf("%f", &start);
                          printf("Enter the ending value\n");
                          scanf("%f", &end);
                          printf("Enter the step\n");
                          scanf("%f", &step);
                          What happens if the person types "x", or just presses Enter?
                          for(x=start;x=e nd;x+=step) {
                          '=' is the assignment operator. The equality test is '=='.

                          Also, as others have pointed out, testing for equality with
                          floating point types is unreliable.
                          y = (pow(260,.5)/130)*cos(2*x-1.463) + (67/20)*exp(x) +
                          (37/52)*exp(-3*x);
                          int divided by int gives int, in C, so 67/20 gives 3 and 37/52 gives
                          0.
                          You can fix it by writing 67./20 and 37./52
                          printf("%3.3f %3.3f\n",x,y);
                          }
                          Consider using 'double' instead of 'float'. You will also have
                          to change your scanf modifiers to '%lf' (or preferably use
                          fgets and strtod instead of scanf!).

                          Comment

                          • Norm Mann

                            #14
                            Re: Infinite loop


                            "Thad Smith" <ThadSmith@acm. orgwrote in message
                            news:45fca28d$0 $27529$892e0abb @auth.newsreade r.octanews.com. ..
                            dmoran21@cox.ne t wrote:
                            >
                            >for(x=start;x= end;x+=step) {
                            >y = (pow(260,.5)/130)*cos(2*x-1.463) + (67/20)*exp(x) +
                            >(37/52)*exp(-3*x);
                            >printf("%3.3 f %3.3f\n",x,y);
                            >}
                            >
                            Besides the problem with the for loop, the expressions (67/20) and (37/52)
                            are integer expressions that yield 3 and 0, respectively. To get floating
                            point values, insert a decimal point: (67./27.). The other integers in
                            the expression, 260, 130, 2, and 3, are already converted to type double
                            because of
                            1) use as a parameter of type double and
                            2) operation with an expression of type double.
                            >
                            In the case of (-3*x), the integer 3 is negated, yielding -3 as an
                            integer, then converted to double (probably all at compile time) before
                            multiplying by x.
                            >
                            Since (pow(260,.5)/130), (67/20) and (37/52) are all constants, if you
                            precompute them and insert them as actual values, you can save yourself some
                            processing time.

                            -NM



                            Comment

                            • Thad Smith

                              #15
                              Re: Infinite loop

                              Norm Mann wrote:
                              "Thad Smith" <ThadSmith@acm. orgwrote in message
                              news:45fca28d$0 $27529$892e0abb @auth.newsreade r.octanews.com. ..
                              >
                              >>dmoran21@cox. net wrote:
                              >>
                              >>
                              >>>for(x=start; x=end;x+=step) {
                              >>>y = (pow(260,.5)/130)*cos(2*x-1.463) + (67/20)*exp(x) +
                              >>>(37/52)*exp(-3*x);
                              >>>printf("%3.3 f %3.3f\n",x,y);
                              >>>}
                              >>
                              >>Besides the problem with the for loop, the expressions (67/20) and (37/52)
                              >>are integer expressions that yield 3 and 0, respectively. To get floating
                              >>point values, insert a decimal point: (67./27.). The other integers in
                              >>the expression, 260, 130, 2, and 3, are already converted to type double
                              >>because of
                              >>1) use as a parameter of type double and
                              >>2) operation with an expression of type double.
                              >
                              Since (pow(260,.5)/130), (67/20) and (37/52) are all constants, if you
                              precompute them and insert them as actual values, you can save yourself some
                              processing time.
                              True, but I prefer having the program compute them to show the
                              derivation. If speed is an issue, you can compute the constants once in
                              the code and assign to variables.

                              --
                              Thad

                              Comment

                              Working...