Price fraction: Difference between revisions

Added F# version
m (Added Nimrod code)
(Added F# version)
Line 784:
printf(1, "%.2f %.2f\n", { i/100, price_fix(i/100) })
end for</lang>
 
 
=={{header|F_Sharp|F#}}==
Inspired by Python's bisect solution. Using decimal (System.Decimal) to avoid number representation problems with floats.
<lang fsharp>let cin = [ 0.06m .. 0.05m ..1.01m ]
let cout = [0.1m; 0.18m] @ [0.26m .. 0.06m .. 0.44m] @ [0.50m .. 0.04m .. 0.98m] @ [1.m]
 
let priceadjuster p =
let rec bisect lo hi =
if lo < hi then
let mid = (lo+hi)/2.
let left = p < cin.[int mid]
bisect (if left then lo else mid+1.) (if left then mid else hi)
else lo
 
if p < 0.m || 1.m < p then p
else cout.[int (bisect 0. (float cin.Length))]
 
[ 0.m .. 0.01m .. 1.m ]
|> Seq.ofList
|> Seq.iter (fun p -> printfn "%.2f -> %.2f" p (priceadjuster p))</lang>
{{out}}
The same as shown by Ada as of 2013-11-03T17:42Z (apart from whitespace formatting)
 
=={{header|Fantom}}==
Anonymous user