wimpie3 Well-Known Member Licensed User Longtime User Feb 17, 2022 #1 Is there a way to simplify this code? Code: If percentage < 0 Then percentage=0 If percentage > 1 Then percentage=1 It's a combination of Min and Max but I'm not sure such a function exists?
Is there a way to simplify this code? Code: If percentage < 0 Then percentage=0 If percentage > 1 Then percentage=1 It's a combination of Min and Max but I'm not sure such a function exists?
agraham Expert Licensed User Longtime User Feb 17, 2022 #2 Are those percentage values integers or doubles? It matters as you have a 'dead band' from 0 to 0.999999.... in your comparison. ? Upvote 0
Are those percentage values integers or doubles? It matters as you have a 'dead band' from 0 to 0.999999.... in your comparison. ?
klaus Expert Licensed User Longtime User Feb 17, 2022 #3 Maybe this: B4X: percentage = Min(Max(percentage, 0), 1) Upvote 1
M Mahares Expert Licensed User Longtime User Feb 17, 2022 #4 How about another variant: Code: Log(IIf( percentage < 0, 0, IIf(percentage > 1, 1, percentage))) Upvote 0
agraham Expert Licensed User Longtime User Feb 17, 2022 #5 @klaus suggestion is correct only for integer values. @Mahares is the same as the original post for both integer and double values. Upvote 0
@klaus suggestion is correct only for integer values. @Mahares is the same as the original post for both integer and double values.
S Spavlyuk Active Member Licensed User Longtime User Feb 17, 2022 #6 agraham said: @klaus suggestion is correct only for integer values. Click to expand... What do you mean? B4X: Dim P As Double = 0.99 Log(Min(Max(P, 0), 1)) ' Logs 0.99 Upvote 0
agraham said: @klaus suggestion is correct only for integer values. Click to expand... What do you mean? B4X: Dim P As Double = 0.99 Log(Min(Max(P, 0), 1)) ' Logs 0.99
klaus Expert Licensed User Longtime User Feb 17, 2022 #7 agraham said: @klaus suggestion is correct only for integer values. Click to expand... Sorry, but this is wrong. Min and Max work with Doubles ! I tested it before posting. From the help: Upvote 0
agraham said: @klaus suggestion is correct only for integer values. Click to expand... Sorry, but this is wrong. Min and Max work with Doubles ! I tested it before posting. From the help:
agraham Expert Licensed User Longtime User Feb 17, 2022 #8 Perhaps I misunderstood the original requirement. I assumed that the actual requirement was for percentage to end up with a value of either 0 or 1. The original code in the first post would do that for integer values but not doubles. It would leave it unchanged for the values from 0 to 0.99999.... That is why I asked for clarification in post #2. Upvote 0
Perhaps I misunderstood the original requirement. I assumed that the actual requirement was for percentage to end up with a value of either 0 or 1. The original code in the first post would do that for integer values but not doubles. It would leave it unchanged for the values from 0 to 0.99999.... That is why I asked for clarification in post #2.