Click here to Skip to main content
15,889,527 members
Home / Discussions / C#
   

C#

 
AnswerRe: Decimal compare Pin
C. David Johnson6-Oct-17 5:56
C. David Johnson6-Oct-17 5:56 
GeneralRe: Decimal compare Pin
Luc Pattyn6-Oct-17 6:06
sitebuilderLuc Pattyn6-Oct-17 6:06 
GeneralRe: Decimal compare Pin
C. David Johnson6-Oct-17 6:13
C. David Johnson6-Oct-17 6:13 
AnswerRe: Decimal compare Pin
Gerry Schmitz7-Oct-17 5:48
mveGerry Schmitz7-Oct-17 5:48 
QuestionDeserialization issue (Should be easy) Pin
C. David Johnson5-Oct-17 9:38
C. David Johnson5-Oct-17 9:38 
AnswerRe: Deserialization issue (Should be easy) Pin
Richard Deeming6-Oct-17 2:25
mveRichard Deeming6-Oct-17 2:25 
AnswerRe: Deserialization issue (Should be easy) Pin
C. David Johnson6-Oct-17 5:03
C. David Johnson6-Oct-17 5:03 
QuestionHeisenbug in program without async, etc. Pin
Alexander Kindel5-Oct-17 6:47
Alexander Kindel5-Oct-17 6:47 
I'm working on a program that is behaving differently when I debug than when I run it. Everyone I've described it to says it sounds like a data race is happening, but if I've done any of the kinds of things that can possibly lead to data races, I've done it without realizing.

It won't compile on its own, but this is the function that is behaving unexpectedly:

C#
static public Dictionary<ComplexNumber, int> getFactorization(ComplexNumber x)
        {
            ComplexNumber xGaussian = new ComplexNumber(x.getRealPart().getNumerator() *
                x.getImaginaryPart().getDenominator(), x.getImaginaryPart().getNumerator() *
                x.getRealPart().getDenominator());
            Dictionary<int, int> denominatorCancellerFactors =
                getFactorization(x.getRealPart().getDenominator() * 
                x.getImaginaryPart().getDenominator());
            Dictionary<ComplexNumber, int> factors = new Dictionary<ComplexNumber, int>();
            foreach (int factor in denominatorCancellerFactors.Keys)
                factors.Add(new ComplexNumber(new Fraction(1, factor), new Fraction(0, 1)),
                    denominatorCancellerFactors[factor]);
            int sumOfRealAndImaginary = 2;
            while (true)
            {
                Console.WriteLine(sumOfRealAndImaginary);//test
                int realPart = sumOfRealAndImaginary / 2;
                int imaginaryPart = sumOfRealAndImaginary - realPart;
                for (int i = 0; realPart - i >= 0;) 
                {
                    if ((realPart - i) * (realPart - i) +
                        (imaginaryPart + i) * (imaginaryPart + i) >
                        xGaussian.getRealPart().getNumerator() *
                        xGaussian.getRealPart().getNumerator() +
                        xGaussian.getImaginaryPart().getNumerator() *
                        xGaussian.getImaginaryPart().getNumerator())
                    {
                        if (factors.ContainsKey(xGaussian))
                            factors[xGaussian] += 1;
                        else
                            factors.Add(xGaussian, 1);
                        return factors;
                    }
                    ComplexNumber factor =
                        new ComplexNumber(realPart - i, imaginaryPart + i);
                    ComplexNumber quotient = (ComplexNumber)(xGaussian / factor);
                    quotient.getRealPart().reduce();
                    quotient.getImaginaryPart().reduce();
                    if (quotient.getRealPart().getDenominator() == 1 &&
                        quotient.getImaginaryPart().getDenominator() == 1) 
                    {
                        if (factors.ContainsKey(factor))
                            factors[factor] += 1;
                        else
                            factors.Add(factor, 1);
                        xGaussian = (ComplexNumber)(xGaussian / factor);
                        continue;
                    }
                    factor = new ComplexNumber(realPart - i, -imaginaryPart - i);
                    quotient = (ComplexNumber)(xGaussian / factor);
                    quotient.getRealPart().reduce();
                    quotient.getImaginaryPart().reduce();
                    if (quotient.getRealPart().getDenominator() == 1 &&
                        quotient.getImaginaryPart().getDenominator() == 1)
                    {
                        if (factors.ContainsKey(factor))
                            factors[factor] += 1;
                        else
                            factors.Add(factor, 1);
                        xGaussian = (ComplexNumber)(xGaussian / factor);
                        continue;
                    }
                    ++i;
                }
                ++sumOfRealAndImaginary;
            }
        }


