Test if variable between two values.

General FreeBASIC programming questions.
Post Reply
Dinosaur
Posts: 1510
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Test if variable between two values.

Post by Dinosaur »

Hi All

I know I can do:
x = 3.2
If (X > 3.1) and (X < 3.4) Then
Do Whatever
EndIf

However, I seem to recall a simpler version something like
If X <3.1< 3.4 Then
Do whatever
EndIf

However that doesn't create an error but doesn't work either.

So basically a test for "If Between" value 1 and Value2.

Sounds embarrassingly easy.

Regards
PaulSquires
Posts: 1084
Joined: Jul 14, 2005 23:41

Re: Test if variable between two values.

Post by PaulSquires »

I don't think that FB can do that type of expression evaluation (looks like something that is done in Python?).

Simple macros could be an easy alternative:

Code: Select all

#define IS_BETWEEN(value, low, high) ((value) >= (low) AndAlso (value) <= (high))
#define IS_STRICTLY_BETWEEN(value, low, high) ((value) > (low) AndAlso (value) < (high))

Dim x As Integer = 5

If IS_BETWEEN(x, 1, 10) Then
    Print "x is between 1 and 10"
Else
    Print "x is not between 1 and 10"
End If

sleep

The macro uses AndAlso to ensure short-circuit evaluation.
The macro includes parentheses around each argument to avoid precedence issues.

(I used ChatGPT to see if it knew of a shorter expression based way in FB of doing the comparison)
Dinosaur
Posts: 1510
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: Test if variable between two values.

Post by Dinosaur »

Hi All

Thanks for the response Paul.
Perhaps it was wishful thinking instead of a memory.

Regards
paul doe
Posts: 1881
Joined: Jul 25, 2017 17:22
Location: Argentina
Contact:

Re: Test if variable between two values.

Post by paul doe »

Dinosaur wrote: Jul 04, 2025 20:00 Perhaps it was wishful thinking instead of a memory.
Not at all:

Code: Select all

#define between_inclusive(_l_, _h_, _v_) (sgn(_l_ - _v_) <= 0 and sgn(_v_ - _h_) <= 0)
#define between(_l_, _h_, _v_) (sgn(_l_ - _v_) < 0 and sgn(_v_ - _h_) < 0)

? between_inclusive(3.14, 3.22, 3.22)
? between_inclusive(3.14, 3.22, 3.14)
? between_inclusive(3.14, 3.22, 3.18)
?
? between(3.14, 3.22, 3.22)
? between(3.14, 3.22, 3.14)
? between(3.14, 3.22, 3.18)
Now is it worth it? That's a different question...
dodicat
Posts: 8297
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Test if variable between two values.

Post by dodicat »

There must be at least a million ways of doing this -- because there are an infinite amount of numbers in our counting system
I am sure there is an elegant way somewhere.
Here is another:

Code: Select all

#define Inclusive <=0
#define Exclusive  <0
#define IsWithin(l,h,x,flag) ((l)-(x))*((h)-(x))flag

? IsWithin(3.14, 3.22, 3.22,Inclusive)
? IsWithin(3.14, 3.22, 3.14,Inclusive)
? IsWithin(3.14, 3.22, 3.18,Inclusive)
?

? IsWithin(3.14, 3.22, 3.22,Exclusive)
? IsWithin(3.14, 3.22, 3.14,Exclusive)
? IsWithin(3.14, 3.22, 3.18,Exclusive)
?

sleep
 
adeyblue
Posts: 367
Joined: Nov 07, 2019 20:08

Re: Test if variable between two values.

Post by adeyblue »

Pfft, anybody can make a computer do it, what you need is humans*

Code: Select all

#macro PrintAndRetry(prompt, v1, v2, failLabel)

Print Using prompt; v1; v2
'' gen variable name
__FB_UNIQUEID_PUSH__(answer)
'' gen retry label
__FB_UNIQUEID_PUSH__(retry)
'' make retry label
__FB_UNIQUEID__(retry):
'' define string and get input
dim as zstring * 2 __FB_UNIQUEID__(answer)
Line Input __FB_UNIQUEID__(answer)

'' check the rapscallion answered
If Len(__FB_UNIQUEID__(answer)) = 0 Then
    Print "But thou must answer!"
    Goto __FB_UNIQUEID__(retry)
End If
If __FB_UNIQUEID__(answer)[0]=Asc("y") OrElse __FB_UNIQUEID__(answer)[0]=Asc("Y") Then
    Goto failLabel
End If
__FB_UNIQUEID_POP__(answer)
__FB_UNIQUEID_POP__(retry)
#endmacro

#macro Between(x, lower, upper, result)

'' gen fail label
__FB_UNIQUEID_PUSH__(failLabel)
'' gen exit label
__FB_UNIQUEID_PUSH__(exitLabel)

'' ask the questions
PrintAndRetry("Is & less than &?", x, lower, __FB_UNIQUEID__(failLabel))
PrintAndRetry("Is & more than &?", x, upper, __FB_UNIQUEID__(failLabel))

'' they answered no twice, it is between
result = True
'' goto exit label
goto __FB_UNIQUEID__(exitLabel)
'' failLabel - they answered yes somewhere
__FB_UNIQUEID__(failLabel):
result = False
'' exit label
__FB_UNIQUEID__(exitLabel):

__FB_UNIQUEID_POP__(exitLabel)
__FB_UNIQUEID_POP__(failLabel)
#endmacro

dim isBetween as Boolean
Between(2, 1, 3, isBetween)
Print Using "2 & between 1 and 3"; Iif(isBetween, "is", "isn't")

Between(7.1, 8.9, 10.6, isBetween)
Print Using "7.1 & between 8.9 and 10.6"; Iif(isBetween, "is", "isn't")
* Not 100% guaranteed to work, some humans is idiots
dodicat
Posts: 8297
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Test if variable between two values.

Post by dodicat »

You'll have to do
static as zstring * 2 __FB_UNIQUEID__(answer)
To avoid the warnings.

Apart from that it runs like a sewing machine.
I've looked at sewing machines from both sides now (Joni Mitchel)

It's sewing machine illusions I recall
I really don't know sewing machines at all
https://www.youtube.com/watch?v=tIv2GoQE3CI
Dinosaur
Posts: 1510
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: Test if variable between two values.

Post by Dinosaur »

Hi All

Many thanks for the replies.
From those I gather that my original statement:
if (X > Y) and (X < Z)

is about as simple as it is going to get.

Regards
Post Reply