Jump to content

Safe addition: Difference between revisions

Add Swift
(Scala solution added)
(Add Swift)
Line 711:
 
}</lang>
 
=={{header|Swift}}==
<lang swift>let a = 1.2
let b = 0.03
 
print("\(a) + \(b) is in the range \((a + b).nextDown)...\((a + b).nextUp)")</lang>
 
{{out}}
<pre>1.2 + 0.03 is in the range 1.2299999999999998...1.2300000000000002</pre>
 
=={{header|Tcl}}==
Tcl's floating point handling is done using IEEE double-precision floating point with the default rounding mode (i.e., to nearest representable value). This means that it is necessary to step away from the computed value slightly in both directions in order to compute a safe range. However, Tcl doesn't expose the <code>nextafter</code> function from <code>math.h</code> by default (necessary to do this right), so a little extra magic is needed.
Cookies help us deliver our services. By using our services, you agree to our use of cookies.