Jump to content
Mike Torrettinni

Micro optimization: Math.InRange

Recommended Posts

On 5/12/2021 at 10:12 AM, Clément said:

 

The code for IsInRangeEx and isInRangeEx2:


function IsInRangeEx(const AValue, AMin, AMax: Cardinal): Boolean; inline;
begin
  Result := (AValue - AMin) <= (aMax - aMin);
end;

function IsInRangeEx2(const AValue, AMin, AMax: Integer): Boolean; inline;
begin
  Result := ((AValue-AMax)*(aValue-AMin) <= 0);
end;

 

Thanks, but these are some math tricks I can't figure out 😉

Share this post


Link to post

image.png

image.png

image.png

 

Need to be Edited again (lEditor lost all my text from 1st post).

 

Seems mathematically correct, but may face internal integer overflows, to be tested in Delphi ...

 

Edited by Rollo62
  • Like 2

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×