Price fraction: Difference between revisions
Content added Content deleted
(→{{header|Matlab}} / {{header|Octave}}: Price fraction - rescale) |
|||
Line 495: | Line 495: | ||
END IF |
END IF |
||
RETURN nResult</lang> |
RETURN nResult</lang> |
||
The function above crashes with an array access bound error if the value passed is negative. |
|||
Also, the spec. indicates that 0.00 should be replaced with standard value 0.10, not 0. |
|||
The following is a more concise solution: |
|||
<lang Clipper>Procedure Main() |
|||
Local i |
|||
For i := -0.02 to 1.02 STEP 0.03 |
|||
? i, "->", PriceFraction(i), i+0.02, "->", PriceFraction(i+0.02) |
|||
Next |
|||
Return |
|||
Static Function PriceFraction( nValue ) |
|||
Local nResult |
|||
Local n |
|||
// Function is only defined for values 0 to 1.00 |
|||
// Return NIL for anything else |
|||
// Table of values {V1, V2} = {Threshhold, Standard value} |
|||
#define TV_THRESHHOLD 1 |
|||
#define TV_STD_VALUE 2 |
|||
Local aTable := { {0, NIL },; |
|||
{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} } |
|||
n := AScan( aTable, {|x| nValue < x[TV_THRESHHOLD] }) |
|||
If n > 0 |
|||
nResult := aTable[n][TV_STD_VALUE] |
|||
Else |
|||
nResult := NIL |
|||
Endif |
|||
Return nResult</lang> |
|||
Sample output: |
|||
<pre> -0.02 -> NIL 0.00 -> 0.10 |
|||
0.01 -> 0.10 0.03 -> 0.10 |
|||
0.04 -> 0.10 0.06 -> 0.18 |
|||
0.07 -> 0.18 0.09 -> 0.18 |
|||
0.10 -> 0.18 0.12 -> 0.26 |
|||
0.13 -> 0.26 0.15 -> 0.26 |
|||
0.16 -> 0.32 0.18 -> 0.32 |
|||
0.19 -> 0.32 0.21 -> 0.38 |
|||
0.22 -> 0.38 0.24 -> 0.38 |
|||
0.25 -> 0.38 0.27 -> 0.44 |
|||
0.28 -> 0.44 0.30 -> 0.44 |
|||
0.31 -> 0.50 0.33 -> 0.50 |
|||
0.34 -> 0.50 0.36 -> 0.54 |
|||
0.37 -> 0.54 0.39 -> 0.54 |
|||
0.40 -> 0.54 0.42 -> 0.58 |
|||
0.43 -> 0.58 0.45 -> 0.58 |
|||
0.46 -> 0.62 0.48 -> 0.62 |
|||
0.49 -> 0.62 0.51 -> 0.66 |
|||
0.52 -> 0.66 0.54 -> 0.66 |
|||
0.55 -> 0.66 0.57 -> 0.70 |
|||
0.58 -> 0.70 0.60 -> 0.70 |
|||
0.61 -> 0.74 0.63 -> 0.74 |
|||
0.64 -> 0.74 0.66 -> 0.78 |
|||
0.67 -> 0.78 0.69 -> 0.78 |
|||
0.70 -> 0.78 0.72 -> 0.82 |
|||
0.73 -> 0.82 0.75 -> 0.82 |
|||
0.76 -> 0.86 0.78 -> 0.86 |
|||
0.79 -> 0.86 0.81 -> 0.90 |
|||
0.82 -> 0.90 0.84 -> 0.90 |
|||
0.85 -> 0.90 0.87 -> 0.94 |
|||
0.88 -> 0.94 0.90 -> 0.94 |
|||
0.91 -> 0.98 0.93 -> 0.98 |
|||
0.94 -> 0.98 0.96 -> 1.00 |
|||
0.97 -> 1.00 0.99 -> 1.00 |
|||
1.00 -> 1.00 1.02 -> NIL</pre> |
|||
=={{header|D}}== |
=={{header|D}}== |
||
<lang d>import std.stdio: writeln; |
<lang d>import std.stdio: writeln; |