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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle big numbers when calculating nice range/numbers #107

Merged
merged 2 commits into from
May 26, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/src/scale/util/nice_numbers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ List<num> _wilkinsonExtended(
int z = (dart_math.log(delta) / dart_math.ln10).ceil();

while (z < _maxLoop) {
final step = j * q * dart_math.pow(10, z);
final step = j * q * dart_math.pow(10.0, z);
final cm = _coverageMax(min, max, (step * (k - 1)));

if (w[0] * sm + w[1] * cm + w[2] * dm + w[3] < bestScore) {
Expand Down
4 changes: 2 additions & 2 deletions lib/src/scale/util/nice_range.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ num _tickIncrement(
: error >= _e2
? 2
: 1) *
dart_math.pow(10, power);
dart_math.pow(10.0, power);
}
return -dart_math.pow(10, -power) /
return -dart_math.pow(10.0, -power) /
(error >= _e10
? 10
: error >= _e5
Expand Down
6 changes: 6 additions & 0 deletions test/scale/util/nice_number_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,11 @@ main() {
expect(linearNiceNumbers(-1.02835066, 3.25839303, 5),
[-1.2, 0, 1.2, 2.4, 3.6]);
});

test('Nice numbers with values that overflow ints', () {
expect(linearNiceNumbers(0.0, 1e30, 5), [0, 3e29, 6e29, 9e29, 1.2e30]);

expect(linearNiceNumbers(0.0, 1e32, 5), [0, 3e31, 6e31, 9e31, 1.2e32]);
});
});
}
12 changes: 12 additions & 0 deletions test/scale/util/nice_range_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,16 @@ main() {

expect(linearNiceRange(0.4, 0.6, 5), [0.4, 0.6]);
});

test('Linear Nice range with values that overflow ints', () {
expect(
linearNiceRange(0, 999999999999999999999999999999.0, 5),
[0.0, 1.9999999999999998e+30],
);

expect(
linearNiceRange(0, 9999999999999999999999999999999.0, 5),
[0.0, 1e31],
);
});
}