I have a double value that will be passed to Round, and I need to make sure it won't overflow an Int64. If it would overflow, I just set the value to 0. What is the best way of doing this? I thought about two ways:
- Handle the exception
try
rounded := Round(value);
except
on E: EInvalidOp do
rounded := 0;
end;
- Compare against
Int64.MaxValueandInt64.MinValue
if (CompareValue(value, Int64.MaxValue) <> GreaterThanValue) and (CompareValue(value, Int64.MinValue) <> LessThanValue) then
rounded := Round(value)
else
rounded := 0;
I'm wondering what is the best approach to handle this problem. Performance is a concern for me. I'm using Delphi 10.3.