Skip to content

[fork-ffi] numbers: toExponentialNative — C printf %e always emits at least two exponent digits and zero-pads… #95

@Unisay

Description

@Unisay

Package: purescript-lua-numbers
File: src/Data/Number/Format.lua
Function: toExponentialNative
Class: format-specifier Severity: medium

C printf %e always emits at least two exponent digits and zero-pads them (e+03), whereas JS toExponential emits the minimal number of exponent digits (e+3). The module docstring (Format.purs lines 13-14) states toStringWith (exponential 2) 1234.56789 = "1.23e+3", but the Lua produces "1.23e+03". Likewise exponential 3 of 0.000123 gives "1.230e-04" (Lua) vs "1.230e-4" (JS), and exponential 0 of 5.0 gives "5e+00" vs "5e+0".

Current (Lua):

toExponentialNative = (function(d) return function(n) return string.format("%." .. tostring(d) .. "e", n) end end)

Expected: JS toExponential exponent uses minimal digits with no zero-padding: "1.23e+3", "1.230e-4", "5e+0".

Proposed fix:

Post-process the %e output to strip a single leading zero from the exponent, e.g.: `local s = string.format("%."..tostring(d).."e", n); return (s:gsub("([eE][%-+])0(%d)$", "%1%2"))`. This turns e+03 into e+3 while leaving two-digit exponents (e+12) intact.

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