Click here to Skip to main content
15,903,175 members
Home / Discussions / C#
   

C#

 
GeneralRe: remoting channel woes Pin
qek27-Oct-03 11:46
qek27-Oct-03 11:46 
Generalcustom attribute question Pin
sharkfish17-Oct-03 9:32
sharkfish17-Oct-03 9:32 
GeneralRe: custom attribute question Pin
Alex Korchemniy17-Oct-03 10:49
Alex Korchemniy17-Oct-03 10:49 
GeneralRe: custom attribute question Pin
sharkfish17-Oct-03 11:31
sharkfish17-Oct-03 11:31 
GeneralDataGrid and ArrayList Pin
Wjousts17-Oct-03 8:26
Wjousts17-Oct-03 8:26 
GeneralRe: DataGrid and ArrayList Pin
Daniel M. Edwards17-Oct-03 16:59
Daniel M. Edwards17-Oct-03 16:59 
GeneralRe: DataGrid and ArrayList Pin
Heath Stewart18-Oct-03 10:49
protectorHeath Stewart18-Oct-03 10:49 
GeneralUnrolling a loop: why can't .NET add 1 + 2 + 3 + ... Pin
MtnBiknGuy17-Oct-03 6:45
MtnBiknGuy17-Oct-03 6:45 
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 terms above
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 to write
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 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;
}//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 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. 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
GeneralRe: Unrolling a loop: why can't .NET add 1 + 2 + 3 + ... Pin
Daniel Turini17-Oct-03 7:08
Daniel Turini17-Oct-03 7:08 
GeneralRe: Unrolling a loop: why can't .NET add 1 + 2 + 3 + ... Pin
leppie17-Oct-03 9:33
leppie17-Oct-03 9:33 
GeneralRe: Unrolling a loop: why can't .NET add 1 + 2 + 3 + ... Pin
MtnBiknGuy17-Oct-03 16:18
MtnBiknGuy17-Oct-03 16:18 
GeneralRe: Unrolling a loop: why can't .NET add 1 + 2 + 3 + ... Pin
Anonymous17-Oct-03 18:24
Anonymous17-Oct-03 18:24 
GeneralRe: Unrolling a loop: why can't .NET add 1 + 2 + 3 + ... Pin
MtnBiknGuy18-Oct-03 3:55
MtnBiknGuy18-Oct-03 3:55 
GeneralRe: Unrolling a loop: why can't .NET add 1 + 2 + 3 + ... Pin
MtnBiknGuy20-Oct-03 14:15
MtnBiknGuy20-Oct-03 14:15 
GeneralRe: Unrolling a loop: why can't .NET add 1 + 2 + 3 + ... Pin
leppie21-Oct-03 8:48
leppie21-Oct-03 8:48 
GeneralRe: Unrolling a loop: why can't .NET add 1 + 2 + 3 + ... Pin
MtnBiknGuy21-Oct-03 10:08
MtnBiknGuy21-Oct-03 10:08 
GeneralRe: Unrolling a loop: why can't .NET add 1 + 2 + 3 + ... Pin
Andy Davey17-Oct-03 10:55
Andy Davey17-Oct-03 10:55 
GeneralRe: Unrolling a loop: why can't .NET add 1 + 2 + 3 + ... Pin
MtnBiknGuy17-Oct-03 16:02
MtnBiknGuy17-Oct-03 16:02 
GeneralXML Complex Types Pin
pahluwalia17-Oct-03 6:27
pahluwalia17-Oct-03 6:27 
Generaladd &quot;user&quot; to &quot;COM+ security role&quot; programmatically Pin
Norman Fung17-Oct-03 6:14
Norman Fung17-Oct-03 6:14 
GeneralURGENT, Help me please !!!!!!!! Pin
god4k17-Oct-03 6:02
god4k17-Oct-03 6:02 
Generallistview Pin
jphuphilly17-Oct-03 6:00
jphuphilly17-Oct-03 6:00 
GeneralRe: listview Pin
Corinna John20-Oct-03 1:52
Corinna John20-Oct-03 1:52 
Questionwhy can't I get through the &quot;lock (this)&quot;? Pin
yyf17-Oct-03 5:18
yyf17-Oct-03 5:18 
Generalthe components's comment Pin
wangier16-Oct-03 22:45
wangier16-Oct-03 22:45 

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.