Skip to content

Data.HugeNum.fromNumber throws error for NaN and Infinity #31

Open
@smilack

Description

@smilack

Describe the bug
NaN and Infinity are both Numbers, but Data.HugeNum.fromNumber fails to parse them and throws an error.

To Reproduce

> import Data.HugeNum (fromNumber)
> import Data.Number (nan, infinity)
> fromNumber nan
file:///home/tom/frunco/.psci_modules/Data.Maybe/index.js:263
        throw new Error("Failed pattern match at Data.Maybe (line 288, column 1 - line 288, column 46): " + [ v.constructor.name ]);
              ^

Error: Failed pattern match at Data.Maybe (line 288, column 1 - line 288, column 46): Nothing
    at file:///home/tom/frunco/.psci_modules/Data.Maybe/index.js:263:15
    at toString (file:///home/tom/frunco/.psci_modules/Data.HugeNum/index.js:173:18)
    at showHugeNum.show (file:///home/tom/frunco/.psci_modules/Data.HugeNum/index.js:233:25)
    at file:///home/tom/frunco/.psci_modules/Effect.Console/index.js:18:29
    at file:///home/tom/frunco/.psci_modules/$PSCI/index.js:5:120
    at ModuleJob.run (node:internal/modules/esm/module_job:217:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)

Node.js v20.9.0

> fromNumber infinity
file:///home/tom/frunco/.psci_modules/Data.Maybe/index.js:263
        throw new Error("Failed pattern match at Data.Maybe (line 288, column 1 - line 288, column 46): " + [ v.constructor.name ]);
              ^

Error: Failed pattern match at Data.Maybe (line 288, column 1 - line 288, column 46): Nothing
    at file:///home/tom/frunco/.psci_modules/Data.Maybe/index.js:263:15
    at toString (file:///home/tom/frunco/.psci_modules/Data.HugeNum/index.js:173:18)
    at showHugeNum.show (file:///home/tom/frunco/.psci_modules/Data.HugeNum/index.js:233:25)
    at file:///home/tom/frunco/.psci_modules/Effect.Console/index.js:18:29
    at file:///home/tom/frunco/.psci_modules/$PSCI/index.js:5:120
    at ModuleJob.run (node:internal/modules/esm/module_job:217:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)

Expected behavior
fromNumber should probably return a Maybe Number instead.

Additional context
fromNumber calls parseNumber, which assumes that anything with type Number can be a valid HugeNum. parseNumber is not exported or called by other functions, so it could be modified to return a Maybe NumberStyle, using Data.Number.isFinite to ensure the input is valid.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugSomething that should function correctly isn't.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions