Distributivity of multiplication over modulo 
Thursday, February 23, 2006, 12:00 AM - Programming
Theorem

Given: Integers a, b, c, such that:

  • a >= 0
  • b > 0
  • c > 0

Given: % represents modulo operator

Theorem: (a % b) × c == (a×c) % (b×c)


Proof Part A:

First I will define a new formula for modulo that uses subtraction, multiplication, and the floor() function.

Given: floor(x/y) represents the integer part of the division of x and y.

1. (x % y) Given: x Modulo y
2. [x/y - floor(x/y)] × y Divide x and y, subtract the integer part, then multiply by the divisor to get the remainder
3. x - y × floor(x/y) Distribute multiplication over subtraction

I didn't really prove how to go from step 1 to step 2, but it is pretty easy to reason about.
In any case, here is another page that comes up with the same formula.

Proof Part B:

Here comes the fun proof. I will prove the theorem by simplifying the equation one step at a time.

1. (a % b) × c =?= (a×c) % (b×c) Given: The theorem we are trying to prove
2. (a % b) × c =?= ca % cb Simplify right side and reverse order of multiplication
3. [a - b × floor(a/b)] × c =?= ca - cb × floor[(ca)/(cb)] Replace modulo operator with formula in step 3 of Proof Part A
4. ca - cb × floor(a/b) =?= ca - cb × floor[(ca)/(cb)] Distribute multiplication over subtraction
5. -cb × floor(a/b) =?= -cb × floor[(ca)/(cb)] Subtract c×a from both sides
6. floor(a/b) =?= floor[(ca)/(cb)] Divide both sides with -c×b
7. floor(a/b) == floor(a/b) The c's divide out.


[ add comment ] ( 547 views )   |  permalink  |   ( 2.8 / 179 )
Visual C++ .Net 2003 Standard - How To Optimize 
Friday, October 28, 2005, 12:00 AM - Programming
Problem 1:

Visual C++ .Net 2003 Standard Edition comes with a non-optimizing compiler!
How annoying!!!

Fix to Problem 1:

See this page for instructions on how to get an optimized compiler, and integrate it with the IDE. Microsoft makes the optimizing compiler available to download for free. Now, why would they let you download a free optimizing compiler, but when you pay $100 and actually buy the product, you get a non-optimizing compiler? In the words of a friend of mine, "That's some shrewd marketing. Or something."

Problem 2:

So, everything is hunky-dory now, right? Not exactly, the optimizing options are still grayed out in the GUI, just as the xona.com article mentions. Well, that's not too annoying, since you can just specify /O2 or whatever in the "Additional Options" section.

However, it actually was still very annoying for me because I use cmake (Cross Platform Make) for small projects and it conveniently changes a /O2 into an 'Optimization="2"' in your project file (which is the correct thing to do, if you have a normal IDE that doesn't gray out optimization options and then filter them out before launching cl.exe). Try as I might, I could not get cmake to just put the "/O2" in the additional options. I could put it in there myself manually, but the next time I reload the project, cmake conveniently rebuilds my project file.

I'm not angry with cmake. It's doing exactly the right thing. It's MS fault for giving me a crippled compiler! If they are giving out a free optimizing compiler, why not actually let me use it from the IDE that I paid for!

Fix to Problem 2:

Well, I wouldn't be writing this nice blog if I didn't have a solution, now would I?

There is a function in VCProjectEngine.dll that filters the command-line. Just replace 0x74 with an 0xEB at offset 0x63D7 in the DLL and you're set! (This only applies to VCProjectEngine.dll version 7.10.3077.0. With version 7.10.3274.0 the offset you want is 0x6AB3.)

Best of luck.

[ 1 comment ] ( 2571 views )   |  permalink  |   ( 2.9 / 145 )
NoStepInto (StepOver) with VS.Net 2003 (7.1) 
Thursday, October 27, 2005, 12:00 AM - Programming
Problem:

NoStepInto does not work with VS.Net 2003 on Windows 2000. Sure it is an undocumented feature, but hey, it's quite handy, and would be nice to have.

Longer description:

When you hit F11 in VS.Net to step into a function, sometimes you don't want it to step into certain things, like basic_string constructor, etc.
Visual Studio 6.0 had a way of disabling stepping into certain functions via the autoexp.dat file, but with VS 7.0 and later, you accomplish it by setting certain values in the registry.

Refer to this page to see more about NoStepInto and how it works.

Refer to this page to see the problem with Win2k and the solution Jan Bares came up with.

Cause:

So, after reading that, you can see that because of bug in MS code, a certain variable goes uninitialized, and therefore the GetRegistryRoot function fails, and therefore NoStepInto functionality doesn't work. Now, was this done intentionally, to "encourage" people to upgrade to Windows XP? I don't know.

Fix:

Well, Jan Bares already came up with the solution, which is a hack/patch to the NatDbgDE.dll file, but unless you have access to a disassembler and can figure out how to download the symbol information, it's probably not going to be that easy for you to figure out where the patch goes.

Just nop out the eight bytes starting at offset 0x15103. (Oh, and don't forget to make a backup of your DLL first.)

Hope it works for you too!

[ add comment ]   |  permalink  |   ( 2.9 / 126 )
boost shared_ptr with vs.net 2003 intellisense 
Thursday, August 25, 2005, 12:00 AM - Programming
Problem:
Intellisense doesn't work with boost::shared_ptr. I am using boost version 1.32.

Longer description:
Here is a code sample:

class foo
{
public:
bool func() { return true; };
};

boost::shared_ptr<foo> pfoo;
pfoo.get();
pfoo->func();


No intellisense pops up after the . or the ->. I love shared_ptr, but having no intellisense makes things really annoying.

Cause:
I think it's because VS.Net does not look at the appropriate header files. It probably could do a better job and maybe it does in VS 2005, but then again, perhaps it would slow it down to try to parse through many more files?
As a side note, I tried Visual Assist and it sort of worked, but sometimes it actually popped up the wrong intellisense, whoops! Also, I did notice that it had to parse through a lot of files.

Fix:
At first glance, you would think that adding boost\shared_ptr.hpp to the project (add exiting item) would solve the program. It ALMOST does, but you need to add that file AND boost/detail/shared_ptr_nmt.hpp. At least, that is what I have to do with boost 1.32 to get it to work.

Hope it works for you too!

[ add comment ]   |  permalink  |   ( 3 / 113 )
NUnit for C# 
Tuesday, June 7, 2005, 12:00 AM - Programming
NUnit rocks! 'Nuff said.

[ add comment ]   |  permalink  |   ( 2.9 / 119 )

Back Next