View Single Post
Old 22nd September 2019, 17:29   #10  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,980
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"
Code:
14/2*2 = 14
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
__________________
I sometimes post sober.
StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace

"Some infinities are bigger than other infinities", but how many of them are infinitely bigger ???

Last edited by StainlessS; 23rd September 2019 at 15:28.
StainlessS is offline   Reply With Quote