Home > Net >  Check if num is between two double value - Dart
Check if num is between two double value - Dart

Time:11-22

Why can't you do this if you try to find out whether an int is between two numbers:

if (16.5 < value < 17.5)

Instead of it, you'll have to do

if (value > 16.5 && value < 17.5)

which seems like a bit of overhead.

CodePudding user response:

short answer

you can make own method like

between(value, 1, 10);

long answer

you have to think about how the compilor works.

first. they have a some kind of parser that reads program language.

if you write 'if' conditional. a parsor read 'i' and 'f' character. then expect '(' sign.

if ( a > b )

check value, check sign, check value again, and check ')' sign.

The program then knows that this is a conditional statement. then make machine code like 01010101(idk. but we can't read something).

In this fomular. Finally comparing the values ​​is one by one(The compiler will work in the smallest unit possible).

Efficiency is very important at this stage. come back your code.

if (16.5 < value < 17.5)

and how about this?

if (16.5 < value > 17.5)

and how about?

if (16.5 > value < 17.5)

this is have many exception. But dart might be able to make this syntax. but they won't.

Because they too have to do the work of comparing one by one.

so you can make own method.

CodePudding user response:

Try the following code:

if (value.clamp(16.6, 17.4) == value) {
  // Do what you want to do
}
  • Related