Fix integer division causing issue #17968 #18094
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
There is a bug in fast_pwm.cpp:277
The intention was most likely (from what the comment says and what would make sense) to
scale the given duty cycle (based on 255 as maximum) to the capcom top value.
But the float cast happens after the integer division already happened, thus would be correct only if top==v_size or a multiple of it.
So this code assumes top is always a multiple of 255.
I was asked to make a PR for this instead of a bug report.
Details
When dividing two integers, the fraction will be dropped and the number rounded towards zero.
In this line you divide the maximums in integer domain, cast to float and then scale the value to the new maximum.
So the line should look like this:
The first might cause integer overflows during multiplication, so I'd go for the second variant which operates on floats immediately.
Benefits
The duty cycle values for AVR targets with FAST_PWM_FAN suddenly would make sense and go way up to 100%
Related Issues
#17968