Welcome to Doom9's Forum, THE inplace to be for everyone interested in DVD conversion. Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules. 
22nd September 2019, 07:38  #1  Link 
Registered User
Join Date: Nov 2003
Posts: 319

How to convert any integer to mod2
I compute an integer in a script and integer can be any value. But I need to modify it so that it is mod2 (evenly divisible by 2). How can we do that? (I've hunted but not found anything.) Will I have to write a custom function just to do that? (And I'm not sure yet whether I want to round up or down, or round closer to zero or fartherone step at a time, ha ha.)
Edit: IF we have to have a function, would this do it? function MakeMod2(int myInt) { bitRshift(myInt,1) bitLshift(myInt,1) return myInt } Last edited by TCmullet; 22nd September 2019 at 07:56. 
22nd September 2019, 09:31  #2  Link 
Registered User
Join Date: Mar 2011
Posts: 3,972

Is something like this what you need?
http://avisynth.nl/index.php/Internal_functions#Floor Code:
function MakeMod2(int myInt, int "Rounding") { # round to nearest integer = 1 # round down = 2 # round up = 3 # round towards zero = any integer other than 1, 2 or 3 rounding = default(rounding, 1) myFloat = float(myInt) Mod2Out = \ (rounding == 1) ? round(myFloat / 2.0) * 2 : \ (rounding == 2) ? floor(myFloat / 2.0) * 2 : \ (rounding == 3) ? ceil(myFloat / 2.0) * 2 : \ int(myFloat / 2.0) * 2 return Mod2Out } round(myFloat / 4.0) * 4 etc. Or something similar. http://avisynth.nl/index.php/Operators I think Mod (%) works much the same way as FMod, but FMod needs float values. http://avisynth.nl/index.php/Internal_functions#Fmod Code:
function MakeMod2(int myInt, bool "RoundUp") { RoundUp = default(RoundUp, false) Mod2Out = \ (myInt % 2 == 0) ? myInt : \ !RoundUp ? myInt  1 : \ myInt + 1 return Mod2Out } Last edited by hello_hello; 22nd September 2019 at 15:26. 
22nd September 2019, 10:01  #3  Link  
HeartlessS Usurer
Join Date: Dec 2009
Location: Over the rainbow
Posts: 7,121

HH, 'myInt' is not defined as an optional arg to your function, but you use 'Default(myInt, 2)'.
Here differing rounding stuff: https://forum.doom9.org/showthread.p...84#post1814184 Quote:
I seem to have missed out round down, [think it was already discussed, and so I missed it out] value/factor*factor. EDIT: Code:
Function RoundInt(int value, int "factor", int "roundMode") { # https://forum.doom9.org/showthread.php?p=1885480#post1885480 # Round +ve Int Value to a multiple of +ve Int Factor, using rounding mode RoundMode. Result will not go ve. factor = Default(factor,2) # Default round to multiple of 2, even roundMode=Default(roundMode,0) # 0=Down(Default), 1=Nearest, 2=Up, 3=ALWAYS_DOWN, 4=ALWAYS_UP Assert(0 <= value,String(value,"RoundInt: 0 <= value(%.0f) [result undefined for ve value]")) Assert(0 < factor,String(factor,"RoundInt: 0 < factor(%.0f)")) Assert(0 <= RoundMode <= 4,String(RoundMode,"RoundInt: 0 <= RoundMode(%.0f) <= 4 ")) return \ (RoundMode==0) ? value / factor * factor [* Down *] \ : (RoundMode==1) ? (value*2+factor)/(2*factor)*factor [* Nearest : About same as (value+factor/2)/factor*factor but without intermediate result precision loss *] \ : (RoundMode==2) ? (value+factor1)/factor*factor [* Up *] \ : (RoundMode==3) ? (value1)/factor*factor [* ALWAYS_DOWN : Rare Use : Result always less than Value, except where Value=0 *] \ : (value+factor)/factor*factor [* ALWAYS_UP : Rare Use : Result always greater than Value *] } FACTOR = 4 # round to multiples of 4 SSS= "Val Down Near Up ADwn AUp\n" For(i=0,16) { S="" for(roundMode=0,4) { r = RoundInt(i, FACTOR, roundMode) S = S + String(r,"%5.0f") } SSS = SSS + String(i,"%2.0f]") + S + "\n" } BlankClip(Width=320,height=360) Subtitle(SSS,Font="Courier New",lsp=0) # Courier New = MonoSpaced font EDIT: Code:
\ : (RoundMode==1) ? (value*2+factor)/(2*factor)*factor [* Nearest : About same as (value+factor/2)/factor*factor but without intermediate result precision loss *] Where factor is KNOWN even [EDIT: KNOWN power of 2, including 1] (most cases) then could use the simpler method. EDIT: Result will not go ve for any round operation. Value Must be +ve, Factor must be 1 or greater.
__________________
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 10:48. 

22nd September 2019, 15:01  #4  Link  
Registered User
Join Date: Mar 2011
Posts: 3,972

Quote:
Actually I did the same for both functions initially, but for some reason I only removed it from the second one after thinking about it. I've done the same for the first one now. Cheers. Last edited by hello_hello; 22nd September 2019 at 15:06. 

22nd September 2019, 15:07  #5  Link 
HeartlessS Usurer
Join Date: Dec 2009
Location: Over the rainbow
Posts: 7,121

You originally had
Code:
myInt = Default(myInt, 2) EDIT: Changed "between 10:01 and about 10:25 BST" to "after 10:01 BST".
__________________
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; 22nd September 2019 at 15:17. 
22nd September 2019, 16:25  #7  Link 
HeartlessS Usurer
Join Date: Dec 2009
Location: Over the rainbow
Posts: 7,121

Sorry, bit under the weather today, significance of smiley not recognised
__________________
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 ??? 
22nd September 2019, 16:53  #8  Link 
Registered User
Join Date: Feb 2002
Location: California
Posts: 2,186

I must be missing something because this seems too easy. Don't you just:
1. Divide integer by two 2. Truncate (remove decimal) 3. Multiply by two. Example 1 (odd numbers) 13 13/2 = 6.5 Truncate = 6.0 Multiply by 2 = 12.0000 Example 1 (even numbers) 14 14/2 = 7.0 Truncate = 7.0 Multiply by 2 = 14.0000 Add one in second step if you want to round up. Last edited by johnmeyer; 23rd September 2019 at 21:04. Reason: typo 
22nd September 2019, 17:29  #10  Link  
HeartlessS Usurer
Join Date: Dec 2009
Location: Over the rainbow
Posts: 7,121

Thread title: "How to convert any integer to mod2"
So, forget about floats, not needed. Quote:
Code:
13 13/2 = 6 # int/int = int (truncated int, fractional part discarded) Multiply by int 2 = 12 so 13/2*2=12 Quote:
Code:
14/2*2 = 14 Quote:
where factor=2 (modulo 2) Round UP (13+21)/2*2=14 OR (13+1)/2*2=14 Round UP (14+21)/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 ]
__________________
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. 

22nd September 2019, 18:50  #11  Link  
Registered User
Join Date: Sep 2003
Location: Berlin, Germany
Posts: 2,613

Quote:
Quote:
Last edited by manolito; 22nd September 2019 at 19:25. 

22nd September 2019, 21:14  #13  Link 
HeartlessS Usurer
Join Date: Dec 2009
Location: Over the rainbow
Posts: 7,121

MeteorRain,
Note, BitAnd() is a function, with significant lookup overhead (although not so much in avs+ compared to avs std, I think hash table was implemented in avs+), whereas ops below are done directly via the parser, and quite a bit faster [will though make little difference unless in some runtime script]. Code:
\ (RoundMode==0) ? value / factor * factor [* Down *] \ : (RoundMode==1) ? (value*2+factor)/(2*factor)*factor [* Nearest : About same as (value+factor/2)/factor*factor but without intermediate result precision loss *] \ : (RoundMode==2) ? (value+factor1)/factor*factor [* Up *] \ : (RoundMode==3) ? (value1)/factor*factor [* ALWAYS_DOWN : Rare Use <EDIT: Result always less than Value, except where Value=0> *] \ : (value+factor)/factor*factor [* ALWAYS_UP : Rare Use <EDIT: Result always greater than Value> *] EDIT: Also, I suggest that method that works for all values of factor is a better option, with possible exception being above Simpler Nearest when factor is power of 2(1,2,4 etc, likely most often).
__________________
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 10:46. 
23rd September 2019, 15:25  #15  Link 
HeartlessS Usurer
Join Date: Dec 2009
Location: Over the rainbow
Posts: 7,121

Maybe simpler, but NOT better, sorry, was a bum steer, ie it dont work.
Code:
FACTOR=4 SSS= "" For(integer=0,16) { r = integer + (factor  integer % factor) S = String(r,"%5.0f") SSS = SSS + String(integer,"%2.0f]") + S + "\n" } BlankClip(Width=320,height=360) Subtitle(SSS,Font="Courier New",lsp=0) # Courier New = MonoSpaced font Guess I shoulda checked it first before posting. Is same as RoundInt(roundMode = 4), ALWAYS_UP. (should be same as below UP column. EDIT: Yip, tis a lesson that many coders will need learn at some stage (me included), it is 2nd nature to presume that bit operations are fast.
__________________
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:42. 
23rd September 2019, 15:52  #17  Link  
HeartlessS Usurer
Join Date: Dec 2009
Location: Over the rainbow
Posts: 7,121

Quote:
EDIT: When factor is KNOWN 2, then, is (integer + 2  1) / 2 * 2 <===> (integer + 1) / 2 * 2 (integer + 0) / 1 * 1 # Known 1, ie same as integer=integer (integer + 1) / 2 * 2 # Known 2 (integer + 2) / 3 * 3 # Known 3 (integer + 3) / 4 * 4 # Known 4, etc
__________________
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 16:26. 

24th September 2019, 00:30  #19  Link  
Registered User
Join Date: Sep 2003
Location: Berlin, Germany
Posts: 2,613

Quote:
You helped me solving the above issue in 2012 https://forum.doom9.org/showthread.p...77#post1574277 And there were many other occasions when I hit a roadblock in an AVS script that your posts in different threads saved me. So I am the one who has to thank you... 

Thread Tools  Search this Thread 
Display Modes  

