Jump to content

Price fraction: Difference between revisions

m
{{out}}
m (→‎version 1: removed the overflow from the style option.)
m ({{out}})
Line 73:
end Test_Price_Fraction;
</lang>
{{out}}
Sample output:
<div style="height: 200px;overflow:scroll">
<pre>
Line 253:
printf(($"Value : "z.2dl,"Converted to standard : "z.2dl$, price, std val))
)</lang>
{{out}}
Sample Output:
<pre>
Value : 0.38
Line 347:
END FUNCTION</lang>
 
Sample outputs{{out}} (run 5 times):
.7388727 .82
.8593103 .9
Line 419:
)
)</lang>
{{out}}
Output:
<pre>0.00 --> 0.10
0.01 --> 0.10
Line 576:
</lang>
 
{{out}}
<pre>Sample output:
<pre>
Enter a fractional number between 0 and 1 ( 0 to end )!
0.7
Line 674 ⟶ 675:
Return nResult</lang>
 
{{out}}
Output:
 
<pre style="height:30ex;overflow:scroll"> -0.02 -> NIL 0.00 -> 0.10
0.01 -> 0.10 0.03 -> 0.10
Line 988:
 
end program price_fraction</lang>
{{out}}
Sample output:
<lang>0.997560 1.00
0.566825 0.70
Line 1,043:
println "$v --> ${priceFraction(v)}"
}</lang>
{{out}}
Output:
<div style="height: 200px;overflow:scroll">
<pre>0.00 --> 0.10
Line 1,249:
</lang>
 
{{out}}
Output:
<pre>
0.0 rescaled is 0.1
Line 1,356:
}
}</lang>
{{out}}
Output:
<pre>0.149969 -> 0.26
0.310605 -> 0.50
Line 1,366:
=={{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.
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.
Line 1,395 ⟶ 1,396:
(range(10;100) | "0.\(.) -> \( 0.01 * . | getScaleFactor)");
 
test</lang sh>
test</lang>Run the test, showing the first few lines of output:
<lang sh>
<pre>
$ jq -n -r -f Price_fraction.jq
0.00 -> 1.00
Line 1,410 ⟶ 1,412:
0.10 -> 0.18
0.11 -> 0.26
...</langpre>
 
=={{header|K}}==
Line 1,421 ⟶ 1,423:
pf:{out@_bin[le;-x]}'
</lang>
{{out}}
Output
<lang Kpre>
pf 0.6094701 0.5003597 0.8512954 0.08951883 0.6868076
0.7 0.62 0.9 0.18 0.78
</langpre>
 
=={{header|Liberty BASIC}}==
Line 1,540 ⟶ 1,542:
KILL PFMAX,PFRES,I
QUIT RESULT</lang>
{{out}}
Output:<pre>USER>W $$PRICFRAC^ROSETTA(.04)
.10
USER>W $$PRICFRAC^ROSETTA(.06)
Line 1,706 ⟶ 1,709:
 
=={{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.
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}
Line 1,761 ⟶ 1,766:
end;
end.</lang>
{{out}}
Output:
<pre>% ./PriceFraction
0.8145 -> 0.90
Line 1,849 ⟶ 1,854:
fail "Out of range";
}</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.
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,
Line 1,941 ⟶ 1,948:
(for N (0.3793 0.4425 0.0746 0.6918 0.2993 0.5486 0.7848 0.9383 0.2292)
(prinl (price N)) )</lang>
{{out}}
Output:
<pre>0.54
0.58
Line 2,051 ⟶ 2,058:
CloseConsole()
EndIf</lang>
{{out}}
Sample output:
<pre>0.3793 -> 0.54
0.4425 -> 0.58
Line 2,219 ⟶ 2,226:
end /*select*/
return ?</lang>
{{out}}
'''output'''
<pre style="height:30ex">
original price ──► 0.00 0.10 ◄── adjusted price
Line 2,458 ⟶ 2,465:
end</lang>
 
{{out}}
output
<pre>Loaded suite price_fraction
Started
Line 2,566 ⟶ 2,573:
def testPriceFraction()=
for(n <- 0.00 to (1.00, 0.01)) println("%.2f %.2f".format(n, priceFraction(n)))</lang>
{{out}}
Output
<pre>
0,00 0,10
Line 2,765 ⟶ 2,772:
puts "$example -> [priceFraction $map $example]"
}</lang>
{{out}}
Output:
<pre>
.7388727 -> 0.82
Line 2,793 ⟶ 2,800:
test = price_fraction* <0.34,0.070145,0.06,0.05,0.50214,0.56,1.,0.99,0.>
</lang>
{{out}}
output:
<pre><
5.000000e-01,
Line 2,840 ⟶ 2,847:
]</lang>
 
{{out}}
Output:
<pre>
0.18
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.