2015-03-18 11:31:47 -04:00
|
|
|
<?xml version="1.0" encoding="ISO-8859-1"?>
|
2015-03-18 13:32:24 -04:00
|
|
|
<!--
|
2019-02-07 13:23:09 -05:00
|
|
|
Copyright (C) 2014-2019 Ryan Specialty Group, LLC.
|
2015-03-18 13:32:24 -04:00
|
|
|
|
|
|
|
This file is part of tame-core.
|
|
|
|
|
|
|
|
tame-core is free software: you can redistribute it and/or modify it
|
2018-01-26 11:13:33 -05:00
|
|
|
under the terms of the GNU General Public License as
|
2015-03-18 13:32:24 -04:00
|
|
|
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/>.
|
|
|
|
-->
|
2015-03-18 11:31:47 -04:00
|
|
|
<package xmlns="http://www.lovullo.com/rater"
|
2013-02-13 09:27:33 -05:00
|
|
|
xmlns:c="http://www.lovullo.com/calc"
|
|
|
|
xmlns:t="http://www.lovullo.com/rater/apply-template"
|
|
|
|
core="true"
|
|
|
|
desc="Numeric computations dealing with rounding">
|
|
|
|
|
2015-03-18 11:31:47 -04:00
|
|
|
<import package="../base" />
|
2013-02-13 09:27:33 -05:00
|
|
|
|
|
|
|
|
|
|
|
<!-- template version of round function, supporting arbitrary nodes -->
|
2015-03-18 11:31:47 -04:00
|
|
|
<template name="_round_" desc="Round to the nearest integer">
|
|
|
|
<param name="@values@" desc="Calculation to round (single node)" />
|
|
|
|
<param name="@precision@" desc="Level of precision after decimal point">
|
|
|
|
<text>0</text>
|
|
|
|
</param>
|
2013-02-13 09:27:33 -05:00
|
|
|
|
|
|
|
<c:apply name="round_real" label="Rounded value">
|
|
|
|
<c:arg name="round_real_val">
|
|
|
|
<c:sum>
|
2015-03-18 11:31:47 -04:00
|
|
|
<param-copy name="@values@" />
|
2013-02-13 09:27:33 -05:00
|
|
|
</c:sum>
|
|
|
|
</c:arg>
|
|
|
|
|
|
|
|
<c:arg name="round_real_n">
|
|
|
|
<c:expt>
|
|
|
|
<c:const value="10" type="integer" desc="Decimal base" />
|
|
|
|
<c:const value="@precision@" type="integer" desc="Exponent" />
|
|
|
|
</c:expt>
|
|
|
|
</c:arg>
|
|
|
|
</c:apply>
|
2015-03-18 11:31:47 -04:00
|
|
|
</template>
|
2013-02-13 09:27:33 -05:00
|
|
|
|
|
|
|
|
|
|
|
<!--
|
|
|
|
Let expression exposing one or more of ceiling, floor, and nearest
|
|
|
|
integer of given name
|
|
|
|
|
|
|
|
This is our Swiss Army knife of rounding values.
|
|
|
|
|
|
|
|
The values @high@, @low@, and @nearest@ (can specify all or just one)
|
|
|
|
represent the names of the variables to store, respectively, the
|
|
|
|
ceiling, floor, and nearest integer to the value @name@. Those values
|
|
|
|
are then accessible within the scope of the let expression.
|
|
|
|
|
|
|
|
An @exp@ can be used to provide a base-10 exponent to move the decimal
|
|
|
|
place (negative is right, positive left) before performing rounding.
|
|
|
|
|
|
|
|
The @step@ allows specifying an arbitrary value to round toward. For
|
|
|
|
example, a @step@ of 1500 rounds in 1500 increments. This may be used
|
|
|
|
with @exp@, in which case the step will apply to @name@ after its value
|
|
|
|
has been exponentiated.
|
|
|
|
-->
|
2015-03-18 11:31:47 -04:00
|
|
|
<template name="_let-round_"
|
2013-02-13 09:27:33 -05:00
|
|
|
desc="Produce floor and ceiling values">
|
2015-03-18 11:31:47 -04:00
|
|
|
<param name="@values@"
|
2013-02-13 09:27:33 -05:00
|
|
|
desc="Calculation subject to let expression" />
|
|
|
|
|
2015-03-18 11:31:47 -04:00
|
|
|
<param name="@name@"
|
2013-02-13 09:27:33 -05:00
|
|
|
desc="Value to round" />
|
|
|
|
|
2015-03-18 11:31:47 -04:00
|
|
|
<param name="@index@"
|
2013-02-13 09:27:33 -05:00
|
|
|
desc="Optional value index">
|
2015-03-18 11:31:47 -04:00
|
|
|
<text></text>
|
|
|
|
</param>
|
2013-02-13 09:27:33 -05:00
|
|
|
|
2015-03-18 11:31:47 -04:00
|
|
|
<param name="@high@"
|
2013-02-13 09:27:33 -05:00
|
|
|
desc="Name to yield ceil'd value into" />
|
|
|
|
|
2015-03-18 11:31:47 -04:00
|
|
|
<param name="@low@"
|
2013-02-13 09:27:33 -05:00
|
|
|
desc="Name to yield floor'd value into" />
|
|
|
|
|
2015-03-18 11:31:47 -04:00
|
|
|
<param name="@nearest@"
|
2013-02-13 09:27:33 -05:00
|
|
|
desc="Name to yield nearest integer to value into" />
|
|
|
|
|
|
|
|
|
2015-03-18 11:31:47 -04:00
|
|
|
<param name="@exp@"
|
2013-02-13 09:27:33 -05:00
|
|
|
desc="Offset digits before rounding">
|
2015-03-18 11:31:47 -04:00
|
|
|
<text>#0</text>
|
|
|
|
</param>
|
2013-02-13 09:27:33 -05:00
|
|
|
|
2015-03-18 11:31:47 -04:00
|
|
|
<param name="@step@"
|
2013-02-13 09:27:33 -05:00
|
|
|
desc="Arbitrary step">
|
2015-03-18 11:31:47 -04:00
|
|
|
<text>#1</text>
|
|
|
|
</param>
|
2013-02-13 09:27:33 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<c:let>
|
|
|
|
<c:values>
|
|
|
|
<c:value name="__div"
|
|
|
|
type="float"
|
|
|
|
desc="Exponential/step divisor">
|
|
|
|
<c:product>
|
|
|
|
<c:expt>
|
|
|
|
<c:const value="10" type="integer"
|
|
|
|
desc="Decimal base" />
|
|
|
|
<c:value-of name="@exp@" />
|
|
|
|
</c:expt>
|
|
|
|
|
|
|
|
<c:value-of name="@step@" />
|
|
|
|
</c:product>
|
|
|
|
</c:value>
|
|
|
|
</c:values>
|
|
|
|
|
|
|
|
<c:let>
|
|
|
|
<c:values>
|
|
|
|
<c:value name="__adjusted"
|
|
|
|
type="float"
|
|
|
|
desc="Value adjusted for 10^@exp@ or step @step@">
|
|
|
|
<c:quotient>
|
|
|
|
<c:value-of name="@name@" index="@index@" />
|
|
|
|
<c:value-of name="__div" />
|
|
|
|
</c:quotient>
|
|
|
|
</c:value>
|
|
|
|
</c:values>
|
|
|
|
|
|
|
|
<c:let>
|
|
|
|
<c:values>
|
2015-03-18 11:31:47 -04:00
|
|
|
<if name="@high@">
|
2013-02-13 09:27:33 -05:00
|
|
|
<c:value name="@high@" type="float"
|
|
|
|
desc="Ceiling of adjusted {@name@}">
|
|
|
|
<c:product>
|
|
|
|
<c:ceil>
|
|
|
|
<c:value-of name="__adjusted" />
|
|
|
|
</c:ceil>
|
|
|
|
|
|
|
|
<c:value-of name="__div" />
|
|
|
|
</c:product>
|
|
|
|
</c:value>
|
2015-03-18 11:31:47 -04:00
|
|
|
</if>
|
2013-02-13 09:27:33 -05:00
|
|
|
|
2015-03-18 11:31:47 -04:00
|
|
|
<if name="@low@">
|
2013-02-13 09:27:33 -05:00
|
|
|
<c:value name="@low@" type="float"
|
|
|
|
desc="floor of adjusted {@name@}">
|
|
|
|
<c:product>
|
|
|
|
<c:floor>
|
|
|
|
<c:value-of name="__adjusted" />
|
|
|
|
</c:floor>
|
|
|
|
|
|
|
|
<c:value-of name="__div" />
|
|
|
|
</c:product>
|
|
|
|
</c:value>
|
2015-03-18 11:31:47 -04:00
|
|
|
</if>
|
2013-02-13 09:27:33 -05:00
|
|
|
|
2015-03-18 11:31:47 -04:00
|
|
|
<if name="@nearest@">
|
2013-02-13 09:27:33 -05:00
|
|
|
<c:value name="@nearest@" type="float"
|
|
|
|
desc="nearest integer to adjusted {@name@}">
|
|
|
|
<c:product>
|
|
|
|
<t:round>
|
|
|
|
<c:value-of name="__adjusted" />
|
|
|
|
</t:round>
|
|
|
|
|
|
|
|
<c:value-of name="__div" />
|
|
|
|
</c:product>
|
|
|
|
</c:value>
|
2015-03-18 11:31:47 -04:00
|
|
|
</if>
|
2013-02-13 09:27:33 -05:00
|
|
|
</c:values>
|
|
|
|
|
|
|
|
<!-- body subject to let expression -->
|
2015-03-18 11:31:47 -04:00
|
|
|
<param-copy name="@values@" />
|
2013-02-13 09:27:33 -05:00
|
|
|
</c:let>
|
|
|
|
</c:let>
|
|
|
|
</c:let>
|
2015-03-18 11:31:47 -04:00
|
|
|
</template>
|
2013-02-13 09:27:33 -05:00
|
|
|
|
|
|
|
|
|
|
|
<!-- ceil if > 0.5, otherwise floor; round() in most languages -->
|
|
|
|
<!-- TODO: support left and right delimiters; do \floor \ceil -->
|
2015-03-18 11:31:47 -04:00
|
|
|
<function name="round" desc="Round to the nearest integer">
|
|
|
|
<param name="roundval" type="float" desc="Value to round to nearest integer" />
|
2013-02-13 09:27:33 -05:00
|
|
|
|
|
|
|
<!-- By raising by 0.5 and taking the floor() of the resulting value, we
|
|
|
|
can produce values consistent with common round() implementations. It
|
|
|
|
may be easier to think of this as ceil( val - 0.4 ), but representing
|
|
|
|
subtraction is more verbose than addition, so a floor() implementation
|
|
|
|
is used instead. -->
|
|
|
|
<c:floor>
|
|
|
|
<c:sum>
|
|
|
|
<c:value-of name="roundval" />
|
|
|
|
<c:const value="0.5" type="float" desc="Raises value in a manner that it can be properly rounded by a floor" />
|
|
|
|
</c:sum>
|
|
|
|
</c:floor>
|
2015-03-18 11:31:47 -04:00
|
|
|
</function>
|
2013-02-13 09:27:33 -05:00
|
|
|
|
|
|
|
|
2015-03-18 11:31:47 -04:00
|
|
|
<function name="round_real" desc="Round to the nearest 1/n">
|
|
|
|
<param name="round_real_val" type="float" desc="Value to round" />
|
|
|
|
<param name="round_real_n" type="integer" desc="Round to 1/n" />
|
2013-02-13 09:27:33 -05:00
|
|
|
|
|
|
|
<!-- we can achieve this by multiplying by N, rounding, and then dividing
|
|
|
|
by the same N -->
|
|
|
|
<c:quotient>
|
|
|
|
<c:apply name="round">
|
|
|
|
<c:arg name="roundval">
|
|
|
|
<c:product>
|
|
|
|
<c:value-of name="round_real_val" />
|
|
|
|
<c:value-of name="round_real_n" />
|
|
|
|
</c:product>
|
|
|
|
</c:arg>
|
|
|
|
</c:apply>
|
|
|
|
|
|
|
|
<c:value-of name="round_real_n" />
|
|
|
|
</c:quotient>
|
2015-03-18 11:31:47 -04:00
|
|
|
</function>
|
2013-02-13 09:27:33 -05:00
|
|
|
|
|
|
|
|
|
|
|
<!-- this is simply a shorthand for round_real -->
|
2015-03-18 11:31:47 -04:00
|
|
|
<function name="round_cents" desc="Round to the nearest penny">
|
|
|
|
<param name="round_cents_val" type="float" desc="Monetary value" />
|
2013-02-13 09:27:33 -05:00
|
|
|
|
|
|
|
<c:apply name="round_real">
|
|
|
|
<c:arg name="round_real_n">
|
|
|
|
<c:const value="100" type="integer" desc="Round to the nearest 100th" />
|
|
|
|
</c:arg>
|
|
|
|
|
|
|
|
<c:arg name="round_real_val">
|
|
|
|
<c:value-of name="round_cents_val" />
|
|
|
|
</c:arg>
|
|
|
|
</c:apply>
|
2015-03-18 11:31:47 -04:00
|
|
|
</function>
|
2013-02-13 09:27:33 -05:00
|
|
|
|
|
|
|
|
2015-03-18 11:31:47 -04:00
|
|
|
<template name="_ceil-n_" desc="Ceiling on the last n digits">
|
|
|
|
<param name="@values@" desc="Calculation to use (single node)" />
|
|
|
|
<param name="@digits@" desc="Level of precision after decimal point" />
|
2013-02-13 09:27:33 -05:00
|
|
|
|
|
|
|
<c:product>
|
|
|
|
<c:ceil>
|
|
|
|
<c:quotient>
|
2015-03-18 11:31:47 -04:00
|
|
|
<param-copy name="@values@" />
|
2013-02-13 09:27:33 -05:00
|
|
|
<c:expt>
|
|
|
|
<c:const value="10" type="integer" desc="Decimal base" />
|
|
|
|
<c:const value="@digits@" type="integer" desc="Number of digits" />
|
|
|
|
</c:expt>
|
|
|
|
</c:quotient>
|
|
|
|
</c:ceil>
|
|
|
|
|
|
|
|
<c:expt>
|
|
|
|
<c:const value="10" type="integer" desc="Decimal base" />
|
|
|
|
<c:const value="@digits@" type="integer" desc="Number of digits" />
|
|
|
|
</c:expt>
|
|
|
|
</c:product>
|
2015-03-18 11:31:47 -04:00
|
|
|
</template>
|
|
|
|
</package>
|
2013-02-13 09:27:33 -05:00
|
|
|
|