Price fraction: Difference between revisions
Content added Content deleted
(→{{header|Julia}}: A new entry for Julia) |
|||
Line 1,512: | Line 1,512: | ||
0.11 -> 0.26 |
0.11 -> 0.26 |
||
...</pre> |
...</pre> |
||
=={{header|Julia}}== |
|||
This solution is somewhat straightforward but does highlight a couple of Julia features. The interval cut-offs and values are exactly represented by rational numbers. The interval to which an input value belongs is identified by applying the <code>findfirst</code> (true value) function to an element-wise comparison (<code>.<</code>) of this value to the cut-off array. |
|||
<lang Julia> |
|||
const PFCUT = [6:5:101]//100 |
|||
const PFVAL = [10:8:26, 32:6:50, 54:4:98, 100]//100 |
|||
function pricefraction{T<:FloatingPoint}(a::T) |
|||
zero(T) <= a || error("a = ", a, ", but it must be >= 0.") |
|||
a <= one(T) || error("a = ", a, ", but it must be <= 1.") |
|||
convert(T, PFVAL[findfirst(a .< PFCUT)]) |
|||
end |
|||
test = [0.:0.05:1., 0.51, 0.56, 0.61, rand(), rand(), rand(), rand()] |
|||
println("Testing the price fraction function") |
|||
for t in test |
|||
println(@sprintf " %.4f -> %.4f" t pricefraction(t)) |
|||
end |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
Testing the price fraction function |
|||
0.0000 -> 0.1000 |
|||
0.0500 -> 0.1000 |
|||
0.1000 -> 0.1800 |
|||
0.1500 -> 0.2600 |
|||
0.2000 -> 0.3200 |
|||
0.2500 -> 0.3800 |
|||
0.3000 -> 0.4400 |
|||
0.3500 -> 0.5000 |
|||
0.4000 -> 0.5400 |
|||
0.4500 -> 0.5800 |
|||
0.5000 -> 0.6200 |
|||
0.5500 -> 0.6600 |
|||
0.6000 -> 0.7000 |
|||
0.6500 -> 0.7400 |
|||
0.7000 -> 0.7800 |
|||
0.7500 -> 0.8200 |
|||
0.8000 -> 0.8600 |
|||
0.8500 -> 0.9000 |
|||
0.9000 -> 0.9400 |
|||
0.9500 -> 0.9800 |
|||
1.0000 -> 1.0000 |
|||
0.5100 -> 0.6600 |
|||
0.5600 -> 0.7000 |
|||
0.6100 -> 0.7400 |
|||
0.5603 -> 0.7000 |
|||
0.9812 -> 1.0000 |
|||
0.5127 -> 0.6600 |
|||
0.4821 -> 0.6200 |
|||
</pre> |
|||
=={{header|K}}== |
=={{header|K}}== |