Skip to content

Commit

Permalink
Merge pull request #1671 from michaelhkay/1261-decimal-divide
Browse files Browse the repository at this point in the history
1261 New fn:divide-decimals() function
  • Loading branch information
ndw authored Jan 7, 2025
2 parents 8b743fb + c1caca6 commit bf9f234
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
96 changes: 96 additions & 0 deletions specifications/xpath-functions-40/src/function-catalog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2315,6 +2315,102 @@ compare($N * $arg2, 0) eq compare($arg1, 0).</eg>
</fos:example>
</fos:examples>
</fos:function>


<fos:function name="divide-decimals" prefix="fn">
<fos:signatures>
<fos:proto name="divide-decimals" return-type="record(quotient as xs:decimal, remainder as xs:decimal)">
<fos:arg name="value" type="xs:decimal"/>
<fos:arg name="divisor" type="xs:decimal"/>
<fos:arg name="precision" type="xs:integer?" default="0"/>
</fos:proto>
</fos:signatures>
<fos:properties>
<fos:property>deterministic</fos:property>
<fos:property>context-independent</fos:property>
<fos:property>focus-independent</fos:property>
</fos:properties>
<fos:summary>
<p>Divides one <code>xs:decimal</code> by another to a defined precision, returning
both the quotient and the remainder.</p>
</fos:summary>
<fos:rules>
<p>The function returns a record with two fields:</p>
<olist>
<item>
<p><code>quotient</code> is the <code>xs:decimal</code> value
furthest from zero such that:</p>
<olist>
<item><p><code>quotient</code> is an exact multiple of ten to the power
of minus <code>$precision</code>;</p></item>
<item><p>the absolute value of <code>quotient</code>
multipled by <code>$divisor</code> is less than or equal to the absolute
value of <code>$value</code>;</p></item>
<item><p>the sign of <code>quotient</code> is the same as the sign
of <code>op:numeric-divide($value, $divisor)</code>.</p></item>
</olist>
</item>
<item>
<p><code>remainder</code> is the exact result of subtracting <code>quotient</code>
multiplied by <code>$divisor</code> from <code>$value</code>.</p>
</item>
</olist>

<p>If <code>$precision</code> exceeds the maximum precision for <code>xs:decimal</code>
values supported by the implementation, then the maximum available precision is
used in its place.</p>

</fos:rules>
<fos:errors>
<p>A dynamic error is raised <errorref class="AR" code="0001"/>
if <code>$divisor</code> is zero.</p>
</fos:errors>

<fos:examples>
<fos:example>
<fos:test>
<fos:expression>divide-decimals(120.6, 60.3, 4)</fos:expression>
<fos:result>{ "quotient": 2, "remainder": 0 }</fos:result>
</fos:test>
<fos:test>
<fos:expression>divide-decimals(10, 3)</fos:expression>
<fos:result>{ "quotient": 3, "remainder": 1 }</fos:result>
</fos:test>
<fos:test>
<fos:expression>divide-decimals(10, -3)</fos:expression>
<fos:result>{ "quotient": -3, "remainder": 1 }</fos:result>
</fos:test>
<fos:test>
<fos:expression>divide-decimals(-10, 3)</fos:expression>
<fos:result>{ "quotient": -3, "remainder": -1 }</fos:result>
</fos:test>
<fos:test>
<fos:expression>divide-decimals(-10, -3)</fos:expression>
<fos:result>{ "quotient": 3, "remainder": -1 }</fos:result>
</fos:test>
<fos:test>
<fos:expression>divide-decimals(10, 3, 6)</fos:expression>
<fos:result>{ "quotient": 3.333333, "remainder": 0.000001 }</fos:result>
</fos:test>
<fos:test>
<fos:expression>divide-decimals(100, 30)</fos:expression>
<fos:result>{ "quotient": 3, "remainder": 10 }</fos:result>
</fos:test>
<fos:test>
<fos:expression>divide-decimals(150_862, 7, -3)</fos:expression>
<fos:result>{ "quotient": 21_000, "remainder": 3_862 }</fos:result>
</fos:test>
</fos:example>
</fos:examples>
<fos:changes>
<fos:change issue="1261" date="2025-01-01">
<p>New in 4.0.</p>
</fos:change>
</fos:changes>
</fos:function>



<fos:function name="format-integer" prefix="fn">
<fos:signatures>
<fos:proto name="format-integer" return-type="xs:string">
Expand Down
3 changes: 3 additions & 0 deletions specifications/xpath-functions-40/src/xpath-functions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1951,6 +1951,9 @@ This differs from <bibref ref="xmlschema-2"/>, which defines
<div3 id="func-round-half-to-even">
<head><?function fn:round-half-to-even?></head>
</div3>
<div3 id="func-divide-decimals">
<head><?function fn:divide-decimals?></head>
</div3>
<div3 id="func-is-NaN" diff="add" at="A">
<head><?function fn:is-NaN?></head>
</div3>
Expand Down

0 comments on commit bf9f234

Please sign in to comment.