Skip to content

Preserve subnormal erfc results in the positive tail - #508

Open
jmh530 wants to merge 1 commit into
libmir:masterfrom
jmh530:codex/erfc-subnormal
Open

jmh530 wants to merge 1 commit into
libmir:masterfrom
jmh530:codex/erfc-subnormal

Conversation

@jmh530

@jmh530 jmh530 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

erfc(9.4375f) and erfc(26.65625) returned zero because the positive-tail calculation stopped when x*x exceeded log(T.max). Both results are representable subnormals.

Derive the early-return bound from the floating-point format. In the far tail, compute z = exp(-x*x/2) once and evaluate (z * erfce(x)) * z. This keeps the intermediate factors normal and lets the final multiplication round to a subnormal or zero.

The regression tests check both reported inputs against their rounded MPFR values. For each type, they also check the two adjacent inputs where the result changes from the smallest positive subnormal to zero.

On Windows x64, all 75 modules passed the DMD 2.114.0 unit suite. The optimized normal-function module tests also passed with DMD 2.114.0 and LDC 1.43.0.

Fixes #507

AI-Assisted-By: Codex (GPT-6.2 Astra Medium)

The log(T.max) cutoff made erfc(9.4375f) and erfc(26.65625) return
zero even though both results are representable. Derive a conservative
cutoff from the floating-point format and evaluate exp(-x*x/2) twice
as a factor, so only the final multiplication can underflow.

Check both reported values against MPFR and test adjacent float and
double inputs on either side of the round-to-zero boundary.

Fixes libmir#507

AI-Assisted-By: Codex (GPT-6.2 Astra Medium)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

erfc returns zero for representable float and double subnormal results

1 participant

⌂ Proxy