Anonymous user
First-class functions/Use numbers analogously: Difference between revisions
First-class functions/Use numbers analogously (view source)
Revision as of 23:12, 9 March 2016
, 8 years ago→{{header|Groovy}}: new solution
m (Fixed a tag omission) |
(→{{header|Groovy}}: new solution) |
||
Line 559:
return 0 // never reached
}</lang>
=={{header|Groovy}}==
<lang groovy>def multiplier = { n1, n2 -> { m -> n1 * n2 * m } }
def ε = 0.00000001 // tolerance(epsilon): acceptable level of "wrongness" to account for rounding error
[(2.0):0.5, (4.0):0.25, (6.0):(1/6.0)].each { num, inv ->
def new_function = multiplier(num, inv)
(1.0..5.0).each { trial ->
assert (new_function(trial) - trial).abs() < ε
printf('%5.3f * %5.3f * %5.3f == %5.3f\n', num, inv, trial, trial)
}
println()
}</lang>
{{out}}
<pre>2.000 * 0.500 * 1.000 == 1.000
2.000 * 0.500 * 2.000 == 2.000
2.000 * 0.500 * 3.000 == 3.000
2.000 * 0.500 * 4.000 == 4.000
2.000 * 0.500 * 5.000 == 5.000
4.000 * 0.250 * 1.000 == 1.000
4.000 * 0.250 * 2.000 == 2.000
4.000 * 0.250 * 3.000 == 3.000
4.000 * 0.250 * 4.000 == 4.000
4.000 * 0.250 * 5.000 == 5.000
6.000 * 0.167 * 1.000 == 1.000
6.000 * 0.167 * 2.000 == 2.000
6.000 * 0.167 * 3.000 == 3.000
6.000 * 0.167 * 4.000 == 4.000
6.000 * 0.167 * 5.000 == 5.000</pre>
=={{header|Haskell}}==
|