Approximate equality: Difference between revisions

Added R.
(Added R.)
Line 960:
3.141592653589793 is approximately equal to 3.141592653589793
</pre>
=={{header|R}}==
 
The base library has the function all.equal() for this task. However, when the numbers are not equal, rather than return FALSE, it tries to explain the difference. To fix this, we use isTRUE(all.equal(....)) instead.
<lang r>approxEq<-function(...) isTRUE(all.equal(...))
tests<-rbind(c(100000000000000.01, 100000000000000.011),
c(100.01, 100.011),
c(10000000000000.001 / 10000.0, 1000000000.0000001000),
c(0.001, 0.0010000001),
c(0.000000000000000000000101, 0.0),
c(sqrt(2) * sqrt(2), 2.0),
c(-sqrt(2) * sqrt(2), -2.0),
c(3.14159265358979323846, 3.14159265358979324))
results<-mapply(approxEq, tests[,1], tests[,2])
#All that remains is to print out our results in a presentable way:
printableTests<-format(tests, scientific = FALSE)
print(data.frame(x = printableTests[,1], y = printableTests[,2], Equal = results, row.names = paste0("Test ", 1:8, ": ")))</lang>
{{out}}
<pre> x y Equal
Test 1: 100000000000000.015625000000000000000000 100000000000000.015625000000000000000000 TRUE
Test 2: 100.010000000000005115907697 100.010999999999995679900167 FALSE
Test 3: 1000000000.000000238418579101562500 1000000000.000000119209289550781250 TRUE
Test 4: 0.001000000000000000020817 0.001000000100000000054917 FALSE
Test 5: 0.000000000000000000000101 0.000000000000000000000000 TRUE
Test 6: 2.000000000000000444089210 2.000000000000000000000000 TRUE
Test 7: -2.000000000000000444089210 -2.000000000000000000000000 TRUE
Test 8: 3.141592653589793115997963 3.141592653589793115997963 TRUE</pre>
=={{header|Racket}}==
 
331

edits