M Marc De Loose Member Licensed User Longtime User Feb 21, 2019 #1 I would like to write: width = width*(pct*(width>height)) what would resolve to: (if width is greater than height) width = width*(pct*1) if not width = width *(pct*0) is width = width*(pct*(width>height)) somehow possible?
I would like to write: width = width*(pct*(width>height)) what would resolve to: (if width is greater than height) width = width*(pct*1) if not width = width *(pct*0) is width = width*(pct*(width>height)) somehow possible?
LucaMs Expert Licensed User Longtime User Feb 21, 2019 #2 No, your code can not work. I would use: Code: Public Sub IIF(c As Boolean, TrueRes As Object, FalseRes As Object) As Object If c Then Return TrueRes Else Return FalseRes End Sub width = width * pct * IIF(width > height, 1, 0) Last edited: Feb 21, 2019 Upvote 0
No, your code can not work. I would use: Code: Public Sub IIF(c As Boolean, TrueRes As Object, FalseRes As Object) As Object If c Then Return TrueRes Else Return FalseRes End Sub width = width * pct * IIF(width > height, 1, 0)
MarkusR Well-Known Member Licensed User Longtime User Feb 21, 2019 #3 or make this boolean a number Code: Sub Test Dim width,height,pct As Float width = 200 height =100 pct = 5 width = width*(pct*B(width>height)) Log(width) End Sub Sub B(truefalse As Boolean) As Float If truefalse Then Return 1.0 Else Return 0.0 End Sub Upvote 0
or make this boolean a number Code: Sub Test Dim width,height,pct As Float width = 200 height =100 pct = 5 width = width*(pct*B(width>height)) Log(width) End Sub Sub B(truefalse As Boolean) As Float If truefalse Then Return 1.0 Else Return 0.0 End Sub
Brandsum Well-Known Member Licensed User Feb 21, 2019 #4 Marc De Loose said: is width = width*(pct*(width>height)) somehow possible? Click to expand... Yup that's quite easy!! Code: width = width * (pct * Min(1,Max(width-height,0)) ) Last edited: Feb 21, 2019 Upvote 0
Marc De Loose said: is width = width*(pct*(width>height)) somehow possible? Click to expand... Yup that's quite easy!! Code: width = width * (pct * Min(1,Max(width-height,0)) )
LucaMs Expert Licensed User Longtime User Feb 21, 2019 #5 Note that IIF is a useful function to be reused; otherwise it was worth using a full "if" block. Upvote 0
E emexes Expert Licensed User Longtime User Feb 21, 2019 #6 Marc De Loose said: I would like to write: width = width*(pct*(width>height)) what would resolve to: (if width is greater than height) width = width*(pct*1) if not width = width *(pct*0) Click to expand... I'd be inclined to just go with: Code: if width > height then width = width * pct else width = 0 end if which I feel wins on simplicity, efficiency and brevity, but if you must have it as one statement (if we ignore the called function...) then LucaMs wins: Code: width = IIF(width > height, width * pct, 0) with Brandsum in a close but cryptic second place, assuming width and height are integers ;-) Last edited: Feb 21, 2019 Upvote 0
Marc De Loose said: I would like to write: width = width*(pct*(width>height)) what would resolve to: (if width is greater than height) width = width*(pct*1) if not width = width *(pct*0) Click to expand... I'd be inclined to just go with: Code: if width > height then width = width * pct else width = 0 end if which I feel wins on simplicity, efficiency and brevity, but if you must have it as one statement (if we ignore the called function...) then LucaMs wins: Code: width = IIF(width > height, width * pct, 0) with Brandsum in a close but cryptic second place, assuming width and height are integers ;-)
Brandsum Well-Known Member Licensed User Feb 21, 2019 #7 LucaMs said: Note that IIF is a useful function to be reused; otherwise it was worth using a full "if" block. Click to expand... That's true. But I just tried to give a minimal solution for what he wanted width = width*(pct*(width>height)) Upvote 0
LucaMs said: Note that IIF is a useful function to be reused; otherwise it was worth using a full "if" block. Click to expand... That's true. But I just tried to give a minimal solution for what he wanted width = width*(pct*(width>height))