Price fraction: Difference between revisions

Content added Content deleted
m (→‎{{header|Sidef}}: minor code fixe)
Line 2,128: Line 2,128:
say price_fraction(+$value);
say price_fraction(+$value);
}</lang>
}</lang>

=={{header|Phix}}==
<lang Phix>constant TBL=split("""
>= 0.00 < 0.06 := 0.10
>= 0.06 < 0.11 := 0.18
>= 0.11 < 0.16 := 0.26
>= 0.16 < 0.21 := 0.32
>= 0.21 < 0.26 := 0.38
>= 0.26 < 0.31 := 0.44
>= 0.31 < 0.36 := 0.50
>= 0.36 < 0.41 := 0.54
>= 0.41 < 0.46 := 0.58
>= 0.46 < 0.51 := 0.62
>= 0.51 < 0.56 := 0.66
>= 0.56 < 0.61 := 0.70
>= 0.61 < 0.66 := 0.74
>= 0.66 < 0.71 := 0.78
>= 0.71 < 0.76 := 0.82
>= 0.76 < 0.81 := 0.86
>= 0.81 < 0.86 := 0.90
>= 0.86 < 0.91 := 0.94
>= 0.91 < 0.96 := 0.98
>= 0.96 < 1.01 := 1.00""",'\n')

sequence limits = {0},
prices = {-1}
atom lt,price
for i=1 to length(TBL) do
{{?,lt,price}} = scanf(TBL[i],">= %.2f < %.2f := %.2f")
limits = append(limits,lt)
prices = append(prices,price)
end for

function price_fix(atom p)
for i=1 to length(limits) do
-- if p<limits[i] then -- see note below
if p-limits[i]<-0.005 then
return prices[i]
end if
end for
return -1
end function
for i=-1 to 101 do
printf(1, "%5.2f %5.2f\n", {i/100,price_fix(i/100)})
end for</lang>
Comparing standard IEEE floats is always dodgy. I suspect that several other examples on this page exhibit the same bug that this change fixed.
In my case I was getting three bugs (0.21->0.32, 0.41->0.54, 0.86->0.90), but it probably depends on how floats are parsed and then stored.

The output, with most lines manually removed, is:
<pre>
-0.01 -1.00
0.00 0.10
0.01 0.10
0.05 0.10
0.06 0.18
0.10 0.18
0.11 0.26
0.20 0.32
0.21 0.38
0.41 0.58
0.86 0.94
0.95 0.98
0.96 1.00
1.00 1.00
1.01 -1.00
</pre>


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==