Numeric error propagation: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→{{header|Perl 6}}: flatten component lists) |
|||
Line 1,655: | Line 1,655: | ||
covariance between 300±2.46 and -350±4.56: -9.68</pre> |
covariance between 300±2.46 and -350±4.56: -9.68</pre> |
||
=={{header|Phix}}== |
|||
<lang Phix>enum VALUE, DELTA |
|||
type imprecise(object imp) |
|||
return sequence(imp) and atom(imp[VALUE]) and atom(imp[DELTA]) |
|||
end type |
|||
function sqr(atom a) |
|||
return a*a |
|||
end function |
|||
function imprecise_add(imprecise a, b) |
|||
atom delta = sqrt(sqr(a[DELTA]) + sqr(b[DELTA])) |
|||
imprecise ret = {a[VALUE] + b[VALUE], delta} |
|||
return ret |
|||
end function |
|||
function imprecise_mul(imprecise a, b) |
|||
atom delta = sqrt(sqr(a[VALUE]*b[DELTA]) + sqr(b[VALUE]*a[DELTA])) |
|||
imprecise ret = {a[VALUE] * b[VALUE],delta} |
|||
return ret |
|||
end function |
|||
function imprecise_div(imprecise a, b) |
|||
atom delta = sqrt(sqr(a[VALUE]*b[DELTA]) + sqr(b[VALUE]*a[DELTA]))/sqr(b[VALUE]) |
|||
imprecise ret = {a[VALUE] / b[VALUE], delta} |
|||
return ret |
|||
end function |
|||
function imprecise_pow(imprecise a, atom c) |
|||
atom v = power(a[VALUE], c), |
|||
delta = abs(v*c*a[DELTA]/a[VALUE]) |
|||
imprecise ret = {v,delta} |
|||
return ret |
|||
end function |
|||
function printImprecise(imprecise imp) |
|||
return sprintf("%g+/-%g",imp) |
|||
end function |
|||
imprecise x1 = {100, 1.1}, |
|||
y1 = {50, 1.2}, |
|||
x2 = {-200, 2.2}, |
|||
y2 = {-100, 2.3}, |
|||
tmp1, tmp2, |
|||
d |
|||
tmp1 = imprecise_add(x1, x2) |
|||
tmp1 = imprecise_pow(tmp1, 2) |
|||
tmp2 = imprecise_add(y1, y2) |
|||
tmp2 = imprecise_pow(tmp2, 2) |
|||
d = imprecise_add(tmp1,tmp2) |
|||
d = imprecise_pow(d, 0.5) |
|||
printf(1,"Distance, d, between the following points :") |
|||
printf(1,"\n( x1, y1) = ( %s, %s)",{printImprecise(x1),printImprecise(y1)}) |
|||
printf(1,"\n( x2, y2) = ( %s, %s)",{printImprecise(x2),printImprecise(y2)}) |
|||
printf(1,"\nis d = %s\n", {printImprecise(d)})</lang> |
|||
Aside: obviously you don't ''have'' to use tmp1/2 like that, but I find that style often makes things much easier to debug. |
|||
{{out}} |
|||
<pre> |
|||
Distance, d, between the following points : |
|||
( x1, y1) = ( 100+/-1.1, 50+/-1.2) |
|||
( x2, y2) = ( -200+/-2.2, -100+/-2.3) |
|||
is d = 111.803+/-2.48717 |
|||
</pre> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |