Skip to content

potential bug on large numeric values #86

@jcoveney-anchorzero

Description

@jcoveney-anchorzero

hello! I'd be happy to file a fix for this, but I wanted to understand the intent behind the code. I imagine that for someone who knows the code well it'd be pretty easy? (assuming it's something that y'all want to fix)

regardless, the issue starts here

https://github.com/sidorares/json-bigint/blob/master/lib/parse.js#L204

in my case, string = '123456789012345678901234567890.012345678901234567890123456789'
since number = +string, then number = 1.2345678901234568e+29.

as Number.isSafeInteger(number) is false, we get to

          return _options.storeAsString
            ? string
            : /[\.eE]/.test(string)
            ? number
            : _options.useNativeBigInt
            ? BigInt(string)
            : new BigNumber(string);

/[\.eE]/.test(string) is true, so this ends up just returning number, so 1.2345678901234568e+29, leading to a loss of precision I'd like to avoid.

I'm wonder if the intent might simply that this library wants to handle bigint values, but doesn't do anything specifically to handle large numeric values? in which case perhaps a PR wouldn't be welcome...but I'd be happy to extend it to support large numeric values. give BigNumber is already being used, it seems like it wouldn't be hard? could also add an option.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions