Skip to content

fromString doesn't support exponential notation #25

Open
@smilack

Description

@smilack

The documentation for fromString says:

Parse a string into a BigInt. Returns Nothing if the parse fails. Supports decimal, binary, octal, hexadecimal and exponentiation notations. See MDN for more examples.

Examples:

fromString "857981209301293808359384092830482"  -- 857981209301293808359384092830482
fromString "0b10000000000000000000000000000000" -- 2147483648
fromString "0B00000000011111111111111111111111" -- 8388607
fromString "0O755"                              -- 493
fromString "0o644"                              -- 420
fromString "0xFFFFFFFFFFFFFFFFF"                -- 295147905179352830000
fromString "0XA"                                -- 10
fromString "0e-5"                               -- 0
fromString "5e1"                                -- 50
fromString "175e-2"                             -- 1.75
fromString "1E3"                                -- 1000

However fromString actually fails to parse the last four examples:

> test s = log $ "fromString " <> show s <> " = " <> show (BigInt.fromString s)
> traverse test ["857981209301293808359384092830482", "0b10000000000000000000000000000000", ...
fromString "857981209301293808359384092830482" = (Just 857981209301293808359384092830482)
fromString "0b10000000000000000000000000000000" = (Just 2147483648)
fromString "0B00000000011111111111111111111111" = (Just 8388607)
fromString "0O755" = (Just 493)
fromString "0o644" = (Just 420)
fromString "0xFFFFFFFFFFFFFFFFF" = (Just 295147905179352825855)
fromString "0XA" = (Just 10)
fromString "0e-5" = Nothing
fromString "5e1" = Nothing
fromString "175e-2" = Nothing
fromString "1E3" = Nothing

I think this is mostly an error in the documentation, because this function is essentially an alias for the JavaScript BigInt function, and JavaScript BigInts don't support exponential notation. However, there is an argument that fromString should be modified to support it because its counterpart in purescript-bigints did and that module was deprecated in favor of this one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions