point499 point500 ; 返回bruteForceSum; } // DoBruteForceCompute Take some standard code such as shown below. It simply loops to add up aseries of terms and it produces the correct result.// sum numbers with a looppublic int DoSumLooping(int iterations){int result = 0;for(int i = 1;i <=iterations;i++){result += i;}return result;}Now translate this into a specific solution that doesn''t use looping (anduse the same value for the number of iterations the loop performs). Thiscode returns an incorrect result. The method consists entirely of a verystraightforward code statement, but in this case .NET adds the numbersincorrectly.public double ComputeSum( ){// Brute force sum method// For iterations == 10000double sum = 0+ 1+ 2+ 3+ 4+ 5+ 6+ 7+ 8+ 9+ 10+ 11+ 12+ 13+ 14+ 15+ ...+ 9997+ 9998+ 9999+ 10000;return sum;}The above method returns an incorrect result with any number of terms aboveabout 200. It will correctly add 1 + 2 + ... + 200, but it will NOTcorrectly add 1 + 2 + ... + 1000.I have just run across this, and I have not yet researched the possiblereasons for this behavior. It may be a known issue related to either stacksize or the length of a code line, but to my knowledge it hasn''t beendiscussed in any of the "popular" literature on C# and .NET. I need to writecode like this, so if anyone has already encountered this issue, pleaseadvise me.Here''s another example that also creates problems, but of a somewhatdifferent nature. Take the following code and translate it into a specific,non-looping method and try to execute it using reflection. It fails.public double LoopToCompute(){double sumOfProducts = 0;double grandTotal = 0;for (int i = 0; i < maxRows; ++i){for (int j = 0; j < maxCols; ++j){sumOfProducts += coeff[j] * table[i][j];}a_point[i] = sumOfProducts;grandTotal += sumOfProducts;sumOfProducts = 0;}return grandTotal;}//LoopToComputeThe above code works -- but it''s equivalent code with loops unrolled (shownbelow) doesn''t work unless the maxRows is set very small. For small values,the 2 methods (above and below) produce identical results. There is nothing"wrong" with the code in that sense. It''s similar to the above situation. Ifthe "size" of the code statement or the number of code statements is toolarge, .NET fails. In this case (using reflection) it doesn''t return theincorrect result, as the first example did. In this case, reflection callsit an invalid program and refuses to run it (but only when the value ofmaxRows is above about 250). The reason for this is probablystraightforward. However, I have the need to make statements like this forperformance reasons so I need a work-around. Any suggestions areappreciated! All comments are appreciated.public double DoBruteForceCompute(){double bruteForceSum = 0;point1=coeff1*table[0][0] +coeff2*table[0][1] +coeff3*table[0][2]+coeff4*table[0][3] +coeff5*table[0][4] +coeff6*table[0][5]+coeff7*table[0][6] +coeff8*table[0][7] +coeff9*table[0][8]+coeff10*table[0][9] +coeff11*table[0][10] +coeff12*table[0][11]+coeff13*table[0][12] +coeff14*table[0][13] +coeff15*table[0][14]+coeff16*table[0][15] +coeff17*table[0][16] +coeff18*table[0][17]+coeff19*table[0][18] +coeff20*table[0][19] +coeff21*table[0][20]+coeff22*table[0][21] +coeff23*table[0][22] +coeff24*table[0][23]+coeff25*table[0][24] +coeff26*table[0][25] +coeff27*table[0][26]+coeff28*table[0][27] +coeff29*table[0][28] +coeff30*table[0][29]+coeff31*table[0][30] +coeff32*table[0][31] +coeff33*table[0][32]+coeff34*table[0][33] +coeff35*table[0][34] ;point2=coeff1*table[1][0] +coeff2*table[1][1] +coeff3*table[1][2]+coeff4*table[1][3] +coeff5*table[1][4] +coeff6*table[1][5]+coeff7*table[1][6] +coeff8*table[1][7] +coeff9*table[1][8]+coeff10*table[1][9] +coeff11*table[1][10] +coeff12*table[1][11]+coeff13*table[1][12] +coeff14*table[1][13] +coeff15*table[1][14]+coeff16*table[1][15] +coeff17*table[1][16] +coeff18*table[1][17]+coeff19*table[1][18] +coeff20*table[1][19] +coeff21*table[1][20]+coeff22*table[1][21] +coeff23*table[1][22] +coeff24*table[1][23]+coeff25*table[1][24] +coeff26*table[1][25] +coeff27*table[1][26]+coeff28*table[1][27] +coeff29*table[1][28] +coeff30*table[1][29]+coeff31*table[1][30] +coeff32*table[1][31] +coeff33*table[1][32]+coeff34*table[1][33] +coeff35*table[1][34] ;[...]point500=coeff1*table[499][0] +coeff2*table[499][1] +coeff3*table[499][2]+coeff4*table[499][3] +coeff5*table[499][4] +coeff6*table[499][5]+coeff7*table[499][6] +coeff8*table[499][7] +coeff9*table[499][8]+coeff10*table[499][9] +coeff11*table[499][10] +coeff12*table[499][11]+coeff13*table[499][12] +coeff14*table[499][13] +coeff15*table[499][14]+coeff16*table[499][15] +coeff17*table[499][16] +coeff18*table[499][17]+coeff19*table[499][18] +coeff20*table[499][19] +coeff21*table[499][20]+coeff22*table[499][21] +coeff23*table[499][22] +coeff24*table[499][23]+coeff25*table[499][24] +coeff26*table[499][25] +coeff27*table[499][26]+coeff28*table[499][27] +coeff29*table[499][28] +coeff30*table[499][29]+coeff31*table[499][30] +coeff32*table[499][31] +coeff33*table[499][32]+coeff34*table[499][33] +coeff35*table[499][34] ;bruteForceSum =point1 +point2 + ... +point499 +point500;return bruteForceSum;}//DoBruteForceCompute 解决方案 You use variables of diferent type for the result.result is *int* // sum numbers with a loop public int DoSumLooping(int iterations) { int result = 0; for(int i = 1;i <=iterations;i++) { result += i; } return result; } public double ComputeSum( ) { // Brute force sum method // For iterations == 10000 double sum = 0+ 1+ 2+ 3+ 4+ 5+ 6+ 7+ 8+ 9+ 10+ 11+ 12+ 13+ 14+ 15+.... + 9997+ 9998+ 9999+ 10000; return sum; }result is *double*Can it be de reason for the different results?B\rgds100I just did it going with 200 Hard coded ints, then 390, then 300. Each timeI got the correct results. Are you positive of the input numbers? I knowthat''s a stupid question, but I first did each one by loading them into anInt Array and adding them like that. It worked. Then I actually usedstraight hard coding, and I used the number 10 each time and added 100000 tothe last one for good measure (just to make sure the value I had wassimilarly high). No problems whatsoever. Inasmuch as you can load thosevalues in an array and iterate it successfully, couldn''t you just load thearray and use an iterative approach? I understand that''s what you areasking about...why should you have to do this, but it''s probably easier thanverifying the math each time when you know it will work. If I can findanything, I''ll repost.HTH,Bill"Mountain Bikn'' Guy" <[email protected]> wrote in messagenews:NZUjb.795942YN5.793228@sccrnsc01... Take some standard code such as shown below. It simply loops to add up a series of terms and it produces the correct result. // sum numbers with a loop public int DoSumLooping(int iterations) { int result = 0; for(int i = 1;i <=iterations;i++) { result += i; } return result; } Now translate this into a specific solution that doesn''t use looping (and use the same value for the number of iterations the loop performs). This code returns an incorrect result. The method consists entirely of a very straightforward code statement, but in this case .NET adds the numbers incorrectly. public double ComputeSum( ) { // Brute force sum method // For iterations == 10000 double sum = 0+ 1+ 2+ 3+ 4+ 5+ 6+ 7+ 8+ 9+ 10+ 11+ 12+ 13+ 14+ 15+.... + 9997+ 9998+ 9999+ 10000; return sum; } The above method returns an incorrect result with any number of termsabove about 200. It will correctly add 1 + 2 + ... + 200, but it will NOT correctly add 1 + 2 + ... + 1000. I have just run across this, and I have not yet researched the possible reasons for this behavior. It may be a known issue related to either stack size or the length of a code line, but to my knowledge it hasn''t been discussed in any of the "popular" literature on C# and .NET. I need towrite code like this, so if anyone has already encountered this issue, please advise me. Here''s another example that also creates problems, but of a somewhat different nature. Take the following code and translate it into aspecific, non-looping method and try to execute it using reflection. It fails. public double LoopToCompute() { double sumOfProducts = 0; double grandTotal = 0; for (int i = 0; i < maxRows; ++i) { for (int j = 0; j < maxCols; ++j) { sumOfProducts += coeff[j] * table[i][j]; } a_point[i] = sumOfProducts; grandTotal += sumOfProducts; sumOfProducts = 0; } return grandTotal; }//LoopToCompute The above code works -- but it''s equivalent code with loops unrolled(shown below) doesn''t work unless the maxRows is set very small. For smallvalues, the 2 methods (above and below) produce identical results. There isnothing "wrong" with the code in that sense. It''s similar to the above situation.If the "size" of the code statement or the number of code statements is too large, .NET fails. In this case (using reflection) it doesn''t return the incorrect result, as the first example did. In this case, reflection calls it an invalid program and refuses to run it (but only when the value of maxRows is above about 250). The reason for this is probably straightforward. However, I have the need to make statements like this for performance reasons so I need a work-around. Any suggestions are appreciated! All comments are appreciated. public double DoBruteForceCompute() { double bruteForceSum = 0; point1=coeff1*table[0][0] +coeff2*table[0][1] +coeff3*table[0][2] +coeff4*table[0][3] +coeff5*table[0][4] +coeff6*table[0][5] +coeff7*table[0][6] +coeff8*table[0][7] +coeff9*table[0][8] +coeff10*table[0][9] +coeff11*table[0][10] +coeff12*table[0][11] +coeff13*table[0][12] +coeff14*table[0][13] +coeff15*table[0][14] +coeff16*table[0][15] +coeff17*table[0][16] +coeff18*table[0][17] +coeff19*table[0][18] +coeff20*table[0][19] +coeff21*table[0][20] +coeff22*table[0][21] +coeff23*table[0][22] +coeff24*table[0][23] +coeff25*table[0][24] +coeff26*table[0][25] +coeff27*table[0][26] +coeff28*table[0][27] +coeff29*table[0][28] +coeff30*table[0][29] +coeff31*table[0][30] +coeff32*table[0][31] +coeff33*table[0][32] +coeff34*table[0][33] +coeff35*table[0][34] ; point2=coeff1*table[1][0] +coeff2*table[1][1] +coeff3*table[1][2] +coeff4*table[1][3] +coeff5*table[1][4] +coeff6*table[1][5] +coeff7*table[1][6] +coeff8*table[1][7] +coeff9*table[1][8] +coeff10*table[1][9] +coeff11*table[1][10] +coeff12*table[1][11] +coeff13*table[1][12] +coeff14*table[1][13] +coeff15*table[1][14] +coeff16*table[1][15] +coeff17*table[1][16] +coeff18*table[1][17] +coeff19*table[1][18] +coeff20*table[1][19] +coeff21*table[1][20] +coeff22*table[1][21] +coeff23*table[1][22] +coeff24*table[1][23] +coeff25*table[1][24] +coeff26*table[1][25] +coeff27*table[1][26] +coeff28*table[1][27] +coeff29*table[1][28] +coeff30*table[1][29] +coeff31*table[1][30] +coeff32*table[1][31] +coeff33*table[1][32] +coeff34*table[1][33] +coeff35*table[1][34] ; [...] point500=coeff1*table[499][0] +coeff2*table[499][1] +coeff3*table[499][2] +coeff4*table[499][3] +coeff5*table[499][4] +coeff6*table[499][5] +coeff7*table[499][6] +coeff8*table[499][7] +coeff9*table[499][8] +coeff10*table[499][9] +coeff11*table[499][10] +coeff12*table[499][11] +coeff13*table[499][12] +coeff14*table[499][13] +coeff15*table[499][14] +coeff16*table[499][15] +coeff17*table[499][16] +coeff18*table[499][17] +coeff19*table[499][18] +coeff20*table[499][19] +coeff21*table[499][20] +coeff22*table[499][21] +coeff23*table[499][22] +coeff24*table[499][23] +coeff25*table[499][24] +coeff26*table[499][25] +coeff27*table[499][26] +coeff28*table[499][27] +coeff29*table[499][28] +coeff30*table[499][29] +coeff31*table[499][30] +coeff32*table[499][31] +coeff33*table[499][32] +coeff34*table[499][33] +coeff35*table[499][34] ; bruteForceSum = point1 + point2 + ... + point499 + point500 ; return bruteForceSum; }//DoBruteForceCompute 这篇关于展开循环:为什么.NET不能添加1 + 2 + 3 ...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-03 06:54