Skip to content

[fork-ffi] integers: pow — Upstream JS is Math.pow(x,y) | 0 — it both truncates the result… #89

@Unisay

Description

@Unisay

Package: purescript-lua-integers
File: src/Data/Int.lua
Function: pow
Class: semantics Severity: medium

Upstream JS is Math.pow(x,y) | 0 — it both truncates the result toward zero and wraps it to Int32. The Lua omits the truncation: math.pow(x, y) returns a float and is not floored/truncated. For negative exponents the result is a fraction, so pow 2 (-1) is 0.5 in Lua but must be 0 (Test/Data/Int.purs lines 180-183: pow 2 (-1) == 0, pow 2 (-2) == 0, pow (-2) (-2) == 0). Even for non-negative exponents the result is a float (8.0 not 8) and large results are not wrapped to Int32.

Current (Lua):

pow = (function(x) return function(y) return math.pow(x, y) end end)

Expected: Integer result truncated toward zero (and ideally Int32-wrapped): pow 2 (-1) == 0, pow 2 (-2) == 0, pow (-2) (-2) == 0, pow 2 2 == 4.

Proposed fix:

pow = (function(x) return function(y)
  local r = math.modf(math.pow(x, y))  -- truncate toward zero like JS `| 0`
  return r
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