How to resolve the algorithm Safe addition step by step in the E programming language

Published on 12 May 2024 09:40 PM
#E

How to resolve the algorithm Safe addition step by step in the E programming language

Table of Contents

Problem Statement

Implementation of   interval arithmetic   and more generally fuzzy number arithmetic require operations that yield safe upper and lower bounds of the exact result. For example, for an addition, it is the operations   +↑   and   +↓   defined as:   a +↓ b ≤ a + b ≤ a +↑ b. Additionally it is desired that the width of the interval   (a +↑ b) - (a +↓ b)   would be about the machine epsilon after removing the exponent part. Differently to the standard floating-point arithmetic, safe interval arithmetic is accurate (but still imprecise). I.E.:   the result of each defined operation contains (though does not identify) the exact mathematical outcome. Usually a   FPU's   have machine   +,-,*,/   operations accurate within the machine precision. To illustrate it, let us consider a machine with decimal floating-point arithmetic that has the precision is 3 decimal points. If the result of the machine addition is   1.23,   then the exact mathematical result is within the interval   ]1.22, 1.24[. When the machine rounds towards zero, then the exact result is within   [1.23,1.24[.   This is the basis for an implementation of safe addition.

Show how   +↓   and   +↑   can be implemented in your language using the standard floating-point type. Define an interval type based on the standard floating-point one,   and implement an interval-valued addition of two floating-point numbers considering them exact, in short an operation that yields the interval   [a +↓ b, a +↑ b].

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Safe addition step by step in the E programming language

Source code in the e programming language

def makeInterval(a :float64, b :float64) {
    require(a <= b)
    def interval {
        to least() { return a }
        to greatest() { return b }
        to __printOn(out) {
            out.print("[", a, ", ", b, "]")
        }
        to add(other) {
            require(a <=> b)
            require(other.least() <=> other.greatest())
            def result := a + other.least()
            return makeInterval(result.previous(), result.next())
        }
    }
    return interval
}

? makeInterval(1.14, 1.14) + makeInterval(2000.0, 2000.0)
# value: [2001.1399999999999, 2001.1400000000003]

  

You may also check:How to resolve the algorithm Abbreviations, automatic step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Chinese zodiac step by step in the Nim programming language
You may also check:How to resolve the algorithm Hailstone sequence step by step in the Rust programming language
You may also check:How to resolve the algorithm Enumerations step by step in the Metafont programming language
You may also check:How to resolve the algorithm Binary digits step by step in the MATLAB / Octave programming language