This commit is contained in:
commit
568474d52f
1591 files changed
+528213
No files matched your search
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"tool": "godbolt",
|
||||
"compiler": "mojo_1_2_0",
|
||||
"mojoVersion": "1.2.0",
|
||||
"source_sha256": "5f4d29c698ba6dc96c3228c429bffd44d1fa4ef515b93600b9520b66a49e2458",
|
||||
"stdin": "7 0 0 1 -1 -1 0 1 1 7 -1 1624898745555275 -55 5902958103587057 -69 end\n",
|
||||
"args": "--target-cpu x86-64-v3 --fp-mode contract=off",
|
||||
"asked": "2026-11-12",
|
||||
"result": {
|
||||
"compiled": true,
|
||||
"buildStderr": "",
|
||||
"executed": true,
|
||||
"exitCode": 0,
|
||||
"stdout": "9.999999717180685e-10\n0.5205000163047472\n-0.8427006897476343\n0.9953221395812268\n0.999999255782864\n0.05085555733320535\n1.128486356871683e-05",
|
||||
"stderr": "",
|
||||
"timedOut": false,
|
||||
"asm": null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
from std.math import exp
|
||||
|
||||
|
||||
def erf_approx(x: Float64) -> Float64:
|
||||
var sign: Float64 = 1.0
|
||||
if x < 0.0:
|
||||
sign = -1.0
|
||||
var a = abs(x)
|
||||
var t = 1.0 / (1.0 + 0.3275911 * a)
|
||||
var poly = ((((1.061405429 * t - 1.453152027) * t + 1.421413741) * t - 0.284496736) * t + 0.254829592) * t
|
||||
return sign * (1.0 - poly * exp(-a * a))
|
||||
@@ -0,0 +1,5 @@
|
||||
import math
|
||||
|
||||
|
||||
def erf_approx(x):
|
||||
return math.erf(x)
|
||||
@@ -0,0 +1,9 @@
|
||||
import math
|
||||
|
||||
|
||||
def erf_approx(x):
|
||||
sign = -1.0 if x < 0.0 else 1.0
|
||||
a = abs(x)
|
||||
t = 1.0 / (1.0 + 0.3275911 * a)
|
||||
poly = ((((1.061405429 * t - 1.453152027) * t + 1.421413741) * t - 0.284496736) * t + 0.254829592) * t
|
||||
return sign * (1.0 - poly * math.exp(-a * a))
|
||||
@@ -0,0 +1,60 @@
|
||||
{
|
||||
"name": "ErfApprox",
|
||||
"summary": "The error function by Abramowitz and Stegun's formula 7.1.26, whose stated absolute error is below 1.5e-7 for every X.",
|
||||
"tags": [
|
||||
"special functions",
|
||||
"approximation",
|
||||
"statistics"
|
||||
],
|
||||
"sources": [
|
||||
{
|
||||
"title": "Abramowitz and Stegun, Handbook of Mathematical Functions (National Bureau of Standards, 1964), section 7.1, formula 7.1.26",
|
||||
"url": "https://archive.org/details/HandbookOfMathematicalFunctions"
|
||||
}
|
||||
],
|
||||
"cases": [
|
||||
{
|
||||
"call": "ErfApprox(0.0)"
|
||||
},
|
||||
{
|
||||
"call": "ErfApprox(0.5)"
|
||||
},
|
||||
{
|
||||
"call": "ErfApprox(-1.0)"
|
||||
},
|
||||
{
|
||||
"call": "ErfApprox(2.0)"
|
||||
},
|
||||
{
|
||||
"call": "ErfApprox(3.5)"
|
||||
},
|
||||
{
|
||||
"call": "ErfApprox(0.0451)"
|
||||
},
|
||||
{
|
||||
"call": "ErfApprox(1.0e-5)"
|
||||
}
|
||||
],
|
||||
"references": [
|
||||
{
|
||||
"lang": "python",
|
||||
"level": "plain",
|
||||
"file": "ref/python/plain.py",
|
||||
"note": "The same formula."
|
||||
},
|
||||
{
|
||||
"lang": "python",
|
||||
"level": "libm",
|
||||
"file": "ref/python/libm.py",
|
||||
"note": "Python's math.erf, which uses the platform C library's erf: the approximation must stay within its stated 1.5e-7 of it. ErfApprox(0.0451) sits next to the largest error, about 1.39e-7.",
|
||||
"abs": 1.5e-07
|
||||
},
|
||||
{
|
||||
"lang": "mojo",
|
||||
"level": "plain",
|
||||
"file": "ref/mojo/plain.mojo",
|
||||
"note": "The same formula in Mojo. The compiled program calls Mojo's own exp(), which in Mojo 1.2.0 is off by up to about 6.5e-12 of the value on [-16, 0], so the last digits are not the pseudo's. The result is 1 - Poly * exp(-A * A), so the difference is small in absolute terms (at most 1.2e-13 over 640,000 values of X between -8 and 8, many of them near 0, computed at run time by Mojo 1.2.0), but not in relative terms near 0, where erf is small: at X = 1.0e-5 the difference is about 8e-12 of the value.",
|
||||
"abs": 2e-13
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// ErfApprox: the error function by Abramowitz and Stegun's formula 7.1.26,
|
||||
// with an absolute error below 1.5e-7 for every X. It is an approximation
|
||||
// with a stated bound, not the correctly rounded erf: the libm reference
|
||||
// holds it to that bound on the cases. ERF(X) for X < 0 is -ERF(-X), so the
|
||||
// sign is set aside first. The prover leaves one obligation open: that the
|
||||
// divisor 1.0 + 0.3275911 * A is not 0. It is at least 1, because A >= 0,
|
||||
// but A and the product are FLOAT values, and the prover does not see into
|
||||
// FLOAT arithmetic, so it cannot conclude it.
|
||||
FUNCTION ErfApprox(X : FLOAT) RETURNS FLOAT
|
||||
DECLARE Sign : FLOAT <- 1.0
|
||||
IF X < 0.0 THEN
|
||||
Sign <- -1.0
|
||||
ENDIF
|
||||
DECLARE A : FLOAT <- ABS(X)
|
||||
DECLARE T : FLOAT <- 1.0 / (1.0 + 0.3275911 * A)
|
||||
DECLARE Poly : FLOAT <- ((((1.061405429 * T - 1.453152027) * T + 1.421413741) * T - 0.284496736) * T + 0.254829592) * T
|
||||
RETURN Sign * (1.0 - Poly * EXP(-A * A))
|
||||
ENDFUNCTION
|
||||
Reference in new issue
Block a user