Price fraction: Difference between revisions
Content added Content deleted
Langurmonkey (talk | contribs) m (→{{header|langur}}: corrected end of range comparison) |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 183: | Line 183: | ||
</pre> |
</pre> |
||
</div> |
</div> |
||
=={{header|AutoHotkey}}== |
|||
<lang AutoHotkey>; Submitted by MasterFocus --- http://tiny.cc/iTunis |
|||
Loop |
|||
{ |
|||
InputBox, OutputVar, Price Fraction Example, Insert the value to be rounded.`n* [ 0 < value < 1 ]`n* Press ESC or Cancel to exit, , 200, 150 |
|||
If ErrorLevel |
|||
Break |
|||
MsgBox % "Input: " OutputVar "`nResult: " PriceFraction( OutputVar ) |
|||
} |
|||
;----------------------------------------- |
|||
PriceFraction( p_Input ) |
|||
{ |
|||
If p_Input is not float ; returns 0 if input is not a float |
|||
Return 0 |
|||
If ( ( p_Input <= 0 ) OR ( p_Input >= 1 ) ) ; returns 0 is input is out of range |
|||
Return 0 |
|||
; declaring the table (arbitrary delimiters in use are '§' and '|') |
|||
l_List := "0.06|0.10§0.11|0.18§0.16|0.26§0.21|0.32§0.26|0.38§0.31|0.44§0.36|0.50§0.41|0.54§0.46|0.58§0.51|0.62§0.56|0.66§0.61|0.70§0.66|0.74§0.71|0.78§0.76|0.82§0.81|0.86§0.86|0.90§0.91|0.94§0.96|0.98§1.01|1.00" |
|||
Loop, Parse, l_List, § ; retrieves each field (delimited by '§') |
|||
{ |
|||
StringSplit, l_Array, A_LoopField, | ; splits current field (using delimiter '|') |
|||
If ( p_Input <= l_Array1 ) |
|||
Return l_Array2 ; returns the second value if input <= first value |
|||
} |
|||
Return 0 ; returns 0, indicating failure (shouldn't be reached though) |
|||
}</lang> |
|||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
Line 262: | Line 226: | ||
Converted to standard : 0.54 |
Converted to standard : 0.54 |
||
</pre> |
</pre> |
||
=={{header|AutoHotkey}}== |
|||
<lang AutoHotkey>; Submitted by MasterFocus --- http://tiny.cc/iTunis |
|||
Loop |
|||
{ |
|||
InputBox, OutputVar, Price Fraction Example, Insert the value to be rounded.`n* [ 0 < value < 1 ]`n* Press ESC or Cancel to exit, , 200, 150 |
|||
If ErrorLevel |
|||
Break |
|||
MsgBox % "Input: " OutputVar "`nResult: " PriceFraction( OutputVar ) |
|||
} |
|||
;----------------------------------------- |
|||
PriceFraction( p_Input ) |
|||
{ |
|||
If p_Input is not float ; returns 0 if input is not a float |
|||
Return 0 |
|||
If ( ( p_Input <= 0 ) OR ( p_Input >= 1 ) ) ; returns 0 is input is out of range |
|||
Return 0 |
|||
; declaring the table (arbitrary delimiters in use are '§' and '|') |
|||
l_List := "0.06|0.10§0.11|0.18§0.16|0.26§0.21|0.32§0.26|0.38§0.31|0.44§0.36|0.50§0.41|0.54§0.46|0.58§0.51|0.62§0.56|0.66§0.61|0.70§0.66|0.74§0.71|0.78§0.76|0.82§0.81|0.86§0.86|0.90§0.91|0.94§0.96|0.98§1.01|1.00" |
|||
Loop, Parse, l_List, § ; retrieves each field (delimited by '§') |
|||
{ |
|||
StringSplit, l_Array, A_LoopField, | ; splits current field (using delimiter '|') |
|||
If ( p_Input <= l_Array1 ) |
|||
Return l_Array2 ; returns the second value if input <= first value |
|||
} |
|||
Return 0 ; returns 0, indicating failure (shouldn't be reached though) |
|||
}</lang> |
|||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
Line 1,045: | Line 1,045: | ||
printf(1, "%.2f %.2f\n", { i/100, price_fix(i/100) }) |
printf(1, "%.2f %.2f\n", { i/100, price_fix(i/100) }) |
||
end for</lang> |
end for</lang> |
||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
Line 1,330: | Line 1,329: | ||
0.96 = 1.00 0.97 = 1.00 0.98 = 1.00 0.99 = 1.00 1.00 = 1.00 |
0.96 = 1.00 0.97 = 1.00 0.98 = 1.00 0.99 = 1.00 1.00 = 1.00 |
||
</pre> |
</pre> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
<lang go>func pf(v float64) float64 { |
<lang go>func pf(v float64) float64 { |
||
Line 1,693: | Line 1,693: | ||
0.194047 -> 0.32 |
0.194047 -> 0.32 |
||
0.724852 -> 0.82</pre> |
0.724852 -> 0.82</pre> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
Line 2,010: | Line 2,009: | ||
{.70, x < .61}, {.74, x < .66}, {.78, x < .71}, {.82, x < .76}, |
{.70, x < .61}, {.74, x < .66}, {.78, x < .71}, {.82, x < .76}, |
||
{.86, x < .81}, {.90, x < .86}, {.94, x < .91}, {.98, x < .96}}, 1]</lang> |
{.86, x < .81}, {.90, x < .86}, {.94, x < .91}, {.98, x < .96}}, 1]</lang> |
||
=={{header|MATLAB}} / {{header|Octave}}== |
=={{header|MATLAB}} / {{header|Octave}}== |
||
Line 2,427: | Line 2,425: | ||
} |
} |
||
</lang> |
</lang> |
||
=={{header|Perl 6}}== |
|||
Simple solution, doing a linear search.<br> |
|||
Note that in Perl 6 we don't have to worry about floating-point misrepresentations of decimals, because decimal fractions are stored as rationals. |
|||
{{works with|rakudo|2016.07}} |
|||
<lang perl6>sub price-fraction ($n where 0..1) { |
|||
when $n < 0.06 { 0.10 } |
|||
when $n < 0.11 { 0.18 } |
|||
when $n < 0.16 { 0.26 } |
|||
when $n < 0.21 { 0.32 } |
|||
when $n < 0.26 { 0.38 } |
|||
when $n < 0.31 { 0.44 } |
|||
when $n < 0.36 { 0.50 } |
|||
when $n < 0.41 { 0.54 } |
|||
when $n < 0.46 { 0.58 } |
|||
when $n < 0.51 { 0.62 } |
|||
when $n < 0.56 { 0.66 } |
|||
when $n < 0.61 { 0.70 } |
|||
when $n < 0.66 { 0.74 } |
|||
when $n < 0.71 { 0.78 } |
|||
when $n < 0.76 { 0.82 } |
|||
when $n < 0.81 { 0.86 } |
|||
when $n < 0.86 { 0.90 } |
|||
when $n < 0.91 { 0.94 } |
|||
when $n < 0.96 { 0.98 } |
|||
default { 1.00 } |
|||
} |
|||
while prompt("value: ") -> $value { |
|||
say price-fraction(+$value); |
|||
}</lang> |
|||
If we expect to rescale many prices, a better approach would be to build a look-up array of 101 entries. |
|||
Memory is cheap, and array indexing is blazing fast. |
|||
<lang perl6>my @price = map *.value, flat |
|||
( 0 ..^ 6 X=> 0.10), |
|||
( 6 ..^ 11 X=> 0.18), |
|||
(11 ..^ 16 X=> 0.26), |
|||
(16 ..^ 21 X=> 0.32), |
|||
(21 ..^ 26 X=> 0.38), |
|||
(26 ..^ 31 X=> 0.44), |
|||
(31 ..^ 36 X=> 0.50), |
|||
(36 ..^ 41 X=> 0.54), |
|||
(41 ..^ 46 X=> 0.58), |
|||
(46 ..^ 51 X=> 0.62), |
|||
(51 ..^ 56 X=> 0.66), |
|||
(56 ..^ 61 X=> 0.70), |
|||
(61 ..^ 66 X=> 0.74), |
|||
(66 ..^ 71 X=> 0.78), |
|||
(71 ..^ 76 X=> 0.82), |
|||
(76 ..^ 81 X=> 0.86), |
|||
(81 ..^ 86 X=> 0.90), |
|||
(86 ..^ 91 X=> 0.94), |
|||
(91 ..^ 96 X=> 0.98), |
|||
(96 ..^101 X=> 1.00), |
|||
; |
|||
while prompt("value: ") -> $value { |
|||
say @price[$value * 100] // "Out of range"; |
|||
}</lang> |
|||
We can also build this same look-up array by parsing the table as formatted in the task description: |
|||
{{works with|rakudo|2016.07}} |
|||
<lang perl6>my $table = q:to/END/; |
|||
>= 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 |
|||
END |
|||
my @price; |
|||
for $table.lines { |
|||
/:s '>=' (\S+) '<' (\S+) ':=' (\S+)/; |
|||
@price[$0*100 ..^ $1*100] »=» +$2; |
|||
} |
|||
while prompt("value: ") -> $value { |
|||
say @price[$value * 100] // "Out of range"; |
|||
}</lang> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 2,885: | Line 2,783: | ||
(define (convert x) (for/or ([c table]) (and (< x (car c)) (cadr c)))) |
(define (convert x) (for/or ([c table]) (and (< x (car c)) (cadr c)))) |
||
</lang> |
</lang> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
Simple solution, doing a linear search.<br> |
|||
Note that in Perl 6 we don't have to worry about floating-point misrepresentations of decimals, because decimal fractions are stored as rationals. |
|||
{{works with|rakudo|2016.07}} |
|||
<lang perl6>sub price-fraction ($n where 0..1) { |
|||
when $n < 0.06 { 0.10 } |
|||
when $n < 0.11 { 0.18 } |
|||
when $n < 0.16 { 0.26 } |
|||
when $n < 0.21 { 0.32 } |
|||
when $n < 0.26 { 0.38 } |
|||
when $n < 0.31 { 0.44 } |
|||
when $n < 0.36 { 0.50 } |
|||
when $n < 0.41 { 0.54 } |
|||
when $n < 0.46 { 0.58 } |
|||
when $n < 0.51 { 0.62 } |
|||
when $n < 0.56 { 0.66 } |
|||
when $n < 0.61 { 0.70 } |
|||
when $n < 0.66 { 0.74 } |
|||
when $n < 0.71 { 0.78 } |
|||
when $n < 0.76 { 0.82 } |
|||
when $n < 0.81 { 0.86 } |
|||
when $n < 0.86 { 0.90 } |
|||
when $n < 0.91 { 0.94 } |
|||
when $n < 0.96 { 0.98 } |
|||
default { 1.00 } |
|||
} |
|||
while prompt("value: ") -> $value { |
|||
say price-fraction(+$value); |
|||
}</lang> |
|||
If we expect to rescale many prices, a better approach would be to build a look-up array of 101 entries. |
|||
Memory is cheap, and array indexing is blazing fast. |
|||
<lang perl6>my @price = map *.value, flat |
|||
( 0 ..^ 6 X=> 0.10), |
|||
( 6 ..^ 11 X=> 0.18), |
|||
(11 ..^ 16 X=> 0.26), |
|||
(16 ..^ 21 X=> 0.32), |
|||
(21 ..^ 26 X=> 0.38), |
|||
(26 ..^ 31 X=> 0.44), |
|||
(31 ..^ 36 X=> 0.50), |
|||
(36 ..^ 41 X=> 0.54), |
|||
(41 ..^ 46 X=> 0.58), |
|||
(46 ..^ 51 X=> 0.62), |
|||
(51 ..^ 56 X=> 0.66), |
|||
(56 ..^ 61 X=> 0.70), |
|||
(61 ..^ 66 X=> 0.74), |
|||
(66 ..^ 71 X=> 0.78), |
|||
(71 ..^ 76 X=> 0.82), |
|||
(76 ..^ 81 X=> 0.86), |
|||
(81 ..^ 86 X=> 0.90), |
|||
(86 ..^ 91 X=> 0.94), |
|||
(91 ..^ 96 X=> 0.98), |
|||
(96 ..^101 X=> 1.00), |
|||
; |
|||
while prompt("value: ") -> $value { |
|||
say @price[$value * 100] // "Out of range"; |
|||
}</lang> |
|||
We can also build this same look-up array by parsing the table as formatted in the task description: |
|||
{{works with|rakudo|2016.07}} |
|||
<lang perl6>my $table = q:to/END/; |
|||
>= 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 |
|||
END |
|||
my @price; |
|||
for $table.lines { |
|||
/:s '>=' (\S+) '<' (\S+) ':=' (\S+)/; |
|||
@price[$0*100 ..^ $1*100] »=» +$2; |
|||
} |
|||
while prompt("value: ") -> $value { |
|||
say @price[$value * 100] // "Out of range"; |
|||
}</lang> |
|||
=={{header|Raven}}== |
=={{header|Raven}}== |