[Mono-list] Coding advice before I raise a bug
David Burnett
vargol at ntlworld.com
Wed Jun 11 22:01:43 UTC 2014
Hi
I may have found a compiler bug, but as I'm fairly new to c# I thought I'd
run it past more experienced developers before creating a test case
and raising it formally to see if I was missing some nuisance of c#.
The code looks like something like this, its actually the top of a larger
function that has been called recursively
The variables start and end are the functions parameters an=d have the values 1 ans 2047;
private void balanceSegment(Photon[] temp, int index, int start, int end)
{
Console.WriteLine(String.Format("index {0}, start {1}, end {2}", index, start, end));
int median = 1;
while ((4 * median) <= (end - start + 1))
median += median;
Console.WriteLine(String.Format("4 median {0}", median));
if ((3 * median) <= (end - start + 1))
{
median += median;
median += (start - 1);
Console.WriteLine(String.Format("3 median {0}", median));
}
else
median = end - median + 1;
This prints...
index 4, start 1, end 2047
4 median 512
3 median 512
It looks like the median += median for the if statement gets ignored.
If I change that for median *= 2 it prints the correct values...
private void balanceSegment(Photon[] temp, int index, int start, int end)
{
Console.WriteLine(String.Format("index {0}, start {1}, end {2}", index, start, end));
int median = 1;
while ((4 * median) <= (end - start + 1))
median += median;
Console.WriteLine(String.Format("4 median {0}", median));
if ((3 * median) <= (end - start + 1))
{
median *= 2;
median += (start - 1);
Console.WriteLine(String.Format("3 median {0}", median));
}
else
median = end - median + 1;
index 4, start 1, end 2047
4 median 512
3 median 1024
Oh, just for completeness, if I change the line median += (start - 1) to median += (start - 2) I get
index 4, start 1, end 2047
4 median 512
3 median 511
which means it is just ignoring the median += median line.
So, have I missed something obvious or is this a compiler bug ?
Dave
More information about the Mono-list
mailing list