Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rounding algorithm #958

Merged
merged 6 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 34 additions & 21 deletions src/lib/Characteristic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,7 @@ describe("Characteristic", () => {
const characteristic = createCharacteristicWithProps({
format: Formats.FLOAT,
minStep: 0.001,
minValue: 0,
OrigamiDream marked this conversation as resolved.
Show resolved Hide resolved
perms: [Perms.NOTIFY],
});
// @ts-expect-error: private access
Expand Down Expand Up @@ -1095,32 +1096,44 @@ describe("Characteristic", () => {
});

it("should validate Formats.FLOAT with precision with minimum steps", () => {
const steps = (100 / 6);
const characteristic = createCharacteristicWithProps({
format: Formats.FLOAT,
perms: [Perms.PAIRED_READ, Perms.PAIRED_WRITE],
const characteristic = createCharacteristic(Formats.FLOAT);
let minStep;

minStep = 100 / 6;
characteristic.setProps({
minValue: 0,
maxValue: 100,
minStep: steps,
minStep: minStep,
});
for(let i = 1; i <= 7; i++) {
const desiredValue = Math.min(Math.max(i * minStep, 0), 100);
characteristic.setValue(i * minStep);
expect(characteristic.value).toEqual(desiredValue);
}

characteristic.setValue(steps);
expect(characteristic.value).toEqual(steps);

characteristic.setValue(steps * 2);
expect(characteristic.value).toEqual(steps * 2);

characteristic.setValue(steps * 3);
expect(characteristic.value).toEqual(steps * 3);

characteristic.setValue(steps * 4);
expect(characteristic.value).toEqual(steps * 4);

characteristic.setValue(steps * 5);
expect(characteristic.value).toEqual(steps * 5);
minStep = 1;
characteristic.setProps({
minValue: 0.5,
maxValue: 2.5,
minStep: minStep,
});
for(let i = 1; i <= 4; i++) {
const desiredValue = Math.min(Math.max(i * minStep + 0.5, 0.5), 2.5);
characteristic.setValue(i * minStep + 0.5);
expect(characteristic.value).toEqual(desiredValue);
}

characteristic.setValue(steps * 6);
expect(characteristic.value).toEqual(steps * 6);
minStep = 100 / 3;
characteristic.setProps({
minValue: 0,
maxValue: 100,
minStep: minStep,
});
for(let i = 1; i <= 4; i++) {
const desiredValue = Math.min(Math.max(i * minStep, 0), 100);
characteristic.setValue(i * minStep);
expect(characteristic.value).toEqual(desiredValue);
}
});

it("should allow negative floats in range for Formats.FLOAT", () => {
Expand Down
15 changes: 5 additions & 10 deletions src/lib/Characteristic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2041,6 +2041,11 @@ export class Characteristic extends EventEmitter {
stepValue = maxWithUndefined(this.props.minStep, 1);
}

if (stepValue != null && stepValue > 0) {
const minValue = numericMin != null ? numericMin : 0;
value = stepValue * Math.round((value - minValue) / stepValue) + minValue;
}

if (numericMin != null && value < numericMin) {
this.characteristicWarning(`characteristic was supplied illegal value: number ${value} exceeded minimum of ${numericMin}`);
value = numericMin;
Expand All @@ -2067,16 +2072,6 @@ export class Characteristic extends EventEmitter {
}
}

if (stepValue != null) {
if (stepValue === 1) {
value = Math.round(value);
} else if (stepValue > 1) {
const eps = 1; // stable constant for floating point precision
value = Math.round(value);
value = value - ((value + eps) % stepValue) + eps;
} // for stepValue < 1 rounding is done only when formatting the response. We can't store the "perfect" .step anyways
}

return value;
}
case Formats.STRING: {
Expand Down