I'm trying to take the absolute value of a number. C, Python, Matlab, etc. all have this kind of thing built in, but in Verilog you have to make your own. So I write this:
function abs(shortreal x);
if (x >= 0.0) begin
abs = x;
end else begin
abs = -1 * x;
end
endfunction
Nothing to screw up, right?
For some reason that is mysterious to me, this function is returning 0 when passed 0.001. How is this possible? 0.001 is obviously > 0. Moreover, it's far enough away from 0 that rounding error should not be a factor. The if statement should evaluate to true and abs = 0.001.
Does anyone know what is going on? Is this one of those things where the SV standard leaves a lot to the imagination so different language implementations behave differently? Sim is running on ModelSim 2020.1_3 in case it matters.