Add _rtop_ template

master
Mike Gerwitz 2015-11-02 15:48:09 -05:00 committed by Mike Gerwitz
parent ff59f1502f
commit d2d595a3d7
4 changed files with 237 additions and 1 deletions

View File

@ -413,4 +413,3 @@
</c:case>
</template>
</package>

View File

@ -0,0 +1,94 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Copyright (C) 2015 LoVullo Associates, Inc.
This file is part of tame-core.
tame-core is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<package xmlns="http://www.lovullo.com/rater"
xmlns:c="http://www.lovullo.com/calc"
xmlns:t="http://www.lovullo.com/rater/apply-template"
core="true"
desc="Percentage arithmetic">
<import package="../base" />
Converting between real and~percent is trivial:
$$
\begin{align}
r &amp;= p\over100;
p &amp;= 100r.
\end{align}
$$
^[Percent difference] (also called ^[relative change]) describes
the~change in value as a~percentage (for example: $10$~is $50%$
of~$20$, and is~$100%$ of~$5$). ^[Percent change] recognizes the
direction of the change (that is---$10$ is~$20$ reduced by~$50%$, so
$p=-50$).
Since ``change'' sounds like a~verb eclarative contexts, we use
the~term ^[percent difference] to mean ^[relative change]; it is
defined as:
$$\delta \over x = {{x_\beta - x_\alpha}\over{x_\alpha}} \times 100 = p.$$
Notice that this preserves the direction of the change. So, in the
context of the previous example, we can say that $10$
\emph{reduces}~$20$ by~$50%$, giving~$p=-50%$.
<template name="_rtop_"
desc="Rational number to percentage">
<param name="@name@" desc="Percentage to convert" />
<param name="@index@" desc="Index">
<text></text>
</param>
<param name="@desc@" desc="Optional description">
<text>Real number to convert into percentage</text>
</param>
<param name="@difference@"
desc="Whether to interpret as a percent difference (default
false)">
<text>false</text>
</param>
<!-- TODO -->
<param name="@negate@" desc="Whether to negate percentage before
conversion">
<text>false</text>
</param>
<c:product>
<c:sum>
<c:value-of name="@name@" index="@index@" />
<if name="@difference@" eq="true">
<c:const value="-1"
desc="Percent difference is relative (1 = 100%)" />
</if>
</c:sum>
<c:const value="100"
desc="Real to percentage" />
</c:product>
</template>
</package>

View File

@ -0,0 +1,142 @@
<?xml version="1.0"?>
<!--
Copyright (C) 2015 LoVullo Associates, Inc.
This file is part of tame-core.
tame-core is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<package xmlns="http://www.lovullo.com/rater"
xmlns:c="http://www.lovullo.com/calc"
xmlns:t="http://www.lovullo.com/rater/apply-template"
desc="Test percentage arithemetic">
<import package="../../spec" />
<import package="../../../base" />
<import package="../../../numeric/percent" />
<t:describe name="_rtop_">
<t:describe name="given only a percentage">
<t:it desc="converts real into whole percentage">
<t:given>
<t:rtop name="#0.05" />
</t:given>
<t:expect>
<t:match-result>
<c:eq>
<c:value-of name="#5" />
</c:eq>
</t:match-result>
</t:expect>
</t:it>
<t:it desc="converts integer into whole percentage">
<t:given>
<t:rtop name="#5" />
</t:given>
<t:expect>
<t:match-result>
<c:eq>
<c:value-of name="#500" />
</c:eq>
</t:match-result>
</t:expect>
</t:it>
<t:it desc="converts negative into negative percentage">
<t:given>
<t:rtop name="#-0.5" />
</t:given>
<t:expect>
<t:match-result>
<c:eq>
<c:value-of name="#-50" />
</c:eq>
</t:match-result>
</t:expect>
</t:it>
</t:describe>
<t:describe name="given a percent difference">
These tests are similar to the above, except that they represent
the~percent difference between two values. That is, given the
values $5$~and $15$, we arrive at a~$200%$ increase. So
if~$p=200$, then we want $5r=15, r=3=\((15-5)/5\)+1$.
<t:it desc="converts R into percentage P that will increase N
by P%">
<t:given>
<t:rtop name="#3" difference="true" />
</t:given>
<t:expect>
<t:match-result>
<c:eq>
<c:value-of name="#200" />
</c:eq>
</t:match-result>
</t:expect>
</t:it>
Similarly, if we go from~$10$ to~$5$, we have~$p=-100$
and~$10r=5$; then~$r=1/2=0.5$.
<t:it desc="converts negative N into percentage P that will
decrease N by P%">
<t:given>
<t:rtop name="#0.5" difference="true" />
</t:given>
<t:expect>
<t:match-result>
<c:eq>
<c:value-of name="#-50" />
</c:eq>
</t:match-result>
</t:expect>
</t:it>
</t:describe>
<t:describe name="given a value index">
<const name="PERCENT_REAL_VECTOR"
desc="Test vector">
<item value="3" desc="Real number" />
</const>
<t:it desc="converts the scalar value at the given index">
<t:given>
<t:rtop name="PERCENT_REAL_VECTOR" index="#0" />
</t:given>
<t:expect>
<t:match-result>
<c:eq>
<c:value-of name="#300" />
</c:eq>
</t:match-result>
</t:expect>
</t:it>
</t:describe>
</t:describe>
</package>

View File

@ -28,6 +28,7 @@
<import package="numeric/round" />
<import package="numeric/convert" />
<import package="numeric/percent" />
<import package="vector/interpolate" />