How to find a closest entry in an array using system verilog

In reply to dave_59:

Yes, I did for positive values, it works, I used a foreach loop and used this condition in that, it’s picking the correct value for positive numbers but for negative numbers it’s not quite working