I want to create a setter for a double variable num, but I would only like to update it if the input is a multiple of 0.5.
Here's what I have, but I'm worried about floating-point errors.
public void setNum(double num) {
if (num % 0.5 == 0.0) {
this.num = num;
}
}
I assume that for some inputs that actually are a multiple of 0.5, it might return some 0.0000003 or 0.49999997, thus not 0.0.
What can I do to remedy this? Or is this not a problem in this case?