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

C#

 
GeneralRe: Serializing \r\n read issue Pin
Richard Deeming6-Oct-17 7:46
mveRichard Deeming6-Oct-17 7:46 
GeneralRe: Serializing \r\n read issue Pin
C. David Johnson6-Oct-17 8:05
C. David Johnson6-Oct-17 8:05 
GeneralRe: Serializing \r\n read issue Pin
Richard Deeming6-Oct-17 8:07
mveRichard Deeming6-Oct-17 8:07 
GeneralRe: Serializing \r\n read issue Pin
jschell11-Oct-17 5:53
jschell11-Oct-17 5:53 
AnswerRe: Serializing \r\n read issue Pin
Gerry Schmitz7-Oct-17 6:02
mveGerry Schmitz7-Oct-17 6:02 
QuestionDecimal compare Pin
VK196-Oct-17 4:16
VK196-Oct-17 4:16 
AnswerRe: Decimal compare Pin
Richard Deeming6-Oct-17 4:49
mveRichard Deeming6-Oct-17 4:49 
SuggestionRe: Decimal compare Pin
Richard MacCutchan6-Oct-17 4:53
mveRichard MacCutchan6-Oct-17 4:53 
GeneralRe: Decimal compare Pin
Mycroft Holmes7-Oct-17 14:38
professionalMycroft Holmes7-Oct-17 14:38 
AnswerRe: Decimal compare Pin
Luc Pattyn6-Oct-17 4:53
sitebuilderLuc Pattyn6-Oct-17 4:53 
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 

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.