Skip to content

[fork-ffi] numbers: max — PureScript docstring (Number.purs lines 196-197) states this version… #97

@Unisay

Description

@Unisay

Package: purescript-lua-numbers
File: src/Data/Number.lua
Function: max
Class: semantics Severity: medium

PureScript docstring (Number.purs lines 196-197) states this version returns NaN if EITHER argument is NaN, matching JS Math.max. Lua math.max folds with the > operator, and nan > x is false, so it keeps whichever non-NaN argument it visited: max(1)(nan) returns 1 (confirmed under Lua 5.1) rather than NaN. NaN propagation is therefore order-dependent (max(nan)(1) does yield nan, max(1)(nan) does not).

Current (Lua):

max = (function(n1) return function(n2) return math.max(n1, n2) end end)

Expected: max NaN x = NaN and max x NaN = NaN for any x, like JS Math.max.

Proposed fix:

Add an explicit NaN guard: `function(n1) return function(n2) if n1 ~= n1 or n2 ~= n2 then return 0/0 else return math.max(n1, n2) end end end`.

Found by the FFI audit; reproduced under Lua 5.1.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions