Skip to content

Conversation

@dynst
Copy link
Contributor

@dynst dynst commented Jul 19, 2025

Closes #1711

bigint doesn't seem to actually be missing any math?

The new byte array convesion helper function from #1698 bytesToUnsignedBigInt() really helps though.

@dynst dynst changed the title replace bn.js replace bn.js in @cosmjs/math Jul 19, 2025
@dynst dynst changed the title replace bn.js in @cosmjs/math remove bn.js Jul 19, 2025
@dynst dynst force-pushed the decimal branch 2 times, most recently from ebdcdc4 to ce6aaa0 Compare July 19, 2025 07:02
Copy link
Member

@webmaster128 webmaster128 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work, thanks a lot

public static compare(a: Decimal, b: Decimal): number {
if (a.fractionalDigits !== b.fractionalDigits) throw new Error("Fractional digits do not match");
return a.data.atomics.cmp(new BN(b.atomics));
return Math.sign(Number(a.data.atomics - b.data.atomics));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we implement this manually instead of using Math.sign, please? This function can cause all sorts of issues with -0 or NaN as return values. We don't need those cases as we have 2 ints as inputs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we have two bigint as inputs, how would any issues even happen?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not aware of any inputs that can cause any issues. But the number type is ugly as a result of bigint math. E.g. we can have a diff value that cannot be converted to number without loss.

With sucht a simple 3liner we don't need to know how number and Math.sign behave together with bigint

if (a.data.atomics === b.data.atomics) return 0;
else if (a.data.atomics < b.data.atomics) return -1;
else return 1;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, used if statements for the sake of future code archaeologists who don't realize Number(2n ** 970n - 2n ** 1024n) clamps the number to -Infinity.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the time when number will be removed from the language ;)

atomics: BigInt(atomics),
fractionalDigits: fractionalDigits,
};
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slightly out of scope, but we can now also change the constuctor argument to atomics: string | bigint for more efficientcy in a few places.

@webmaster128 webmaster128 added this to the 0.35 milestone Jul 22, 2025
dynst added 3 commits July 22, 2025 16:48
Number(bigint) will clamp large numbers to positive
or negative Infinity so the code works fine but this
is simply clearer-looking.
@dynst dynst mentioned this pull request Jul 22, 2025
@webmaster128
Copy link
Member

Looks good to me, thank you!

We just need an entry in the CHANGELOG.md. I will read through it again tomorrow and then merge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

replacing bn.js

2 participants