Price fraction: Difference between revisions

(→‎{{header|Ruby}}: add bsearch example)
 
(11 intermediate revisions by 7 users not shown)
Line 1,547:
 
=={{header|Bracmat}}==
Bracmat has no native support for floating point variables nor for the fixed point values in the conversion table. Instead thisThis solution just applies a string comparison.
<syntaxhighlight lang="bracmat">( ( convert
=
Line 1,965:
0.9
0.5</pre>
 
=={{header|Dart}}==
{{trans|Swift}}
<syntaxhighlight lang="Dart">
class Range {
final double start;
final double end;
 
Range(this.start, this.end);
 
bool contains(double value) {
return value >= start && value < end;
}
}
 
List<MapEntry<Range, double>> ranges = [
MapEntry(Range(0.00, 0.06), 0.10),
MapEntry(Range(0.06, 0.11), 0.18),
MapEntry(Range(0.11, 0.16), 0.26),
MapEntry(Range(0.16, 0.21), 0.32),
MapEntry(Range(0.21, 0.26), 0.38),
MapEntry(Range(0.26, 0.31), 0.44),
MapEntry(Range(0.31, 0.36), 0.50),
MapEntry(Range(0.36, 0.41), 0.54),
MapEntry(Range(0.41, 0.46), 0.58),
MapEntry(Range(0.46, 0.51), 0.62),
MapEntry(Range(0.51, 0.56), 0.66),
MapEntry(Range(0.56, 0.61), 0.70),
MapEntry(Range(0.61, 0.66), 0.74),
MapEntry(Range(0.66, 0.71), 0.78),
MapEntry(Range(0.71, 0.76), 0.82),
MapEntry(Range(0.76, 0.81), 0.86),
MapEntry(Range(0.81, 0.86), 0.90),
MapEntry(Range(0.86, 0.91), 0.94),
MapEntry(Range(0.91, 0.96), 0.98),
MapEntry(Range(0.96, 1.01), 1.00),
];
 
double adjustDouble(double val, List<MapEntry<Range, double>> ranges) {
for (var range in ranges) {
if (range.key.contains(val)) {
return range.value;
}
}
return val; // Return the original value if no range is found
}
 
void main() {
for (double val = 0.0; val <= 1.0; val += 0.01) {
String strFmt(double n) => n.toStringAsFixed(2);
 
double adjusted = adjustDouble(val, ranges);
print("${strFmt(val)} -> ${strFmt(adjusted)}");
}
}
</syntaxhighlight>
{{out}}
<pre style="overflow: scroll; height: 20em">
0.00 -> 0.10
0.01 -> 0.10
0.02 -> 0.10
0.03 -> 0.10
0.04 -> 0.10
0.05 -> 0.10
0.06 -> 0.18
0.07 -> 0.18
0.08 -> 0.18
0.09 -> 0.18
0.10 -> 0.18
0.11 -> 0.18
0.12 -> 0.26
0.13 -> 0.26
0.14 -> 0.26
0.15 -> 0.26
0.16 -> 0.32
0.17 -> 0.32
0.18 -> 0.32
0.19 -> 0.32
0.20 -> 0.32
0.21 -> 0.38
0.22 -> 0.38
0.23 -> 0.38
0.24 -> 0.38
0.25 -> 0.38
0.26 -> 0.44
0.27 -> 0.44
0.28 -> 0.44
0.29 -> 0.44
0.30 -> 0.44
0.31 -> 0.50
0.32 -> 0.50
0.33 -> 0.50
0.34 -> 0.50
0.35 -> 0.50
0.36 -> 0.54
0.37 -> 0.54
0.38 -> 0.54
0.39 -> 0.54
0.40 -> 0.54
0.41 -> 0.58
0.42 -> 0.58
0.43 -> 0.58
0.44 -> 0.58
0.45 -> 0.58
0.46 -> 0.62
0.47 -> 0.62
0.48 -> 0.62
0.49 -> 0.62
0.50 -> 0.62
0.51 -> 0.66
0.52 -> 0.66
0.53 -> 0.66
0.54 -> 0.66
0.55 -> 0.66
0.56 -> 0.70
0.57 -> 0.70
0.58 -> 0.70
0.59 -> 0.70
0.60 -> 0.70
0.61 -> 0.74
0.62 -> 0.74
0.63 -> 0.74
0.64 -> 0.74
0.65 -> 0.74
0.66 -> 0.78
0.67 -> 0.78
0.68 -> 0.78
0.69 -> 0.78
0.70 -> 0.78
0.71 -> 0.82
0.72 -> 0.82
0.73 -> 0.82
0.74 -> 0.82
0.75 -> 0.82
0.76 -> 0.86
0.77 -> 0.86
0.78 -> 0.86
0.79 -> 0.86
0.80 -> 0.86
0.81 -> 0.90
0.82 -> 0.90
0.83 -> 0.90
0.84 -> 0.90
0.85 -> 0.90
0.86 -> 0.94
0.87 -> 0.94
0.88 -> 0.94
0.89 -> 0.94
0.90 -> 0.94
0.91 -> 0.98
0.92 -> 0.98
0.93 -> 0.98
0.94 -> 0.98
0.95 -> 0.98
0.96 -> 1.00
0.97 -> 1.00
0.98 -> 1.00
0.99 -> 1.00
 
</pre>
 
=={{header|Delphi}}==
See [https://rosettacode.org/wiki/Price_fraction#Pascal Pascal].
 
=={{header|EasyLang}}==
<syntaxhighlight lang=easylang>
n[] = [ 10 18 26 32 38 44 50 54 58 62 66 70 74 78 82 86 90 94 98 100 ]
func conv p .
cat = (p - 1) div 5 + 1
return n[cat]
.
for in = 5 step 5 to 100
if in = 100
in$ = "1.00"
elif in < 10
in$ = "0.0" & in
else
in$ = "0." & in
.
out = conv in
if out = 100
out$ = "1.00"
else
out$ = "0." & out
.
print in$ & " -> " & out$
.
</syntaxhighlight>
{{out}}
<pre>
0.05 -> 0.10
0.10 -> 0.18
0.15 -> 0.26
0.20 -> 0.32
0.25 -> 0.38
0.30 -> 0.44
0.35 -> 0.50
0.40 -> 0.54
0.45 -> 0.58
0.50 -> 0.62
0.55 -> 0.66
0.60 -> 0.70
0.65 -> 0.74
0.70 -> 0.78
0.75 -> 0.82
0.80 -> 0.86
0.85 -> 0.90
0.90 -> 0.94
0.95 -> 0.98
1.00 -> 1.00
</pre>
 
=={{header|Eiffel}}==
Line 2,068 ⟶ 2,276:
Given: 0.95 Adjusted:0.98
</pre>
 
 
=={{header|Elixir}}==
Line 2,369 ⟶ 2,576:
0.347081 0.50
0.342244 0.50</syntaxhighlight>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn PriceFraction( price as double ) as double
double result
if price < 0.00 or price > 1.00 then exit fn = price
if price < 0.06 then exit fn = 0.10
if price < 0.11 then exit fn = 0.18
if price < 0.16 then exit fn = 0.26
if price < 0.21 then exit fn = 0.32
if price < 0.26 then exit fn = 0.38
if price < 0.31 then exit fn = 0.44
if price < 0.36 then exit fn = 0.50
if price < 0.41 then exit fn = 0.54
if price < 0.46 then exit fn = 0.58
if price < 0.51 then exit fn = 0.62
if price < 0.56 then exit fn = 0.66
if price < 0.61 then exit fn = 0.70
if price < 0.66 then exit fn = 0.74
if price < 0.71 then exit fn = 0.78
if price < 0.76 then exit fn = 0.82
if price < 0.81 then exit fn = 0.86
if price < 0.86 then exit fn = 0.90
if price < 0.91 then exit fn = 0.94
if price < 0.96 then exit fn = 0.98
result = 1.00
end fn = result
 
void local fn GetPriceFractions
NSUInteger i
for i = 1 to 100
double d = i/100.0
printf @"%.2f -> %.2f\t\b", d, fn PriceFraction( d )
if i mod 5 == 0 then print
next
end fn
 
fn GetPriceFractions
 
NSLog( @"%@", fn WindowPrintViewString( 1 ) )
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
0.01 -> 0.10 0.02 -> 0.10 0.03 -> 0.10 0.04 -> 0.10 0.05 -> 0.10
0.06 -> 0.18 0.07 -> 0.18 0.08 -> 0.18 0.09 -> 0.18 0.10 -> 0.18
0.11 -> 0.26 0.12 -> 0.26 0.13 -> 0.26 0.14 -> 0.26 0.15 -> 0.26
0.16 -> 0.32 0.17 -> 0.32 0.18 -> 0.32 0.19 -> 0.32 0.20 -> 0.32
0.21 -> 0.38 0.22 -> 0.38 0.23 -> 0.38 0.24 -> 0.38 0.25 -> 0.38
0.26 -> 0.44 0.27 -> 0.44 0.28 -> 0.44 0.29 -> 0.44 0.30 -> 0.44
0.31 -> 0.50 0.32 -> 0.50 0.33 -> 0.50 0.34 -> 0.50 0.35 -> 0.50
0.36 -> 0.54 0.37 -> 0.54 0.38 -> 0.54 0.39 -> 0.54 0.40 -> 0.54
0.41 -> 0.58 0.42 -> 0.58 0.43 -> 0.58 0.44 -> 0.58 0.45 -> 0.58
0.46 -> 0.62 0.47 -> 0.62 0.48 -> 0.62 0.49 -> 0.62 0.50 -> 0.62
0.51 -> 0.66 0.52 -> 0.66 0.53 -> 0.66 0.54 -> 0.66 0.55 -> 0.66
0.56 -> 0.70 0.57 -> 0.70 0.58 -> 0.70 0.59 -> 0.70 0.60 -> 0.70
0.61 -> 0.74 0.62 -> 0.74 0.63 -> 0.74 0.64 -> 0.74 0.65 -> 0.74
0.66 -> 0.78 0.67 -> 0.78 0.68 -> 0.78 0.69 -> 0.78 0.70 -> 0.78
0.71 -> 0.82 0.72 -> 0.82 0.73 -> 0.82 0.74 -> 0.82 0.75 -> 0.82
0.76 -> 0.86 0.77 -> 0.86 0.78 -> 0.86 0.79 -> 0.86 0.80 -> 0.86
0.81 -> 0.90 0.82 -> 0.90 0.83 -> 0.90 0.84 -> 0.90 0.85 -> 0.90
0.86 -> 0.94 0.87 -> 0.94 0.88 -> 0.94 0.89 -> 0.94 0.90 -> 0.94
0.91 -> 0.98 0.92 -> 0.98 0.93 -> 0.98 0.94 -> 0.98 0.95 -> 0.98
0.96 -> 1.00 0.97 -> 1.00 0.98 -> 1.00 0.99 -> 1.00 1.00 -> 1.00
</pre>
 
 
 
=={{header|Go}}==
Line 2,959 ⟶ 3,236:
Langur uses decimal floating point.
 
<syntaxhighlight lang="langur">#val using.pricefrac an= impliedfn(.f) parameter{ switch[and] .f ...{
val .pricefrac = f given .f {
case >= 0.00, < 0.06: 0.10
case >= 0.06, < 0.11: 0.18
Line 2,982 ⟶ 3,258:
case >= 0.96, <= 1.00: 1.00
default: throw "bad data"
}}
 
# The default operator between test cases is "and".
# That is, writing "case" without a logical operator is the same as writing "case and".
# To make a given case act as a switch case does in other languages, use "case or".
}
 
writeln .pricefrac(0.17)
Line 3,033 ⟶ 3,305:
Random value: 0.36528313938816
Adjusted price: 0.54</pre>
 
=={{header|M2000 Interpreter}}==
Derived from BASIC
 
<syntaxhighlight lang="m2000 interpreter">
Module PriceFraction {
Currency i
Print $("0.00"),
for i=0@ to 1@ step .10@
Print i, @PriceFraction(i)
next
Print $(""),
function PriceFraction(price as currency)
select case price
case < 0
= price
case < .06
= .1
case < .11
= .18
case < .16
= .26
case < .21
= .32
case < .26
= .38
case < .31
= .44
case < .36
= .5
case < .41
= .54
case < .46
= .58
case < .51
= .62
case < .56
= .66
case < .61
= .7
case < .66
= .74
case < .71
= .78
case < .76
= .82
case < .81
= .86
case < .86
= .9
case < .91
= .94
case < .96
= .98
case < 1.01
= 1!
case else
= price
end select
end function
}
PriceFraction
</syntaxhighlight>
{{out}}
<pre>
0.00 0.10
0.10 0.18
0.20 0.32
0.30 0.44
0.40 0.54
0.50 0.62
0.60 0.70
0.70 0.78
0.80 0.86
0.90 0.94
1.00 1.00
</pre>
 
 
 
=={{header|Maple}}==
Line 3,132 ⟶ 3,484:
 
<syntaxhighlight lang="mercury">adjust(Cents) = array.lookup(price_table, Cents).</syntaxhighlight>
 
 
=={{header|MUMPS}}==
Line 5,030 ⟶ 5,383:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var rescale = Fn.new { |v|
885

edits