Price fraction: Difference between revisions

m (Added the Sidef language)
Line 2,030:
 
=={{header|Perl 6}}==
{{works with|rakudo|2015-10-20}}
<lang perl6>my $table = "q:to/END/;
>= 0.00 < 0.06 := 0.10
>= 0.06 < 0.11 := 0.18
Line 2,051 ⟶ 2,052:
>= 0.91 < 0.96 := 0.98
>= 0.96 < 1.01 := 1.00
END
";
 
my $value = 0.44;
Line 2,069 ⟶ 2,070:
Moreover, in Perl&nbsp;6 we don't have to worry about floating point misrepresentations of decimals because decimal fractions are stored as rationals.
 
<lang perl6>my @price = map *.value, flat
( 0 ..^ 6 X=> 0.10),
( 6 ..^ 11 X=> 0.18),
Line 2,097 ⟶ 2,098:
 
Yet another approach is to use the conditional operator to encode the table.
This allows each endpoint to be written once, avoiding duplication. The <tt>Rat()</tt>
type in the signature coerces any numeric type to a rational.
<lang perl6>sub price_fraction ( NumRat() $n where { $^n >= 0 and $^n <= 1 } ) {
( $n < 0.06 ) ?? 0.10
!! ( $n < 0.11 ) ?? 0.18
Anonymous user