# If Statement Threshold

This topic is 4033 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a statement like... if(mNeedleRotation != mNeedleRotationGoal ) mNeedleRotation is updated every tick and when mNeedleRotation == mNeedleRotationGoal, the needle will no longer be updated. However, the goal could potentially be 15 and the needle rotation potentially might skip over EXACTLY 15. Is there a way I can put a threshold into the if statement to get values near 15? Like 15.5 - 16.5, etc... Thanks!

##### Share on other sites
Assuming a language with the fabs() function (floating point absoulute value) you can use
if(fabs(mNeedleRotation - mNeedleRotationGoal) < 1.0)

Or something similar.

##### Share on other sites
Just write down what you said in the last line, but in code [smile]

You need to work out what your acceptable limits are.

eg: (assuming C++)
const float SOME_MAGIC_NAMED_CONSTANT = ...;float distance = fabs(mNeedleRotation - mNeedleRotationGoal);if( distance < SOME_MAGIC_NAMED_CONSTANT ) {}

##### Share on other sites
Thanks guys - I didn't realize that is what fabs was basically for.

##### Share on other sites
Quote:
 Original post by rip-off const float SOME_MAGIC_NAMED_CONSTANT = ...; float distance = fabs(mNeedleRotation - mNeedleRotationGoal); if( distance < SOME_MAGIC_NAMED_CONSTANT ) { }

The problem with that is that if mNeedleRotation has passed mNeedleRotationGoal, but by more than SOME_MAGIC_NAMED_CONSTANT. For example, what if SOME_MAGIC_NAMED_CONSTANT is set to 0.1 and mNeedleRotation is 15.2? Of course, you could make the value of SOME_MAGIC_NAMED_CONSTANT bigger, but that will make the condition less accurate.

Take a look at this. It will detect when mNeedleRotation crosses mNeedleRotationGoal.
    ...    float delta = mNeedleRotation - mOldNeedleRotation;    if ( delta >= 0 && mNeedleRotation >= mNeedleRotationGoal ||         delta < 0 && mNeedleRotation <= mNeedleRotationGoal )    {        ...    }    mOldNeedleRotation = mNeedleRotation;    ...

##### Share on other sites
Quote:
 Original post by JohnBoltonTake a look at this. It will detect when mNeedleRotation crosses mNeedleRotationGoal.  ... float delta = mNeedleRotation - mOldNeedleRotation; if ( delta >= 0 && mNeedleRotation >= mNeedleRotationGoal || delta < 0 && mNeedleRotation <= mNeedleRotationGoal ) { ... } mOldNeedleRotation = mNeedleRotation; ...

Also known as:
    ...    if ( (mNeedleRotation - mNeedleRotationGoal) *          (mOldNeedleRotation - mNeedleRotationGoal) > 0.0)    {        // Still on the same side of the goal.    }    mOldNeedleRotation = mNeedleRotation;    ...

1. 1
Rutin
30
2. 2
3. 3
4. 4
5. 5

• 13
• 13
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632961
• Total Posts
3009494
• ### Who's Online (See full list)

There are no registered users currently online

×