Thread title:- "How to convert any integer to mod2"
So, forget about floats, not needed.
Quote:
Example 1 (odd numbers)
13
13/2 = 6.5
Truncate = 6.0
Multiply by 2 = 12.0000
|
Code:
13
13/2 = 6 # int/int = int (truncated int, fractional part discarded)
Multiply by int 2 = 12
so 13/2*2=12
Quote:
Example 1 (even numbers)
14
14/2 = 7.0
Truncate = 7.0
Multiply by 14.0 # Presume "Multiply by 2 = 14.0"
|
Quote:
Add one in second step if you want to round up.
|
(value+factor-1)/factor*factor # General round up where factor is variable
where factor=2 (modulo 2)
Round UP (13+2-1)/2*2=14 OR (13+1)/2*2=14
Round UP (14+2-1)/2*2=14 OR (14+1)/2*2=14
EDIT: NOTE, Prev post Manolito Method for Round UP only works for Modulo 2.
Code:
integer = integer - (integer % factor) # Manolito method Round Down works OK
integer = integer + (factor - integer % factor) # Modified Manolito Round Up [EDIT: This is a BUM STEER, it dont work ]
Above bugged