[go: nahoru, domu]

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(up, down, halfUp): fix handling of numbers close to 0 and rounding of integer results #711

Merged
merged 6 commits into from
Feb 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix(up, down, halfUp): fix handling of numbers close to 0 and roundin…
…g of integer results

up/down - Functions should not round integer results.

up - up should only round positive numbers, 0 is not a positive number.

halfUp - halfUp was incorrectly rounding numbers in the [0,1) range due to the incorrect
definition of isPositive.

Fixes #710
  • Loading branch information
shasderias committed Jan 11, 2023
commit 58d96e239cbb2d56250553a7ca2e16f468b796af
60 changes: 60 additions & 0 deletions packages/core/src/divide/__tests__/down.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import { down } from '../down';

describe('down', () => {
describe('decimal factors', () => {
it('should not round positive integer quotients', () => {
expect(down(20, 10, calculator)).toBe(2);
});
it('should not round negative integer quotients', () => {
expect(down(-20, 10, calculator)).toBe(-2);
});
it('rounds down with a positive quotient below half', () => {
expect(down(14, 10, calculator)).toBe(1);
});
Expand All @@ -22,8 +28,41 @@ describe('down', () => {
it('rounds down with a negative quotient above half', () => {
expect(down(-16, 10, calculator)).toBe(-2);
});
it('rounds to 0 with a positive quotient above half that is close to 0', () => {
expect(down(6, 10, calculator)).toBe(0);
});
it('rounds to 0 with a positive half quotient that is close to 0', () => {
expect(down(5, 10, calculator)).toBe(0);
});
it('rounds to 0 with a positive quotient and below half that is close to 0', () => {
expect(down(4, 10, calculator)).toBe(0);
});
it('rounds to 0 with amount 1 and a positive quotient below half that is close to 0', () => {
expect(down(1, 10, calculator)).toBe(0);
});
it('rounds to 0 when quotient is 0', () => {
expect(down(0, 10, calculator)).toBe(0);
});
it('rounds to -1 with amount 1 and a negative quotient below half that is close to 0', () => {
expect(down(-1, 10, calculator)).toBe(-1);
});
it('rounds to -1 with a negative quotient close to and below half, that is close to 0', () => {
expect(down(-4, 10, calculator)).toBe(-1);
});
it('rounds to -1 with a negative half quotient that is close to 0', () => {
expect(down(-5, 10, calculator)).toBe(-1);
});
it('rounds to -1 with a negative quotient above half, that is close to 0', () => {
expect(down(-6, 10, calculator)).toBe(-1);
});
});
describe('non-decimal factors', () => {
it('should not round positive integer quotients', () => {
expect(down(20, 5, calculator)).toBe(4);
});
it('should not round negative integer quotients', () => {
expect(down(-20, 5, calculator)).toBe(-4);
});
it('rounds down with a positive quotient below half', () => {
expect(down(22, 5, calculator)).toBe(4);
});
Expand All @@ -42,5 +81,26 @@ describe('down', () => {
it('rounds down with a negative quotient above half', () => {
expect(down(-24, 5, calculator)).toBe(-5);
});
it('rounds to 0 with a positive quotient above half that is close to 0', () => {
expect(down(3, 5, calculator)).toBe(0);
});
it('rounds to 0 with a positive half quotient that is close to 0', () => {
expect(down(3, 6, calculator)).toBe(0);
});
it('rounds to 0 with amount 1 and a positive quotient below half that is close to 0', () => {
expect(down(1, 5, calculator)).toBe(0);
});
it('rounds to 0 when quotient is 0', () => {
expect(down(0, 5, calculator)).toBe(0);
});
it('rounds to -1 with amount -1 and a negative quotient below half that is close to 0', () => {
expect(down(-1, 5, calculator)).toBe(-1);
});
it('rounds to -1 with a negative half quotient that is close to 0', () => {
expect(down(-3, 6, calculator)).toBe(-1);
});
it('rounds to -1 with a negative quotient above half that is close to 0', () => {
expect(down(-3, 5, calculator)).toBe(-1);
});
});
});
60 changes: 60 additions & 0 deletions packages/core/src/divide/__tests__/halfAwayFromZero.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import { halfAwayFromZero } from '../halfAwayFromZero';

describe('halfAwayFromZero', () => {
describe('decimal factors', () => {
it('should not round positive integer quotients', () => {
expect(halfAwayFromZero(20, 10, calculator)).toBe(2);
});
it('should not round negative integer quotients', () => {
expect(halfAwayFromZero(-20, 10, calculator)).toBe(-2);
});
it('rounds down with a positive quotient below half', () => {
expect(halfAwayFromZero(14, 10, calculator)).toBe(1);
});
Expand All @@ -22,8 +28,41 @@ describe('halfAwayFromZero', () => {
it('rounds down with a negative quotient above half', () => {
expect(halfAwayFromZero(-16, 10, calculator)).toBe(-2);
});
it('rounds to 1 with a positive quotient above half that is close to 0', () => {
expect(halfAwayFromZero(6, 10, calculator)).toBe(1);
});
it('rounds to 1 with a positive half quotient that is close to 0', () => {
expect(halfAwayFromZero(5, 10, calculator)).toBe(1);
});
it('rounds to 0 with a positive quotient and below half that is close to 0', () => {
expect(halfAwayFromZero(4, 10, calculator)).toBe(0);
});
it('rounds to 0 with amount 1 and a positive quotient below half that is close to 0', () => {
expect(halfAwayFromZero(1, 10, calculator)).toBe(0);
});
it('rounds to 0 when quotient is 0', () => {
expect(halfAwayFromZero(0, 10, calculator)).toBe(0);
});
it('rounds to -0 with amount 1 and a negative quotient below half that is close to 0', () => {
expect(halfAwayFromZero(-1, 10, calculator)).toBe(-0);
});
it('rounds to -0 with a negative quotient close to and below half, that is close to 0', () => {
expect(halfAwayFromZero(-4, 10, calculator)).toBe(-0);
});
it('rounds to -1 with a negative half quotient that is close to 0', () => {
expect(halfAwayFromZero(-5, 10, calculator)).toBe(-1);
});
it('rounds to -1 with a negative quotient above half, that is close to 0', () => {
expect(halfAwayFromZero(-6, 10, calculator)).toBe(-1);
});
});
describe('non-decimal factors', () => {
it('should not round positive integer quotients', () => {
expect(halfAwayFromZero(20, 5, calculator)).toBe(4);
});
it('should not round negative integer quotients', () => {
expect(halfAwayFromZero(-20, 5, calculator)).toBe(-4);
});
it('rounds down with a positive quotient below half', () => {
expect(halfAwayFromZero(22, 5, calculator)).toBe(4);
});
Expand All @@ -42,5 +81,26 @@ describe('halfAwayFromZero', () => {
it('rounds down with a negative quotient above half', () => {
expect(halfAwayFromZero(-24, 5, calculator)).toBe(-5);
});
it('rounds to 1 with a positive quotient above half that is close to 0', () => {
expect(halfAwayFromZero(3, 5, calculator)).toBe(1);
});
it('rounds to 1 with a positive half quotient that is close to 0', () => {
expect(halfAwayFromZero(3, 6, calculator)).toBe(1);
});
it('rounds to 0 with amount 1 and a positive quotient below half that is close to 0', () => {
expect(halfAwayFromZero(1, 5, calculator)).toBe(0);
});
it('rounds to 0 when quotient is 0', () => {
expect(halfAwayFromZero(0, 5, calculator)).toBe(0);
});
it('rounds to -0 with amount -1 and a negative quotient below half that is close to 0', () => {
expect(halfAwayFromZero(-1, 5, calculator)).toBe(-0);
});
it('rounds to -1 with a negative half quotient that is close to 0', () => {
expect(halfAwayFromZero(-3, 6, calculator)).toBe(-1);
});
it('rounds to -1 with a negative quotient above half that is close to 0', () => {
expect(halfAwayFromZero(-3, 5, calculator)).toBe(-1);
});
});
});
60 changes: 60 additions & 0 deletions packages/core/src/divide/__tests__/halfDown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import { halfDown } from '../halfDown';

describe('halfDown', () => {
describe('decimal factors', () => {
it('should not round positive integer quotients', () => {
expect(halfDown(20, 10, calculator)).toBe(2);
});
it('should not round negative integer quotients', () => {
expect(halfDown(-20, 10, calculator)).toBe(-2);
});
it('rounds down with a positive quotient below half', () => {
expect(halfDown(14, 10, calculator)).toBe(1);
});
Expand All @@ -22,8 +28,41 @@ describe('halfDown', () => {
it('rounds down with a negative quotient above half', () => {
expect(halfDown(-16, 10, calculator)).toBe(-2);
});
it('rounds to 1 with a positive quotient above half that is close to 0', () => {
expect(halfDown(6, 10, calculator)).toBe(1);
});
it('rounds to 0 with a positive half quotient that is close to 0', () => {
expect(halfDown(5, 10, calculator)).toBe(0);
});
it('rounds to 0 with a positive quotient and below half that is close to 0', () => {
expect(halfDown(4, 10, calculator)).toBe(0);
});
it('rounds to 0 with amount 1 and a positive quotient below half that is close to 0', () => {
expect(halfDown(1, 10, calculator)).toBe(0);
});
it('rounds to 0 when quotient is 0', () => {
expect(halfDown(0, 10, calculator)).toBe(0);
});
it('rounds to -0 with amount 1 and a negative quotient below half that is close to 0', () => {
expect(halfDown(-1, 10, calculator)).toBe(-0);
});
it('rounds to -0 with a negative quotient close to and below half, that is close to 0', () => {
expect(halfDown(-4, 10, calculator)).toBe(-0);
});
it('rounds to -1 with a negative half quotient that is close to 0', () => {
expect(halfDown(-5, 10, calculator)).toBe(-1);
});
it('rounds to -1 with a negative quotient above half, that is close to 0', () => {
expect(halfDown(-6, 10, calculator)).toBe(-1);
});
});
describe('non-decimal factors', () => {
it('should not round positive integer quotients', () => {
expect(halfDown(20, 5, calculator)).toBe(4);
});
it('should not round negative integer quotients', () => {
expect(halfDown(-20, 5, calculator)).toBe(-4);
});
it('rounds down with a positive quotient below half', () => {
expect(halfDown(22, 5, calculator)).toBe(4);
});
Expand All @@ -42,5 +81,26 @@ describe('halfDown', () => {
it('rounds down with a negative quotient above half', () => {
expect(halfDown(-24, 5, calculator)).toBe(-5);
});
it('rounds to 1 with a positive quotient above half that is close to 0', () => {
expect(halfDown(3, 5, calculator)).toBe(1);
});
it('rounds to 0 with a positive half quotient that is close to 0', () => {
expect(halfDown(3, 6, calculator)).toBe(0);
});
it('rounds to 0 with amount 1 and a positive quotient below half that is close to 0', () => {
expect(halfDown(1, 5, calculator)).toBe(0);
});
it('rounds to 0 when quotient is 0', () => {
expect(halfDown(0, 5, calculator)).toBe(0);
});
it('rounds to -0 with amount -1 and a negative quotient below half that is close to 0', () => {
expect(halfDown(-1, 5, calculator)).toBe(-0);
});
it('rounds to -1 with a negative half quotient that is close to 0', () => {
expect(halfDown(-3, 6, calculator)).toBe(-1);
});
it('rounds to -1 with a negative quotient above half that is close to 0', () => {
expect(halfDown(-3, 5, calculator)).toBe(-1);
});
});
});
60 changes: 60 additions & 0 deletions packages/core/src/divide/__tests__/halfEven.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import { halfEven } from '../halfEven';

describe('halfEven', () => {
describe('decimal factors', () => {
it('should not round positive integer quotients', () => {
expect(halfEven(20, 10, calculator)).toBe(2);
});
it('should not round negative integer quotients', () => {
expect(halfEven(-20, 10, calculator)).toBe(-2);
});
it('rounds down with a positive quotient below half', () => {
expect(halfEven(14, 10, calculator)).toBe(1);
});
Expand All @@ -25,8 +31,41 @@ describe('halfEven', () => {
it('rounds down with a negative quotient above half', () => {
expect(halfEven(-16, 10, calculator)).toBe(-2);
});
it('rounds to 1 with a positive quotient above half that is close to 0', () => {
expect(halfEven(6, 10, calculator)).toBe(1);
});
it('rounds to 0 with a positive half quotient that is close to 0', () => {
expect(halfEven(5, 10, calculator)).toBe(0);
});
it('rounds to 0 with a positive quotient and below half that is close to 0', () => {
expect(halfEven(4, 10, calculator)).toBe(0);
});
it('rounds to 0 with amount 1 and a positive quotient below half that is close to 0', () => {
expect(halfEven(1, 10, calculator)).toBe(0);
});
it('rounds to 0 when quotient is 0', () => {
expect(halfEven(0, 10, calculator)).toBe(0);
});
it('rounds to -0 with amount 1 and a negative quotient below half that is close to 0', () => {
expect(halfEven(-1, 10, calculator)).toBe(-0);
});
it('rounds to -0 with a negative quotient close to and below half, that is close to 0', () => {
expect(halfEven(-4, 10, calculator)).toBe(-0);
});
it('rounds to -0 with a negative half quotient that is close to 0', () => {
expect(halfEven(-5, 10, calculator)).toBe(-0);
});
it('rounds to -1 with a negative quotient above half, that is close to 0', () => {
expect(halfEven(-6, 10, calculator)).toBe(-1);
});
});
describe('non-decimal factors', () => {
it('should not round positive integer quotients', () => {
expect(halfEven(20, 5, calculator)).toBe(4);
});
it('should not round negative integer quotients', () => {
expect(halfEven(-20, 5, calculator)).toBe(-4);
});
it('rounds down with a positive quotient below half', () => {
expect(halfEven(22, 5, calculator)).toBe(4);
});
Expand All @@ -48,5 +87,26 @@ describe('halfEven', () => {
it('rounds down with a negative quotient above half', () => {
expect(halfEven(-24, 5, calculator)).toBe(-5);
});
it('rounds to 1 with a positive quotient above half that is close to 0', () => {
expect(halfEven(3, 5, calculator)).toBe(1);
});
it('rounds to 0 with a positive half quotient that is close to 0', () => {
expect(halfEven(3, 6, calculator)).toBe(0);
});
it('rounds to 0 with amount 1 and a positive quotient below half that is close to 0', () => {
expect(halfEven(1, 5, calculator)).toBe(0);
});
it('rounds to 0 when quotient is 0', () => {
expect(halfEven(0, 5, calculator)).toBe(0);
});
it('rounds to -0 with amount -1 and a negative quotient below half that is close to 0', () => {
expect(halfEven(-1, 5, calculator)).toBe(-0);
});
it('rounds to -0 with a negative half quotient that is close to 0', () => {
expect(halfEven(-3, 6, calculator)).toBe(-0);
});
it('rounds to -1 with a negative quotient above half that is close to 0', () => {
expect(halfEven(-3, 5, calculator)).toBe(-1);
});
});
});
Loading