Safe addition: Difference between revisions

Go solution
(Go solution)
Line 291:
E provides only 64-bit "double precision" floats, and always prints them with sufficient decimal places to reproduce the original floating point number exactly.
 
=={{header|Go}}==
{{trans|Tcl}}
<lang go>package main
 
import (
"fmt"
"math"
)
 
type interval struct {
lower, upper float64
}
 
func safeAdd(a, b float64) interval {
s := a + b
return interval{
math.Nextafter(s, math.Inf(-1)),
math.Nextafter(s, math.Inf(1))}
}
 
func main() {
a, b := 1.2, .03
fmt.Println(a, b, safeAdd(a, b))
}</lang>
Output:
<pre>
1.2 0.03 {1.2299999999999998 1.2300000000000002}
</pre>
=={{header|J}}==
J uses 64 bit IEEE floating points, providing 53 binary digits of accuracy.
1,707

edits