CheapStepper icon indicating copy to clipboard operation
CheapStepper copied to clipboard

newMoveToDegree Int overflow in toStep variable

Open drp0 opened this issue 4 years ago • 3 comments

int toStep = deg * totalSteps / 360 does not create correct value Solved using:

void CheapStepper::newMoveToDegree (bool clockwise, int deg){

	// keep to 0-359 range
	if (deg >= 360) deg %= 360;
	else if (deg < 0) {
		deg %= 360; // returns negative if deg not multiple of 360
		if (deg < 0) deg += 360; // shift into 0-359 range
	}
	float f = totalSteps / 360.0; // drp
	int toStep = 0.5 + deg * f;
	newMoveTo (clockwise, toStep);
}

David

drp0 avatar Nov 02 '19 11:11 drp0

If i well understand, your code in practise rounds the float result before the int convertion. Right?

mrv96 avatar Jan 08 '20 18:01 mrv96

float f = totalSteps/360.0 forces a float result deg *f therefore produces float 0.5 +float does indeed round the float to nearest integer David

drp0 avatar Jan 08 '20 18:01 drp0

Well. I will update my fork asap and i will open a pull request.

I have also to see if i have to add this rounding code in other methods. I suppose that newMoveToDegree() isn't the only one that should be updated.

mrv96 avatar Jan 08 '20 18:01 mrv96