I'm working on a Nelder-Mead optimization routine in C that involves taking the average of two float
s. In rare (but perfectly reproducible) circumstances, the two float
s, say x
and y
, differ only by the least significant bit of their significand. When the average is taken, rounding errors imply that the result will be either x
or y
.
I'd like to specify that rounding should always be towards the second float
. That is, I cannot simply specify that rounding should be towards zero, or infinity, because I do not know in advance whether x
will be larger than y
.
(How) can I do that?
I don't think there's a hardware rounding mode for that. You have to write your own function, then,
double average(double x, double y) {
double a = 0.5*(x+y);
return (a == x) ? y : a;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With