Results 1 to 6 of 6

Math Help - software problem?

  1. #1
    Newbie
    Joined
    May 2010
    Posts
    3

    Unhappy software problem?

    I've the following code in Matlab:

    Code:
    clear;
    s=100;
    for i = 0.05 : 0.05 : s
      if i == 1 %8.05
        i
      end 
    end
    in some cases ( for example 1 or .05 and so on) it works, the code prints out i, but in many other cases ( for example 10) it doesn't work it doesn't write out i

    I don't know why I tried many things for example using 10.0000 or double().

    if anybody has a good idea please write it me! Thanks


    Hi again!

    I wrote this code:

    Code:
    clear;
    k = 0;
    
    for j = 0.05 : 0.05 : 15
    
      for i = 0.05 : 0.05 : 15
        if i == 10 % if I use i == j it works but in this situation the code doesn't write out i
          i
          k = k+1;
        end 
      end
    
    end
    Last edited by CaptainBlack; May 5th 2010 at 11:40 PM.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by ftomi7 View Post
    I've the following code in Matlab:

    Code:
    clear;
    s=100;
    for i = 0.05 : 0.05 : s
      if i == 1 %8.05
        i
      end 
    end
    in some cases ( for example 1 or .05 and so on) it works, the code prints out i, but in many other cases ( for example 10) it doesn't work it doesn't write out i

    I don't know why I tried many things for example using 10.0000 or double().

    if anybody has a good idea please write it me! Thanks


    Hi again!

    I wrote this code:

    Code:
    clear;
    k = 0;
     
    for j = 0.05 : 0.05 : 15
     
      for i = 0.05 : 0.05 : 15
        if i == 10 % if I use i == j it works but in this situation the code doesn't write out i
          i
          k = k+1;
        end 
      end
     
    end
    Your question is difficult to understand, but at a guess is that your problem is a result of trying to test floating point numbers for equality. This is always a problem as floats are only under certain circumstances exact representations of the numbers you think they are.

    Try this:

    Code:
    clear;
    k = 0;
     
    for j = 0.05 : 0.05 : 15
     
      for i = 0.05 : 0.05 : 15
        if abs(i-10) < 1e-11
          i
          k = k+1;
        end 
      end
     
    end
    CB
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    May 2010
    Posts
    3
    thanks

    but I don't understand in which situations it works... why is good 8 and why isn't good 10?

    for example it never will be equal:

    for i = 0.05 : 0.05 : 15
    if i == 10
    i
    end
    end
    Last edited by ftomi7; May 6th 2010 at 12:19 AM.
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by ftomi7 View Post
    thanks

    but I don't understand in which situations it works... why is good 8 and why isn't good 10?

    for example it never will be equal:

    for i = 0.05 : 0.05 : 15
    if i == 10
    i
    end
    end
    It does not matter what you understand or do not, it has to do with which decimal fractions can be represented exactly in binary. Never compare floats for equality.

    CB
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Newbie
    Joined
    May 2010
    Posts
    3
    I think I found a solution, but it may not the best, but it works:

    for i = 0.05 : 0.05 : 15
    if round(i*10000) == round(10*10000)
    i;
    end
    end

    In the above code I convert double to int.
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by ftomi7 View Post
    I think I found a solution, but it may not the best, but it works:
    Quote Originally Posted by ftomi7 View Post

    Code:
    for i = 0.05 : 0.05 : 15 
       if round(i*10000) == round(10*10000)
         i;
       end
    end


    In the above code I convert double to int.


    1. No you don't. The result of round is not an int type but is still a float but with the fractional part set to zero, as long as the integer part fits into 54 bits (IIRC) this is exactly representable in a float.

    2. Did you read my previous post that gave a perfectly acceptable method of doing what you want (more efficiently and in fewer operations)?

    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Replies: 1
    Last Post: October 16th 2011, 09:08 PM
  2. Software advice
    Posted in the Math Software Forum
    Replies: 6
    Last Post: March 4th 2011, 02:35 PM
  3. Which Software?
    Posted in the Math Software Forum
    Replies: 5
    Last Post: August 27th 2009, 02:46 AM
  4. Is there any software that does this?
    Posted in the Math Software Forum
    Replies: 1
    Last Post: November 1st 2008, 12:54 PM

Search Tags


/mathhelpforum @mathhelpforum