There are several ways you could easily fix this:
Use integers or rational numbers instead of floating point numbers.
Use rounding instead of truncating where applicable. You can implement
round(x)
simply asfloor(x + 0.5)
. Not particularly suitable in your case, but it leads straightforwardly to the next one.You can round at higher decimals and then floor. Say you want to floor anything lower than 0.9999 to 0, but 0.9999 or anything higher should be floored to 1. Then all you need is to use
floor(x + 0.0001)
instead offloor(x)
.