Problem with the while statemnet

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mickey22
    New Member
    • Feb 2007
    • 105

    Problem with the while statemnet

    I have some problem in execution of the program :

    the code is for example:

    void a (float value)
    {

    float size=0;
    size=value;
    while(size>=val ue)
    {
    some statements
    }

    when I print the values of size and value, they are the same.but it is
    not entering the while loop even though the condition is true.If I just write some
    cout statement it enters into the loop.

    when I run the debugger , value =1e-10 and when I assign the same value to
    size, size=1.074e-038#DEN in the watch window.

    When I display using the cout I get both the same values.

    I am not able to understand what the problem is.I tried to run in debug and release versions , it does'nt work.

    Could anyone give any suggestion ?Thanks
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by mickey22
    I have some problem in execution of the program :

    the code is for example:

    void a (float value)
    {

    float size=0;
    size=value;
    while(size>=val ue)
    {
    some statements
    }

    when I print the values of size and value, they are the same.but it is
    not entering the while loop even though the condition is true.If I just write some
    cout statement it enters into the loop.

    when I run the debugger , value =1e-10 and when I assign the same value to
    size, size=1.074e-038#DEN in the watch window.

    When I display using the cout I get both the same values.

    I am not able to understand what the problem is.I tried to run in debug and release versions , it does'nt work.

    Could anyone give any suggestion ?Thanks

    HI,
    Floating numerals are not suited for comparion in the condition statements.
    Sometimes the condition may be successful and sometimes not.
    Raghuram

    Comment

    • jinendrashankar
      New Member
      • Nov 2007
      • 10

      #3
      Hi this code working fine for me check code & result

      Code:--->

      1 #include<iostre am.h>
      2 void a (float value)
      3 {
      4
      5 float size=0;
      6 size=value;
      7 while(size>=val ue)
      8 {cout<<"Inside of while"<<endl;
      9 cout<<"value of size in Function A = \t "<<size<<en dl;
      10 cout<<"value of value in function A = \t"<<value<<end l;
      11 value++;
      12 }
      13 }
      14 int main ()
      15 {
      16 float v=1e-10;
      17 cout<<"value of V in Main = \t"<<v<<endl;
      18 a(v);
      19 cout<<"out of main"<<endl;
      20 return 0;
      21 }
      22

      output:--->

      value of V in Main = 1e-10
      Inside of while
      value of size in Function A = 1e-10
      value of value in function A = 1e-10
      out of main

      Comment

      • mickey22
        New Member
        • Feb 2007
        • 105

        #4
        I just tried in the same way writing separately..It works fine.When I include some other statements it does'nt work.Its really wierd.

        Thanks for all the suggestions.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Generally, you cannot use operators like ==, !=, >, <, <=, >=, etc wth floating point numbers.
          Due to the automatic rounding, these operators may report a condition as true when the numbers
          are only close in value.

          Google for Floating Point Arithmetic or read IEEE 754 standard specification for details.

          To compare two floating point numbers, you have to establish a sigma error tolerance.

          Code:
          if (fabs(i - j) < 0.000001) { ... // almost equal }

          Comment

          Working...