Skip to content
Open
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
5 changes: 3 additions & 2 deletions dist/fraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -856,9 +856,10 @@ Fraction.prototype = {
const d = this['d'] * P['n'];
const r = n % d;

// round(n / d) = ifloor(n / d) + 2(n % d) >= d ? 1 : 0
// Break ties towards +Infinity, like round(): round up on an exact half
// only when the value is non-negative, so roundTo(1) matches round().
let k = ifloor(n / d);
if (r + r >= d) {
if ((this['s'] >= C_ZERO ? C_ONE : C_ZERO) + r + r > d) {
k++;
}
return newFraction(this['s'] * k * P['n'], P['d']);
Expand Down
2 changes: 1 addition & 1 deletion dist/fraction.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions dist/fraction.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -856,9 +856,10 @@ Fraction.prototype = {
const d = this['d'] * P['n'];
const r = n % d;

// round(n / d) = ifloor(n / d) + 2(n % d) >= d ? 1 : 0
// Break ties towards +Infinity, like round(): round up on an exact half
// only when the value is non-negative, so roundTo(1) matches round().
let k = ifloor(n / d);
if (r + r >= d) {
if ((this['s'] >= C_ZERO ? C_ONE : C_ZERO) + r + r > d) {
k++;
}
return newFraction(this['s'] * k * P['n'], P['d']);
Expand Down
5 changes: 3 additions & 2 deletions src/fraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -862,9 +862,10 @@ Fraction.prototype = {
const d = this['d'] * P['n'];
const r = n % d;

// round(n / d) = ifloor(n / d) + 2(n % d) >= d ? 1 : 0
// Break ties towards +Infinity, like round(): round up on an exact half
// only when the value is non-negative, so roundTo(1) matches round().
let k = ifloor(n / d);
if (r + r >= d) {
if ((this['s'] >= C_ZERO ? C_ONE : C_ZERO) + r + r > d) {
k++;
}
return newFraction(this['s'] * k * P['n'], P['d']);
Expand Down
19 changes: 19 additions & 0 deletions tests/fraction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1518,6 +1518,25 @@ var tests = [{
fn: "roundTo",
param: "1/2",
expect: "-3.5"
}, {
// a tie must round the same way as round() (half towards +Infinity)
label: "-1/2 round to multiple of 1",
set: "-1/2",
fn: "roundTo",
param: "1",
expect: "0"
}, {
label: "-3/2 round to multiple of 1",
set: "-3/2",
fn: "roundTo",
param: "1",
expect: "-1"
}, {
label: "-1/4 round to multiple of 1/2",
set: "-1/4",
fn: "roundTo",
param: "1/2",
expect: "0"
}, {
label: "log_2(8)",
set: "8",
Expand Down