CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 22 of 22

Thread: loop problem?

  1. #16
    Join Date
    Aug 2007
    Posts
    39

    Re: loop problem?

    Quote Originally Posted by Zaccheus
    Yes, but isn't n*(n/4 + 0.5) exactly the same as (n *n)/4 + (n* 0.5) ?
    if you know algebra then you should know this
    c * (a + b) = c * a + c * b;
    2 * (3 + 5) = 2 * 3 + 2 *5;
    16 = 16 ;

  2. #17
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: loop problem?

    Indeed.

    Quote Originally Posted by klamath23
    This works for human, this does'nt work in c#.
    You are correct, because it is integer division!

    It would wouk if he wrote n / 4.0.
    My hobby projects:
    www.rclsoftware.org.uk

  3. #18
    Join Date
    May 2007
    Posts
    1,546

    Re: loop problem?

    Quote Originally Posted by Zaccheus
    6*(6/4+0.5) = 6 * 2 = 12
    Ah, but you're performing integer division, not floating point division.

    6/4 == 1

    EDIT: It won't actually be completly accurate if you just change it to 4.0. You may end up with an answer like 11.99999999 instead of 12.

    To round a floating point number to the *nearest* integer what you need to do is add 0.5 then just cast the float to int.

    i.e.

    double result = n * (n / 4.0 + 0.5);
    int finalAnswer = (int)(result + 0.5);

    Alternatively one of the other forms of the equation would be a better way of doing it.
    Last edited by Mutant_Fruit; August 23rd, 2007 at 09:48 AM.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  4. #19
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: loop problem?

    Well, as it happens 1.5, can be accurately reprepresented in binary - but I do take your point. Rounding errors are always an issue in floating point calculations.
    My hobby projects:
    www.rclsoftware.org.uk

  5. #20
    Join Date
    Aug 2007
    Posts
    39

    Re: loop problem?

    Quote Originally Posted by Zaccheus
    Well, as it happens 1.5, can be accurately reprepresented in binary - but I do take your point. Rounding errors are always an issue in floating point calculations.
    There are better ways to do this problem
    like this
    Code:
    int sumEven = 0;
    for(int i =2; i<=n;n++)
    {
    if((i %  2)== 0)
    sumEven+=i;
    }
    
    Console.WriteLine(" the sum of even numbers till n is:"+sumEven);

  6. #21
    Join Date
    May 2003
    Location
    Germany
    Posts
    936

    Re: loop problem?

    Quote Originally Posted by Zaccheus
    Indeed.



    You are correct, because it is integer division!

    It would wouk if he wrote n / 4.0.
    You are right. It was just a typo mistake of me because I did not test the code. It means n / 4.0 on that part.

    But I am understand the point of view for a rounding error. There is a simple solution. You have only to eliminate the fraction inside.

    Code:
    public uint Calculate(uint n)
    {
        //ensure the number is an even number
        if (n % 2 == 1)
           n -= 1;
        //calculate sum without a loop because it is a arithmetic progression
        return n*(n + 2) >> 2;
    }
    That's it. There should no rounding error any longer.
    Useful or not? Rate my posting. Thanks.

  7. #22
    Join Date
    Aug 2007
    Posts
    23

    Re: loop problem?

    thanks for the help but I myself solved it
    www.coderewind.com
    Best Place to hunt for Code

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured