Price fraction: Difference between revisions

Content added Content deleted
m (→‎version 1: removed the overflow from the style option.)
m ({{out}})
Line 73: Line 73:
end Test_Price_Fraction;
end Test_Price_Fraction;
</lang>
</lang>
{{out}}
Sample output:
<div style="height: 200px;overflow:scroll">
<div style="height: 200px;overflow:scroll">
<pre>
<pre>
Line 253: Line 253:
printf(($"Value : "z.2dl,"Converted to standard : "z.2dl$, price, std val))
printf(($"Value : "z.2dl,"Converted to standard : "z.2dl$, price, std val))
)</lang>
)</lang>
{{out}}
Sample Output:
<pre>
<pre>
Value : 0.38
Value : 0.38
Line 347: Line 347:
END FUNCTION</lang>
END FUNCTION</lang>


Sample outputs (run 5 times):
{{out}} (run 5 times):
.7388727 .82
.7388727 .82
.8593103 .9
.8593103 .9
Line 419: Line 419:
)
)
)</lang>
)</lang>
{{out}}
Output:
<pre>0.00 --> 0.10
<pre>0.00 --> 0.10
0.01 --> 0.10
0.01 --> 0.10
Line 576: Line 576:
</lang>
</lang>


{{out}}
<pre>Sample output:
<pre>
Enter a fractional number between 0 and 1 ( 0 to end )!
Enter a fractional number between 0 and 1 ( 0 to end )!
0.7
0.7
Line 674: Line 675:
Return nResult</lang>
Return nResult</lang>


{{out}}
Output:

<pre style="height:30ex;overflow:scroll"> -0.02 -> NIL 0.00 -> 0.10
<pre style="height:30ex;overflow:scroll"> -0.02 -> NIL 0.00 -> 0.10
0.01 -> 0.10 0.03 -> 0.10
0.01 -> 0.10 0.03 -> 0.10
Line 988: Line 988:


end program price_fraction</lang>
end program price_fraction</lang>
{{out}}
Sample output:
<lang>0.997560 1.00
<lang>0.997560 1.00
0.566825 0.70
0.566825 0.70
Line 1,043: Line 1,043:
println "$v --> ${priceFraction(v)}"
println "$v --> ${priceFraction(v)}"
}</lang>
}</lang>
{{out}}
Output:
<div style="height: 200px;overflow:scroll">
<div style="height: 200px;overflow:scroll">
<pre>0.00 --> 0.10
<pre>0.00 --> 0.10
Line 1,249: Line 1,249:
</lang>
</lang>


{{out}}
Output:
<pre>
<pre>
0.0 rescaled is 0.1
0.0 rescaled is 0.1
Line 1,356: Line 1,356:
}
}
}</lang>
}</lang>
{{out}}
Output:
<pre>0.149969 -> 0.26
<pre>0.149969 -> 0.26
0.310605 -> 0.50
0.310605 -> 0.50
Line 1,366: Line 1,366:
=={{header|JavaScript}}==
=={{header|JavaScript}}==


In the task definition, the first step is 0.06, the rest are 0.05 so a re-factoring can subtract 0.01 from the value and divide by 0.05 to get the step.
In the task definition, the first step is 0.06, the rest are 0.05
so a re-factoring can subtract 0.01 from the value and divide by 0.05 to get the step.


Working with decimal numbers in JavaScript has issues, e.g. 0.06 - 0.01 = 0.049999999999999996 due to using IEEE 754 double precision numbers that can't accurately represent all decimals. So values are multiplied by 100 and integer arithmetic is used.
Working with decimal numbers in JavaScript has issues, e.g. 0.06 - 0.01 = 0.049999999999999996 due to using IEEE 754 double precision numbers that can't accurately represent all decimals. So values are multiplied by 100 and integer arithmetic is used.
Line 1,395: Line 1,396:
(range(10;100) | "0.\(.) -> \( 0.01 * . | getScaleFactor)");
(range(10;100) | "0.\(.) -> \( 0.01 * . | getScaleFactor)");


test</lang>
test</lang>Run the test, showing the first few lines of output:
Run the test, showing the first few lines of output:
<lang sh>
<pre>
$ jq -n -r -f Price_fraction.jq
$ jq -n -r -f Price_fraction.jq
0.00 -> 1.00
0.00 -> 1.00
Line 1,410: Line 1,412:
0.10 -> 0.18
0.10 -> 0.18
0.11 -> 0.26
0.11 -> 0.26
...</lang>
...</pre>


=={{header|K}}==
=={{header|K}}==
Line 1,421: Line 1,423:
pf:{out@_bin[le;-x]}'
pf:{out@_bin[le;-x]}'
</lang>
</lang>
{{out}}
Output
<lang K>
<pre>
pf 0.6094701 0.5003597 0.8512954 0.08951883 0.6868076
pf 0.6094701 0.5003597 0.8512954 0.08951883 0.6868076
0.7 0.62 0.9 0.18 0.78
0.7 0.62 0.9 0.18 0.78
</lang>
</pre>


=={{header|Liberty BASIC}}==
=={{header|Liberty BASIC}}==
Line 1,540: Line 1,542:
KILL PFMAX,PFRES,I
KILL PFMAX,PFRES,I
QUIT RESULT</lang>
QUIT RESULT</lang>
{{out}}
Output:<pre>USER>W $$PRICFRAC^ROSETTA(.04)
<pre>USER>W $$PRICFRAC^ROSETTA(.04)
.10
.10
USER>W $$PRICFRAC^ROSETTA(.06)
USER>W $$PRICFRAC^ROSETTA(.06)
Line 1,706: Line 1,709:


=={{header|Oz}}==
=={{header|Oz}}==
Using a for-loop with return and a default value for values >= 1.01. For out-of-range input, a "failed value" is returned, i.e. a value that throws an exception when it is accessed.
Using a for-loop with return and a default value for values >= 1.01.
For out-of-range input, a "failed value" is returned,
i.e. a value that throws an exception when it is accessed.


<lang oz>fun {PriceFraction X}
<lang oz>fun {PriceFraction X}
Line 1,761: Line 1,766:
end;
end;
end.</lang>
end.</lang>
{{out}}
Output:
<pre>% ./PriceFraction
<pre>% ./PriceFraction
0.8145 -> 0.90
0.8145 -> 0.90
Line 1,849: Line 1,854:
fail "Out of range";
fail "Out of range";
}</lang>
}</lang>
Perhaps a better approach is just to build an array of 101 entries. Memory is cheap, and array lookup is blazing fast, especially important if used in a loop as below. Moreover, in Perl&nbsp;6 we don't have to worry about floating point misrepresentations of decimals because decimal fractions are stored as rationals.
Perhaps a better approach is just to build an array of 101 entries.
Memory is cheap, and array lookup is blazing fast, especially important if used in a loop as below.
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,
<lang perl6>my @price = map *.value,
Line 1,941: Line 1,948:
(for N (0.3793 0.4425 0.0746 0.6918 0.2993 0.5486 0.7848 0.9383 0.2292)
(for N (0.3793 0.4425 0.0746 0.6918 0.2993 0.5486 0.7848 0.9383 0.2292)
(prinl (price N)) )</lang>
(prinl (price N)) )</lang>
{{out}}
Output:
<pre>0.54
<pre>0.54
0.58
0.58
Line 2,051: Line 2,058:
CloseConsole()
CloseConsole()
EndIf</lang>
EndIf</lang>
{{out}}
Sample output:
<pre>0.3793 -> 0.54
<pre>0.3793 -> 0.54
0.4425 -> 0.58
0.4425 -> 0.58
Line 2,219: Line 2,226:
end /*select*/
end /*select*/
return ?</lang>
return ?</lang>
{{out}}
'''output'''
<pre style="height:30ex">
<pre style="height:30ex">
original price ──► 0.00 0.10 ◄── adjusted price
original price ──► 0.00 0.10 ◄── adjusted price
Line 2,458: Line 2,465:
end</lang>
end</lang>


{{out}}
output
<pre>Loaded suite price_fraction
<pre>Loaded suite price_fraction
Started
Started
Line 2,566: Line 2,573:
def testPriceFraction()=
def testPriceFraction()=
for(n <- 0.00 to (1.00, 0.01)) println("%.2f %.2f".format(n, priceFraction(n)))</lang>
for(n <- 0.00 to (1.00, 0.01)) println("%.2f %.2f".format(n, priceFraction(n)))</lang>
{{out}}
Output
<pre>
<pre>
0,00 0,10
0,00 0,10
Line 2,765: Line 2,772:
puts "$example -> [priceFraction $map $example]"
puts "$example -> [priceFraction $map $example]"
}</lang>
}</lang>
{{out}}
Output:
<pre>
<pre>
.7388727 -> 0.82
.7388727 -> 0.82
Line 2,793: Line 2,800:
test = price_fraction* <0.34,0.070145,0.06,0.05,0.50214,0.56,1.,0.99,0.>
test = price_fraction* <0.34,0.070145,0.06,0.05,0.50214,0.56,1.,0.99,0.>
</lang>
</lang>
{{out}}
output:
<pre><
<pre><
5.000000e-01,
5.000000e-01,
Line 2,840: Line 2,847:
]</lang>
]</lang>


{{out}}
Output:
<pre>
<pre>
0.18
0.18