Solve denominator so quotient is whole number?

53 Views Asked by At

I have a simple equation.

road_length = ROADLENGTH / ROADSPACING

The problem is, I really need road_length to be a whole number because it's used in FOR loop in C++. Currently ROADLENGTH is 78.539 and I can't change it. ROADSPACING is how far one road segment is from the next and is the only other part of the equation I can adjust.

My question is, without guessing and checking a thousand times, is there a way to mathematically solve what ROADSPACING needs to be in order to get road_length to be a whole number? Keep in mind ROADLENGTH will change from road to road, so it's not always going to be 78.539. ROADSPACING is currently 0.1.

1

There are 1 best solutions below

0
On BEST ANSWER
integer = (int)(ROADLENGTH / ROADSPACING) // Equals 785
result = ROADLENGTH / integer // Equals 0.10004968152866242038216560509554

road_length = ROADLENGTH / result; //Equals ~785.0 (very, very close)

I actually get the (int) casting for the last step so it is 785 on the dot, but I didn't want to confuse anyone.