In my situation I am attempting to divide one float p
by another q
. The top is a multiple of the bottom and both have these properties:
- Exactly representable in decimal
- Have at most 3 or 4 significant figures
- Are between
1
and1e-8
.
(think, e.g., p=.0014
and q=.00002
)
In a perfect world the division would come out to a perfect integer (here 70). But floating point arithmetic is often imperfect.
I would like the simplest, safest, and most efficient method to avoid an error of returning p/q - 1
when I cast the quotient to int
.
My best solution right now is to do something like this:
int(p/q + 1e-10)
but that feels unclean and potentially less efficient that what may be possible.
Also, I am aware I can round, but that seems misleading in the code and potentially less efficient than a straight cast of some sort.