if number>=minnuber and number <=maxinumber then exit sub else messagebod.show("number is out of range.number must be between the minimum and the maximum number") end if
check if number is between two numbers inclusive
Page 1 of 114 Replies  86688 Views  Last Post: 24 February 2013  08:21 PM
#1
check if number is between two numbers inclusive
Posted 05 November 2010  06:25 AM
Replies To: check if number is between two numbers inclusive
#2
Re: check if number is between two numbers inclusive
Posted 05 November 2010  06:31 AM
#3
Re: check if number is between two numbers inclusive
Posted 05 November 2010  06:33 AM
#4
Re: check if number is between two numbers inclusive
Posted 05 November 2010  07:15 AM
if number <= minnumber or number >= maxinumber then messagebox.show("number is out of range.number must be between the minimum and the maximum number") exit sub end if
Please copy and paste this code as I made a lot of changes that might be overlooked.
This post has been edited by CharlieMay: 05 November 2010  07:16 AM
#5
Re: check if number is between two numbers inclusive
Posted 05 November 2010  10:18 AM
LowerBound <= Value <= UpperBound
Now you can see there are two sub expressions
1. (LowerBound <= Value) <= UpperBound
2. LowerBound <= (Value <= UpperBound)
If (LowerBound <= Value) AndAlso (Value<=UpperBound) Then ' Is within bounds Else ' Is not within bounds End If {/code] If you just want to see if the value is outside the bounds, enclose both expression inside of a not. [code] If Not ( (LowerBound <= Value) AndAlso (Value<=UpperBound) ) Then ' Not with in bounds Else ' Is within bounds End If
#6
Re: check if number is between two numbers inclusive
Posted 05 November 2010  01:23 PM
Instead I create a Util class in which you can define the operation. Then no matter how you write the implementation, the if statement is always the same. And if you find a better way to do it, you can easily update it later on.
So then the result would be:
If MyUtil.InRange(num, min, max) Then ''do stuff
as one may notice in this class... the exact code you're expecting is like the 3rd function.
Namespace Utils ''' <summary> ''' A port of the LoDMath static member class written in AS3 under the MIT license agreement. ''' ''' A collection of math functions that can be very useful for many things. ''' ''' ''' As per the license agrrement of the lodGameBox license agreement ''' ''' Copyright (c) 2009 Dylan Engelman ''' '''Permission is hereby granted, free of charge, to any person obtaining a copy '''of this software and associated documentation files (the "Software"), to deal '''in the Software without restriction, including without limitation the rights '''to use, copy, modify, merge, publish, distribute, sublicense, and/or sell '''copies of the Software, and to permit persons to whom the Software is '''furnished to do so, subject to the following conditions: ''' '''The above copyright notice and this permission notice shall be included in '''all copies or substantial portions of the Software. ''' '''THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR '''IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, '''FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE '''AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER '''LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, '''OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN '''THE SOFTWARE. ''' ''' http://code.google.com/p/lodgamebox/source/browse/trunk/com/lordofduct/util/LoDMath.as ''' </summary> ''' <remarks> ''' ''' </remarks> Public NotInheritable Class MathUtil #Region "Public ReadOnly Properties" Public Const PI As Double = 3.1415926535897931 ' Number pi Public Const PI_2 As Double = 1.5707963267948966 ' PI / 2 OR 90 deg Public Const PI_4 As Double = 0.78539816339744828 ' PI / 4 OR 45 deg Public Const PI_8 As Double = 0.39269908169872414 ' PI / 8 OR 22.5 deg Public Const PI_16 As Double = 0.19634954084936207 ' PI / 16 OR 11.25 deg Public Const TWO_PI As Double = 6.2831853071795862 ' 2 * PI OR 180 deg Public Const THREE_PI_2 As Double = 4.71238898038469 ' 3 * PI_2 OR 270 deg Public Const E As Double = 2.71828182845905 ' Number e Public Const LN10 As Double = 2.3025850929940459 ' ln(10) Public Const LN2 As Double = 0.69314718055994529 ' ln(2) Public Const LOG10E As Double = 0.43429448190325182 ' logB10(e) Public Const LOG2E As Double = 1.4426950408889634 ' logB2(e) Public Const SQRT1_2 As Double = 0.70710678118654757 ' sqrt( 1 / 2 ) Public Const SQRT2 As Double = 1.4142135623730951 ' sqrt( 2 ) Public Const DEG_TO_RAD As Double = 0.017453292519943295 ' PI / 180 Public Const RAD_TO_DEG As Double = 57.295779513082323 ' 180.0 / PI Public Const B_16 As Integer = 65536 ' 2^16 Public Const B_31 As Long = 2147483648 ' 2^31 Public Const B_32 As Long = 4294967296 ' 2^32 Public Const B_48 As Long = 281474976710656 ' 2^48 Public Const B_53 As Long = 9007199254740992 ' 2^53 !!NOTE!! largest accurate double floating point whole value Public Shared ReadOnly B_63 As ULong = CULng("9223372036854775808") ' 2^63 Public Const B_64_m1 As ULong = ULong.MaxValue '18446744073709551615 or 2^64  1 or ULong.MaxValue... Public Const ONE_THIRD As Double = 0.33333333333333331 ' 1.0/3.0 Public Const TWO_THIRDS As Double = 0.66666666666666663 ' 2.0/3.0 Public Const ONE_SIXTH As Double = 0.16666666666666666 ' 1.0/6.0 Public Const COS_PI_3 As Double = 0.8660254037844386 ' COS( PI / 3 ) Public Const SIN_2PI_3 As Double = 0.03654595 ' SIN( 2*PI/3 ) Public Const CIRCLE_ALPHA As Double = 0.55228474983079345 ' 4*(Math.sqrt(2)1)/3.0 Public Const ONN As Boolean = True Public Const OFF As Boolean = False Public Const SHORT_EPSILON As Double = 0.1 ' round integer epsilon Public Const PERC_EPSILON As Double = 0.001 ' percentage epsilon Public Const EPSILON As Double = 0.0001 ' single float average epsilon Public Const LONG_EPSILON As Double = 0.00000001 ' arbitrary 8 digit epsilon Public Shared ReadOnly MACHINE_EPSILON As Double = MathUtil.ComputeMachineEpsilon() Public Shared Function ComputeMachineEpsilon() As Double Dim fourThirds As Double = 4.0 / 3.0 Dim third As Double = fourThirds  1.0 Dim one As Double = third + third + third Return Math.Abs(1.0  one) End Function #End Region #Region "Public Shared Methods" Public Shared Function Largest(ByVal ParamArray args() As Double) As Double If args.Length < 1 Then Return Double.NaN Dim res As Double = args(0) For i As Integer = 1 To UBound(args) If args(i) > res Then res = args(i) Next Return res End Function Public Shared Function Smallest(ByVal ParamArray args() As Double) As Double If args.Length < 1 Then Return Double.NaN Dim res As Double = args(0) For i As Integer = 1 To UBound(args) If args(i) < res Then res = args(i) Next Return res End Function Public Shared Function InRange(ByVal value As Double, ByVal max As Double, Optional ByVal min As Double = 0) As Boolean Return (value >= min AndAlso value <= max) End Function REDACTED  several lines redacted seeing as it doesn't like to put it into a scrolly box... End Class End Namespace
This post has been edited by lordofduct: 05 November 2010  01:28 PM
#7
Re: check if number is between two numbers inclusive
Posted 05 November 2010  02:44 PM
Edit: lordofduct you've made an fundamental error in code you've shown. In .net arrays are ZeroIndex based.
This post has been edited by AdamSpeight2008: 05 November 2010  02:48 PM
#8
Re: check if number is between two numbers inclusive
Posted 05 November 2010  11:14 PM
#9
Re: check if number is between two numbers inclusive
Posted 06 November 2010  11:42 AM
#10
Re: check if number is between two numbers inclusive
Posted 06 November 2010  03:41 PM
'if the number does not fall within the minimum and maximum numbers inclusive, the function should return, else print the MsgBox. if Not (number>=minnuber) or Not(number <=maxinumber) then exit sub else messagebox.show("number is out of range.number must be between the minimum and the maximum number") end if
#11
Re: check if number is between two numbers inclusive
Posted 11 October 2012  09:47 AM
Select Case number Case minnumber to maxnumber : Exit Select Case Else : MsgBox("The number is outside of the range. It must be between " & minnumber & " and " & maxnumber & ".") End Select
#12
Re: check if number is between two numbers inclusive
Posted 24 February 2013  04:21 PM
Here is how the compiler was interpreting it:
if (((number >= minnumber) and number) <= maxnumber) then messagebox.show("number is out of range.number must be between the minimum and the maximum number") exit sub end if
Result:
(number >= minnumber) = True ((number >= minnumber) And number) = 5 (((number >= minnumber) And number) <= maxnumber) = 5
Essentially it reads it as:
If 5 then
When doing conditional operations, it will implicitly convert integers to booleans and interpret everything that is nonzero as true. If you turn Option Strict on then it will not let you run the application with the OP code because it no longer allows implicit conversions.
The whole problem could have been fixed using:
if (number >= minnumber) and (number <= maxnumber) then messagebox.show("number is out of range.number must be between the minimum and the maximum number") exit sub end if
#13
Re: check if number is between two numbers inclusive
Posted 24 February 2013  04:29 PM
#14
Re: check if number is between two numbers inclusive
Posted 24 February 2013  05:22 PM
#15
Re: check if number is between two numbers inclusive
Posted 24 February 2013  08:21 PM