The ComplexNumber type is designed to behave the way complex numbers do in math (though limited to real and imaginary parts that are rational) so I'll type the value of a ComplexNumber object the same way as I would a complex number in math.

Note how sumOfRealAndImaginary is initialized to 2, and the only place it's changed is where it's incremented at the bottom of the while loop. In debug mode, when I pass getFactorization() the value 3+4i and step through the code at a sufficiently moderate speed, it returns at a time when sumOfRealAndImaginary = 3. This is what I expect. However, when I run the program, also with 3+4i as the function argument, it doesn't return until sumOfRealAndImaginary = 126, as can be seen from the WriteLine() call at the top of the while loop. In particular, the first if statement in the while loop, whose code block includes the function's return statement, takes many more passes to evaluate to true when running than when debugging. It should evaluate to true not only for the expected sumOfRealAndImaginary value of 3, but also for every value larger than that. Also, when I step through the code sufficiently quickly in debug mode, that if statement keeps evaluating to false until I slow down, at which point it evaluates to true on the next pass. So it would seem that whether the if statement evaluates correctly depends on how quickly the code is executed. What could cause this behavior?

Unfortunately, any compilable test program I can figure out how to make for getFactorization() would include the majority of the original program, so...here is a text file of the code for the whole thing, for if the problem depends on the other parts of my code that getFactorization() refers to, and in case people need to test it themselves firsthand. A command line input of (3+4i)^2 will cause getFactorization() to be called with an argument of 3+4i.

modified 5-Oct-17 12:53pm.

AnswerRe: Heisenbug in program without async, etc. Pin
harold aptroot5-Oct-17 10:42
harold aptroot5-Oct-17 10:42 
GeneralRe: Heisenbug in program without async, etc. Pin
Alexander Kindel5-Oct-17 11:28
Alexander Kindel5-Oct-17 11:28 
GeneralRe: Heisenbug in program without async, etc. Pin
harold aptroot5-Oct-17 12:32
harold aptroot5-Oct-17 12:32 
GeneralRe: Heisenbug in program without async, etc. Pin
Alexander Kindel5-Oct-17 12:50
Alexander Kindel5-Oct-17 12:50 
GeneralRe: Heisenbug in program without async, etc. Pin
harold aptroot5-Oct-17 23:17
harold aptroot5-Oct-17 23:17 
GeneralRe: Heisenbug in program without async, etc. Pin
Alexander Kindel5-Oct-17 22:22
Alexander Kindel5-Oct-17 22:22 
QuestionHelp with a Windows Form Application parser Pin
Member 134478105-Oct-17 4:59
Member 134478105-Oct-17 4:59 
QuestionRe: Help with a Windows Form Application parser Pin
Eddy Vluggen7-Oct-17 3:13
professionalEddy Vluggen7-Oct-17 3:13 
AnswerRe: Help with a Windows Form Application parser Pin
Member 134478109-Oct-17 6:43
Member 134478109-Oct-17 6:43 
GeneralRe: Help with a Windows Form Application parser Pin
Eddy Vluggen9-Oct-17 7:31
professionalEddy Vluggen9-Oct-17 7:31 
Questionhow to show sql server row wise data in column wise in dynamic table Pin
Member 127429835-Oct-17 1:08
Member 127429835-Oct-17 1:08 
AnswerRe: how to show sql server row wise data in column wise in dynamic table Pin
Richard MacCutchan5-Oct-17 1:14
mveRichard MacCutchan5-Oct-17 1:14 
QuestionAjax based filled DropDown Reset on Page Postback in Asp.Net Pin
khaqanbaloch4-Oct-17 18:33
professionalkhaqanbaloch4-Oct-17 18:33 
AnswerRe: Ajax based filled DropDown Reset on Page Postback in Asp.Net Pin
Richard MacCutchan4-Oct-17 22:37
mveRichard MacCutchan4-Oct-17 22:37 
QuestionJson login with authentification and getting the result in C# Pin
Member 113544104-Oct-17 17:15
Member 113544104-Oct-17 17:15 
AnswerRe: Json login with authentification and getting the result in C# Pin
Karthik_Mahalingam4-Oct-17 17:55
professionalKarthik_Mahalingam4-Oct-17 17:55 
GeneralRe: Json login with authentification and getting the result in C# Pin
Mycroft Holmes4-Oct-17 21:28
professionalMycroft Holmes4-Oct-17 21:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.