Skip to content

Commit b8c5688

Browse files
authored
Defensive guards to prevent ending up with Infinity (#6335)
1 parent cbd15ec commit b8c5688

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

ui/packages/shared/utilities/src/bigint.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ export const scaleLinear = (
5555
console.log('domainRange', domainRange, rangeRange, divide(rangeRange, domainRange));
5656
}
5757

58-
// rate * MULTIPLE to retain the decimal places in BigInt format, then divide by MULTIPLE to get the final result
59-
const rate = BigInt(Math.round(divide(rangeRange, domainRange) * MULTIPLE));
58+
// guard 0n to avoid BigInt(Infinity).
59+
const rate =
60+
domainRange === 0n ? 0n : BigInt(Math.round(divide(rangeRange, domainRange) * MULTIPLE));
6061

6162
const func = (x: bigint): number => {
6263
if (debugLog) {
@@ -74,6 +75,9 @@ export const scaleLinear = (
7475
};
7576

7677
func.ticks = (count = 5): bigint[] => {
78+
if (count <= 1 || domainRange === 0n) {
79+
return [domainMin];
80+
}
7781
const step = domainRange / BigInt(count - 1);
7882
const ticks: bigint[] = [];
7983
for (let i = 0; i < count; i++) {

0 commit comments

Comments
 (0)