|
-
August 23rd, 2007, 09:40 AM
#16
Re: loop problem?
 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 ;
-
August 23rd, 2007, 09:43 AM
#17
Re: loop problem?
Indeed.
 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.
-
August 23rd, 2007, 09:45 AM
#18
Re: loop problem?
 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.
-
August 23rd, 2007, 09:57 AM
#19
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.
-
August 23rd, 2007, 10:04 AM
#20
Re: loop problem?
 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);
-
August 23rd, 2007, 10:46 AM
#21
Re: loop problem?
 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.
-
September 8th, 2007, 06:30 AM
#22
Re: loop problem?
thanks for the help but I myself solved it
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|