Numeric error propagation: Difference between revisions
Content deleted Content added
Kotlin version |
Kotlin: fields renamed, new constructor |
||
Line 751: | Line 751: | ||
<lang scala>import java.lang.Math.* |
<lang scala>import java.lang.Math.* |
||
data class Approx(val |
data class Approx(val ν: Double, val σ: Double = 0.0) { |
||
constructor(a: Approx) : this(a. |
constructor(a: Approx) : this(a.ν, a.σ) |
||
constructor(n: Number) : this(n.toDouble(), 0.0) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
operator infix fun |
operator infix fun plus(a: Approx) = Approx(ν + a.ν, sqrt(σ * σ + a.σ * a.σ)) |
||
operator infix fun |
operator infix fun plus(d: Double) = Approx(ν + d, σ) |
||
⚫ | |||
⚫ | |||
operator infix fun times(a: Approx): Approx { |
operator infix fun times(a: Approx): Approx { |
||
val v = |
val v = ν * a.ν |
||
return Approx(v, sqrt(v * v * |
return Approx(v, sqrt(v * v * σ * σ / (ν * ν) + a.σ * a.σ / (a.ν * a.ν))) |
||
} |
} |
||
operator infix fun times(d: Double) = Approx( |
operator infix fun times(d: Double) = Approx(ν * d, abs(d * σ)) |
||
operator infix fun div(a: Approx): Approx { |
operator infix fun div(a: Approx): Approx { |
||
val v = |
val v = ν / a.ν |
||
return Approx(v, sqrt(v * v * |
return Approx(v, sqrt(v * v * σ * σ / (ν * ν) + a.σ * a.σ / (a.ν * a.ν))) |
||
} |
} |
||
operator infix fun div(d: Double) = Approx( |
operator infix fun div(d: Double) = Approx(ν / d, abs(d * σ)) |
||
fun pow(d: Double): Approx { |
fun pow(d: Double): Approx { |
||
val v = pow( |
val v = pow(ν, d) |
||
return Approx(v, abs(v * d * |
return Approx(v, abs(v * d * σ / ν)) |
||
} |
} |
||
⚫ | |||
} |
} |
||