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

Different results when using String vs Number in bignumber function #3137

Closed
bornova opened this issue Jan 24, 2024 · 3 comments
Closed

Different results when using String vs Number in bignumber function #3137

bornova opened this issue Jan 24, 2024 · 3 comments
Labels

Comments

@bornova
Copy link
Contributor

bornova commented Jan 24, 2024

Describe the bug
Using a number value instead of string gives different results in the following case:

math.evaluate(hex(bignumber(18446744073709552)))  // 0x4189374bc6a7ee
math.evaluate(hex(bignumber("18446744073709552")))  // 0x4189374bc6a7f0

To Reproduce
Visit: https://mathnotepad.com/#contents=N4IgzgLghhCmIC5RlgG1gYwgSwPYDtFRt8ATWAD0QEYAacaAJwkQAZ7Yy2BfesDRrlSoAKrgAOPDhXGNYYMHnxhEAbVAA3KKgCu8BCAAWlABQAjbAHN8OgLZnYjE9QAcAFjcA2AOwfW3gGZvVgBOAFYwgCYASmiAHXxjCnMrG3tHEziQVw8fP0Dg8Kis2JB6W1hoIm5eTW09RBAykAqqpBqAXXoNR0UCRAD6HXFSGFhSRsjWSLcAWlZqWZmRahCEaYQAzwA6VjCXAC0QbiA%3D

Note
Changing configuration to 'BigNumber' does fix the issue: math.config({ number: 'BigNumber' }):

// With math.config({ number: 'BigNumber' })
math.evaluate(hex(18446744073709552))  // 0x4189374bc6a7f0
math.evaluate(hex("18446744073709552"))  // 0x4189374bc6a7f0

However, then the following expression gives an error, which may be a separate bug:

// With math.config({ number: 'BigNumber' })
math.evaluate(format(-830, { notation: "hex", wordSize: 32 }))  // Error: Option "wordSize" must be a number

// can be fixed by doing:
math.evaluate(format(-830, { notation: "hex", wordSize: number(32) }))  // 0xfffffcc2i32
// With math.config({ number: 'number' })
math.evaluate(format(-830, { notation: "hex", wordSize: 32 }))  // 0xfffffcc2i32

Thank you

@josdejong
Copy link
Owner

Try console.log(18446744073709552) in your developer console. By creating it as number, the value is already rounded off before it reaches the bignumber(...) constructor, since a JavaScript number can hold only about 16 digits.

@bornova
Copy link
Contributor Author

bornova commented Jan 24, 2024

Thank you @josdejong, that explains it. How about the second part of the question where an error is throws when evaluating the following:

// With math.config({ number: 'BigNumber' })
math.evaluate(format(-830, { notation: "hex", wordSize: 32 }))  // Error: Option "wordSize" must be a number

@josdejong
Copy link
Owner

You're welcome.

The function format does not support a BigNumber as wordSize, it would be neat to improve on that! Can you open a separate issue for that @bornova?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants