Numeric error propagation: Difference between revisions

no edit summary
(→‎{{header|Fortran}}: The relative error is interesting also...)
No edit summary
Line 1,216:
{{out}}
<pre>111.80339887498948±2.4871670631463423</pre>
 
=={{header|Julia}}==
<lang julia>
module NumericError
 
import Base: convert, promote_rule, +, -, *, /, ^
 
export Measure
 
type Measure <: Number
x::Float64
σ::Float64
Measure(x::Real) = new(Float64(x), 0)
Measure(x::Real, σ::Real) = new(Float64(x), σ)
end
 
Base.show(io::IO, x::Measure) = print(io, string(x.x, " ± ", x.σ))
 
Base.convert(::Type{Measure}, x::Real) = Measure(Float64(x), 0.0)
Base.promote_rule(::Type{Float64}, ::Type{Measure}) = Measure
Base.promote_rule(::Type{Int64}, ::Type{Measure}) = Measure
 
+(a::Measure, b::Measure) = Measure(a.x + b.x, sqrt(a.σ ^ 2 + b.σ ^ 2))
-(a::Measure, b::Measure) = Measure(a.x - b.x, sqrt(a.σ ^ 2 + b.σ ^ 2))
-(a::Measure) = Measure(-a.x, a.σ)
 
*(a::Measure, b::Measure) = begin
x = a.x * b.x
σ = sqrt(x ^ 2 * ((a.σ / a.x) ^ 2 + (b.σ / b.x) ^ 2))
Measure(x, σ)
end
/(a::Measure, b::Measure) = begin
x = a.x / b.x
σ = sqrt(x ^ 2 * ((a.σ / a.x) ^ 2 + (b.σ / b.x) ^ 2))
Measure(x, σ)
end
 
^(a::Measure, b::Float64) = begin
x = a.x ^ b
σ = abs(x * b * a.σ / a.x)
Measure(x, σ)
end
 
Base.sqrt(a::Measure) = a ^ .5
 
end # module NumericError
 
using NumericError
 
# x1 = 100 ± 1.1
# y1 = 50 ± 1.2
# x2 = 200 ± 2.2
# y2 = 100 ± 2.3
 
x1 = Measure(100, 1.1)
y1 = Measure(50, 1.2)
x2 = Measure(200, 2.2)
y2 = Measure(100, 2.3)
 
d = sqrt((x1 - x2) ^ 2 + (y1 - y2) ^ 2)
 
@show x1 y1 x2 y2 d
</lang>
 
{{out}}
<pre>
x1 = 100.0 ± 1.1
y1 = 50.0 ± 1.2
x2 = 200.0 ± 2.2
y2 = 100.0 ± 2.3
d = 111.80339887498948 ± 1.7586926962946086
</pre>
 
=={{header|Kotlin}}==
Anonymous user