|
| 1 | +<?xml version="1.0" encoding="utf-8"?> |
| 2 | +<refentry xml:id="bcmath-number.divmod" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xi="http://www.w3.org/2001/XInclude"> |
| 3 | + <refnamediv> |
| 4 | + <refname>BcMath\Number::divmod</refname> |
| 5 | + <refpurpose>Gets the quotient and modulus of an arbitrary precision number</refpurpose> |
| 6 | + </refnamediv> |
| 7 | + |
| 8 | + <refsect1 role="description"> |
| 9 | + &reftitle.description; |
| 10 | + <methodsynopsis role="BcMath\\Number"> |
| 11 | + <modifier>public</modifier> <type>array</type><methodname>BcMath\Number::divmod</methodname> |
| 12 | + <methodparam><type class="union"><type>BcMath\Number</type><type>string</type><type>int</type></type><parameter>num</parameter></methodparam> |
| 13 | + <methodparam choice="opt"><type class="union"><type>int</type><type>null</type></type><parameter>scale</parameter><initializer>&null;</initializer></methodparam> |
| 14 | + </methodsynopsis> |
| 15 | + <simpara> |
| 16 | + Gets the quotient and remainder of dividing this object by <parameter>num</parameter>. |
| 17 | + </simpara> |
| 18 | + </refsect1> |
| 19 | + |
| 20 | + <!-- parameters --> |
| 21 | + <xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('bcmath-number.div')/db:refsect1[@role='parameters'])" /> |
| 22 | + |
| 23 | + <refsect1 role="returnvalues"> |
| 24 | + &reftitle.returnvalues; |
| 25 | + <simpara> |
| 26 | + Returns an indexed <type>array</type> where the first element is the quotient as a new <classname> |
| 27 | + BcMath\Number</classname> object and the second element is the remainder as a new <classname> |
| 28 | + BcMath\Number</classname> object. |
| 29 | + </simpara> |
| 30 | + <simpara> |
| 31 | + The quotient is always an integer value, so <property>BcMath\Number::scale</property> is always <literal>0</literal>. |
| 32 | + </simpara> |
| 33 | + </refsect1> |
| 34 | + |
| 35 | + <refsect1 role="errors"> |
| 36 | + &reftitle.errors; |
| 37 | + <!-- ValueError cases of using the method --> |
| 38 | + <xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('bcmath-number.add')/db:refsect1[@role='errors']/db:para[1])" /> |
| 39 | + <!-- The DivisionByZeroError case of using the method --> |
| 40 | + <xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('bcmath-number.div')/db:refsect1[@role='errors']/db:simpara[1])" /> |
| 41 | + <!-- ValueError cases of using the operator --> |
| 42 | + <xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('bcmath-number.add')/db:refsect1[@role='errors']/db:simpara[1])" /> |
| 43 | + <!-- The DivisionByZeroError case of using the operator --> |
| 44 | + <xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('bcmath-number.div')/db:refsect1[@role='errors']/db:simpara[2])" /> |
| 45 | + </refsect1> |
| 46 | + |
| 47 | + <refsect1 role="bc.autoscale"> |
| 48 | + <!-- auto scale title --> |
| 49 | + <xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('bcmath-number.add')/db:refsect1[@role='bc.autoscale']/db:title)" /> |
| 50 | + <simpara> |
| 51 | + The quotient is always an integer value, so <property>BcMath\Number::scale</property> of the quotient will |
| 52 | + always be <literal>0</literal>, regardless of whether explicitly specify <parameter>scale</parameter>. |
| 53 | + </simpara> |
| 54 | + <simpara> |
| 55 | + If <parameter>scale</parameter> is explicitly specified, <property>BcMath\Number::scale</property> of the |
| 56 | + remainder will be the specified value. |
| 57 | + When the <property>BcMath\Number::scale</property> of the result's remainder object is automatically set, |
| 58 | + the larger <property>BcMath\Number::scale</property> of the two numbers used for modulus operation is used. |
| 59 | + </simpara> |
| 60 | + <simpara> |
| 61 | + That is, if the <property>BcMath\Number::scale</property>s of two values are <literal>2</literal> |
| 62 | + and <literal>5</literal> respectively, the <property>BcMath\Number::scale</property> of the remainder |
| 63 | + will be <literal>5</literal>. |
| 64 | + </simpara> |
| 65 | + <!-- Regarding the handling of scale when calculating with string value --> |
| 66 | + <xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('bcmath-number.add')/db:refsect1[@role='bc.autoscale']/db:simpara[3])" /> |
| 67 | + </refsect1> |
| 68 | + |
| 69 | + <refsect1 role="examples"> |
| 70 | + &reftitle.examples; |
| 71 | + <example> |
| 72 | + <title><methodname>BcMath\Number::divmod</methodname> example when <parameter>scale</parameter> is not specified</title> |
| 73 | + <programlisting role="php"> |
| 74 | +<![CDATA[ |
| 75 | +<?php |
| 76 | +echo '8.3 / 2.22' . PHP_EOL; |
| 77 | +[$quot, $rem] = new BcMath\Number('8')->divmod(new BcMath\Number('2.22')); |
| 78 | +var_dump($quot, $rem); |
| 79 | +
|
| 80 | +echo PHP_EOL . '8.3 / 8.3' . PHP_EOL; |
| 81 | +[$quot, $rem] = new BcMath\Number('8.3')->divmod('8.3'); |
| 82 | +var_dump($quot, $rem); |
| 83 | +
|
| 84 | +echo PHP_EOL . '10 / -3' . PHP_EOL; |
| 85 | +[$quot, $rem] = new BcMath\Number('10')->divmod(-3); |
| 86 | +var_dump($quot, $rem); |
| 87 | +?> |
| 88 | +]]> |
| 89 | + </programlisting> |
| 90 | + &example.outputs; |
| 91 | + <screen> |
| 92 | +<![CDATA[ |
| 93 | +8.3 / 2.22 |
| 94 | +object(BcMath\Number)#3 (2) { |
| 95 | + ["value"]=> |
| 96 | + string(1) "3" |
| 97 | + ["scale"]=> |
| 98 | + int(0) |
| 99 | +} |
| 100 | +object(BcMath\Number)#4 (2) { |
| 101 | + ["value"]=> |
| 102 | + string(4) "1.34" |
| 103 | + ["scale"]=> |
| 104 | + int(2) |
| 105 | +} |
| 106 | +
|
| 107 | +8.3 / 8.3 |
| 108 | +object(BcMath\Number)#2 (2) { |
| 109 | + ["value"]=> |
| 110 | + string(1) "1" |
| 111 | + ["scale"]=> |
| 112 | + int(0) |
| 113 | +} |
| 114 | +object(BcMath\Number)#5 (2) { |
| 115 | + ["value"]=> |
| 116 | + string(3) "0.0" |
| 117 | + ["scale"]=> |
| 118 | + int(1) |
| 119 | +} |
| 120 | +
|
| 121 | +10 / -3 |
| 122 | +object(BcMath\Number)#3 (2) { |
| 123 | + ["value"]=> |
| 124 | + string(2) "-3" |
| 125 | + ["scale"]=> |
| 126 | + int(0) |
| 127 | +} |
| 128 | +object(BcMath\Number)#1 (2) { |
| 129 | + ["value"]=> |
| 130 | + string(1) "1" |
| 131 | + ["scale"]=> |
| 132 | + int(0) |
| 133 | +} |
| 134 | +]]> |
| 135 | + </screen> |
| 136 | + </example> |
| 137 | + |
| 138 | + <example> |
| 139 | + <title><methodname>BcMath\Number::divmod</methodname> example of explicitly specifying <parameter>scale</parameter></title> |
| 140 | + <programlisting role="php"> |
| 141 | +<![CDATA[ |
| 142 | +<?php |
| 143 | +echo '8.3 / 2.22' . PHP_EOL; |
| 144 | +[$quot, $rem] = new BcMath\Number('8')->divmod(new BcMath\Number('2.22'), 1); |
| 145 | +var_dump($quot, $rem); |
| 146 | +
|
| 147 | +echo PHP_EOL . '8.3 / 8.3' . PHP_EOL; |
| 148 | +[$quot, $rem] = new BcMath\Number('8.3')->divmod('8.3', 4); |
| 149 | +var_dump($quot, $rem); |
| 150 | +
|
| 151 | +echo PHP_EOL . '10 / -3' . PHP_EOL; |
| 152 | +[$quot, $rem] = new BcMath\Number('10')->divmod(-3, 5); |
| 153 | +var_dump($quot, $rem); |
| 154 | +?> |
| 155 | +]]> |
| 156 | + </programlisting> |
| 157 | + &example.outputs; |
| 158 | + <screen> |
| 159 | +<![CDATA[ |
| 160 | +8.3 / 2.22 |
| 161 | +object(BcMath\Number)#3 (2) { |
| 162 | + ["value"]=> |
| 163 | + string(1) "3" |
| 164 | + ["scale"]=> |
| 165 | + int(0) |
| 166 | +} |
| 167 | +object(BcMath\Number)#4 (2) { |
| 168 | + ["value"]=> |
| 169 | + string(3) "1.3" |
| 170 | + ["scale"]=> |
| 171 | + int(1) |
| 172 | +} |
| 173 | +
|
| 174 | +8.3 / 8.3 |
| 175 | +object(BcMath\Number)#2 (2) { |
| 176 | + ["value"]=> |
| 177 | + string(1) "1" |
| 178 | + ["scale"]=> |
| 179 | + int(0) |
| 180 | +} |
| 181 | +object(BcMath\Number)#5 (2) { |
| 182 | + ["value"]=> |
| 183 | + string(6) "0.0000" |
| 184 | + ["scale"]=> |
| 185 | + int(4) |
| 186 | +} |
| 187 | +
|
| 188 | +10 / -3 |
| 189 | +object(BcMath\Number)#3 (2) { |
| 190 | + ["value"]=> |
| 191 | + string(2) "-3" |
| 192 | + ["scale"]=> |
| 193 | + int(0) |
| 194 | +} |
| 195 | +object(BcMath\Number)#1 (2) { |
| 196 | + ["value"]=> |
| 197 | + string(7) "1.00000" |
| 198 | + ["scale"]=> |
| 199 | + int(5) |
| 200 | +} |
| 201 | +]]> |
| 202 | + </screen> |
| 203 | + </example> |
| 204 | + </refsect1> |
| 205 | + |
| 206 | + <refsect1 role="seealso"> |
| 207 | + &reftitle.seealso; |
| 208 | + <simplelist> |
| 209 | + <member><function>bcdivmod</function></member> |
| 210 | + <member><methodname>BcMath\Number::div</methodname></member> |
| 211 | + <member><methodname>BcMath\Number::mod</methodname></member> |
| 212 | + </simplelist> |
| 213 | + </refsect1> |
| 214 | + |
| 215 | +</refentry> |
| 216 | +<!-- Keep this comment at the end of the file |
| 217 | +Local variables: |
| 218 | +mode: sgml |
| 219 | +sgml-omittag:t |
| 220 | +sgml-shorttag:t |
| 221 | +sgml-minimize-attributes:nil |
| 222 | +sgml-always-quote-attributes:t |
| 223 | +sgml-indent-step:1 |
| 224 | +sgml-indent-data:t |
| 225 | +indent-tabs-mode:nil |
| 226 | +sgml-parent-document:nil |
| 227 | +sgml-default-dtd-file:"~/.phpdoc/manual.ced" |
| 228 | +sgml-exposed-tags:nil |
| 229 | +sgml-local-catalogs:nil |
| 230 | +sgml-local-ecat-files:nil |
| 231 | +End: |
| 232 | +vim600: syn=xml fen fdm=syntax fdl=2 si |
| 233 | +vim: et tw=78 syn=sgml |
| 234 | +vi: ts=1 sw=1 |
| 235 | +--> |
0 commit comments