Pages

Tuesday, March 31, 2009

Modulo a negative number

I'm quite surprised to found that MOD with -ve dividend number is NOT THE SAME as MOD with +ve dividend number.

What is modulo (MOD)?
It's a operation/function which returns the remainder of a division by two integers. It's like division, except it returns the remainder. E.g. 9/4=2.25, 9 MOD 4 = 1 (since 2x4+1=9).

For +ve dividend number, x MOD n means = x - (floor(x/n) * n)
But for -ve dividend number, x MOD n means =
  1. x + ( floor(-x/n)+1 ) * n, or
  2. -(-x MOD n) + n
Literally, the solution is like this : do as normal MOD, but add the remainder (negative number) with the divisor. E.g. -8 MOD 3, frankly return -2, but then add -2 with 3 = 1. Also in the above formulae, floor() is a function which return the answer rounded up towards bottom integer value e.g. 8.4 = 8, 11.9 = 11 etc. (always take the low integer).

Example:
x = 11, n = 3

11 MOD 3, use x - (floor(x/n) * n);
= 11 - ( floor(11/3) * 3)
= 11 - (3 * 3)
= 2

but if x = -11, n = 3

-11 MOD 3, use x + ( floor(-x/n)+1 ) * n;
= -11 + ( floor(11/3) + 1) * 3
= -11 + (3 + 1) * 3
= -11 + 12
= 1


or

-11 MOD 3, use -(-x MOD n) + n
= -(-(-11) MOD 3) + 3
= -(11 MOD 3) + 3
= -2 + 3
= 1

but, what if -11 MOD -3?
Hmmm...

Links:
http://en.wikipedia.org/wiki/Modulo_operation


p/s
: if u still wondering what is 'dividend' : if a/b = c, a is dividend, b is divisor and c is quotient.

Wednesday, February 04, 2009

RA / Programmer wanted!

Great news.

For those who is very (yes, VERY) interested in Bioinformatics and research, and interested in:
  • pursuing Master or PhD, or
  • doing programming, or
  • being a research assistant, or
  • gaining experience in research, then

You may qualified for this job!
RA is wanted.

Pls contact Dr. Mohd Firdaus Raih (firdaus [at] mfrlab.org) by forwarding your CV. You may visit the research group website. More vacancies

p/s: I used to work there (before i join UPM), and gained a great experience. See my name in the publication section!

Tuesday, January 27, 2009

A blunder ... can compiler help?

Yesterday, as i was writing another code, i made this simple mistake, illustrated by the following example (example only, don't do this!):

int foo(int i)
{
int j = 2;
if(i > 0)
return i; /* here should be j*/
else
return 0;
}

ok, this is not a good example (it is rubbish), yet clarify my point. It is not simply a typo (which can possibly be another blunder, duh!). I wrote 'return i' instead of 'return j' because i thought it should be 'i'. This is called semantical error. It is not syntactical error, nor grammatical error. It is simply a blunder, and worse, it can't be detected by a compiler.

Thursday, January 22, 2009

Is it only about logic?

What programming is all about? I mean the philosophy beneath. Is it only about logical thinking, or pure imagination (creative) or something that combined both? Hmmm... i'm still curious ...

However, i believe that both thinking are involved (other than the fact both are the functions of our brain). Since at certain levels, we have to use our abstract, creative imagination as to think about a any possible solutions. Whereas during the level where we are going to form the solution, we must use our logical thinking.

... ?