You need to do this using integers, or be happy with imprecision, because floats and doubles are inherently imprecise*. Your code is working exactly according to the C language spec. Floats and doubles are not safe to use in situations requiring exact precision.
Other than that you need more information in your question regarding what your requirements are. If nr
is a temperature then to calculate the exact value you need to use physics that either do not exist or cannot exist (I'm not a physicist so I couldn't tell you). If step
is actually an irrational value then you have to round up or down - your call and which is correct depends on your business logic requirements. If it's rational and no rounding is needed then you need to multiply out by the lowest common denominator and use int
s (or a fraction class in C or such).
*Per comment floats/doubles are precise when the fractions have denominator that is a power of 2, if you can force step
and y
to conform to this your problem will sort of clear, but of course if step
does not divide evenly into y
you will need to do the exact same business logic-handling as above.