Math and incrementing. (Examples)

Recently Chris Eargle (Telerik bad-mojo) kicked my butt with some very simplistic (though unexpected results) from incrementing within multi part math problems.

This stuff is pretty basic, but you never think to do it until it comes up in as a
user group question.    Essentially — or ++ before the variable in/decrements then uses.  However if you do variable and then — or ++ then it will do the math then in/decrements

var a = 2.0;
 var b = 4.0;
 var c = 3.0;
 var d = 5.0;
 var total1 = 0.0;
 var total2 = 0.0;
 Console.WriteLine("----- TOP ---");
 Console.WriteLine(String.Format("a:{0} b:{1} c:{2} d:{3}", a, b, c, d));
 Console.WriteLine("(a-- + b++);");
 Console.WriteLine((a-- + b++));
 Console.WriteLine(String.Format("a:{0} b:{1} c:{2} d:{3}", a, b, c, d));
 Console.WriteLine("(++c - ++d);");
 Console.WriteLine((++c - ++d));
 Console.WriteLine(String.Format("a:{0} b:{1} c:{2} d:{3}", a, b, c, d));
 Console.WriteLine("a--");
 Console.WriteLine(a--);
 Console.WriteLine(String.Format("a:{0} b:{1} c:{2} d:{3}", a, b, c, d));
 Console.WriteLine("--a");
 Console.WriteLine(--a);
 Console.WriteLine(String.Format("a:{0} b:{1} c:{2} d:{3}", a, b, c, d));
 Console.WriteLine("--a * a++");
 Console.WriteLine(--a * a++);
 Console.WriteLine(String.Format("a:{0} b:{1} c:{2} d:{3}", a, b, c, d));
 Console.WriteLine("++c - ++d * a++");
 Console.WriteLine(++c - ++d * a++);
 Console.WriteLine(String.Format("a:{0} b:{1} c:{2} d:{3}", a, b, c, d));
Console.WriteLine("FirstTry--");
 Console.WriteLine(String.Format("a:{0} b:{1} c:{2} d:{3}", a, b, c, d));
 Console.WriteLine("(++c * --d) / ++b - (++a * d++) - a-- / b");
 Console.WriteLine((++c * --d) / ++b - (++a * d++) - a-- / b);
 Console.WriteLine(String.Format("a:{0} b:{1} c:{2} d:{3}", a, b, c, d));
Console.WriteLine("++c");
 Console.WriteLine(++c);
 //Take each part in order of operation.
 Console.WriteLine("(++c * --d)");
 Console.WriteLine((++c * --d));
 Console.WriteLine(String.Format("a:{0} b:{1} c:{2} d:{3}", a, b, c, d));
 Console.WriteLine("(++a * d++) ");
 Console.WriteLine((++a * d++) );
 Console.WriteLine(String.Format("a:{0} b:{1} c:{2} d:{3}", a, b, c, d));
 Console.WriteLine("a-- / b");
 Console.WriteLine(a-- / b);
 Console.WriteLine(String.Format("a:{0} b:{1} c:{2} d:{3}", a, b, c, d));
 Console.WriteLine("(++c * --d) / ++b");
 Console.WriteLine((++c * --d) / ++b );
 Console.WriteLine(String.Format("a:{0} b:{1} c:{2} d:{3}", a, b, c, d));
 Console.WriteLine("(++a * d++) - a-- / b");
 Console.WriteLine((++a * d++) - a-- / b);
 Console.WriteLine(String.Format("a:{0} b:{1} c:{2} d:{3}", a, b, c, d));
 Console.WriteLine("SecondTry--");
 Console.WriteLine(String.Format("a:{0} b:{1} c:{2} d:{3}", a, b, c, d));
 Console.WriteLine("(++c * --d) / ++b - (++a * d++) - a-- / b");
 Console.WriteLine((++c * --d) / ++b - (++a * d++) - a-- / b);
Console.ReadKey();
RESULTS:
----- TOP ---
 a:2 b:4 c:3 d:5
 (a-- + b++);
 6
 a:1 b:5 c:3 d:5
 (++c - ++d);
 -2
 a:1 b:5 c:4 d:6
 a--
 1
 a:0 b:5 c:4 d:6
 --a
 -1
 a:-1 b:5 c:4 d:6
 --a * a++
-- Here it is most obvious.  Starts at -1 Decrements to -2 
then multiplies -2 * -2 to get 4, then re-increments 
to bring a back to -1  
 4
 a:-1 b:5 c:4 d:6
 ++c - ++d * a++
 12
 a:0 b:5 c:5 d:7
 FirstTry--
 a:0 b:5 c:5 d:7
 (++c * --d) / ++b - (++a * d++) - a-- / b
 -0.166666666666667
 a:0 b:6 c:6 d:7
 ++c
 7
 (++c * --d)
 48
 a:0 b:6 c:8 d:6
 (++a * d++)
 6
 a:1 b:6 c:8 d:7
 a-- / b
 0.166666666666667
 a:0 b:6 c:8 d:7
 (++c * --d) / ++b
 7.71428571428571
 a:0 b:7 c:9 d:6
 (++a * d++) - a-- / b
 5.85714285714286
 a:0 b:7 c:9 d:7
 SecondTry--
 a:0 b:7 c:9 d:7
 (++c * --d) / ++b - (++a * d++) - a-- / b
 1.375

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: