Skip to content

Commit 3ee4949

Browse files
committed
Created mod()
1 parent 64e3458 commit 3ee4949

File tree

2 files changed

+272
-0
lines changed

2 files changed

+272
-0
lines changed

reference/bc/bcmath/number/mod.xml

Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<refentry xml:id="bcmath-number.mod" 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::mod</refname>
5+
<refpurpose>Gets the 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>BcMath\Number</type><methodname>BcMath\Number::mod</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 remainder of dividing this object by <parameter>num</parameter>.
17+
Unless <parameter>num</parameter> is <literal>0</literal>, the result has the same sign
18+
as this object.
19+
</simpara>
20+
</refsect1>
21+
22+
<!-- parameters -->
23+
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('bcmath-number.div')/db:refsect1[@role='parameters'])" />
24+
25+
<refsect1 role="returnvalues">
26+
&reftitle.returnvalues;
27+
<simpara>
28+
Returns the modulus as a new <classname>BcMath\Number</classname> object.
29+
</simpara>
30+
<!-- About the operator return value -->
31+
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('bcmath-number.add')/db:refsect1[@role='returnvalues']/db:simpara[2])" />
32+
</refsect1>
33+
34+
<refsect1 role="errors">
35+
&reftitle.errors;
36+
<!-- ValueError cases of using the method -->
37+
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('bcmath-number.add')/db:refsect1[@role='errors']/db:para[1])" />
38+
<!-- The DivisionByZeroError case of using the method -->
39+
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('bcmath-number.div')/db:refsect1[@role='errors']/db:simpara[1])" />
40+
<!-- ValueError cases of using the operator -->
41+
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('bcmath-number.add')/db:refsect1[@role='errors']/db:simpara[1])" />
42+
<!-- The DivisionByZeroError case of using the operator -->
43+
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('bcmath-number.div')/db:refsect1[@role='errors']/db:simpara[2])" />
44+
</refsect1>
45+
46+
<refsect1 role="bc.autoscale">
47+
<!-- auto scale title -->
48+
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('bcmath-number.add')/db:refsect1[@role='bc.autoscale']/db:title)" />
49+
<simpara>
50+
When the <property>BcMath\Number::scale</property> of the result object is automatically set,
51+
the larger <property>BcMath\Number::scale</property> of the two numbers used for modulus operation is used.
52+
</simpara>
53+
<!-- Auto scale example -->
54+
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('bcmath-number.add')/db:refsect1[@role='bc.autoscale']/db:simpara[2])" />
55+
<!-- Regarding the handling of scale when calculating with string value -->
56+
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('bcmath-number.add')/db:refsect1[@role='bc.autoscale']/db:simpara[3])" />
57+
</refsect1>
58+
59+
<refsect1 role="examples">
60+
&reftitle.examples;
61+
<example>
62+
<title><methodname>BcMath\Number::mod</methodname> example when <parameter>scale</parameter> is not specified</title>
63+
<programlisting role="php">
64+
<![CDATA[
65+
<?php
66+
$number = new BcMath\Number('8.3');
67+
68+
$ret1 = $number->mod(new BcMath\Number('2.22'));
69+
$ret2 = $number->mod('8.3');
70+
$ret3 = $number->mod(-5);
71+
72+
var_dump($number, $ret1, $ret2, $ret3);
73+
?>
74+
]]>
75+
</programlisting>
76+
&example.outputs;
77+
<screen>
78+
<![CDATA[
79+
object(BcMath\Number)#1 (2) {
80+
["value"]=>
81+
string(3) "8.3"
82+
["scale"]=>
83+
int(1)
84+
}
85+
object(BcMath\Number)#3 (2) {
86+
["value"]=>
87+
string(4) "1.64"
88+
["scale"]=>
89+
int(2)
90+
}
91+
object(BcMath\Number)#2 (2) {
92+
["value"]=>
93+
string(3) "0.0"
94+
["scale"]=>
95+
int(1)
96+
}
97+
object(BcMath\Number)#4 (2) {
98+
["value"]=>
99+
string(3) "3.3"
100+
["scale"]=>
101+
int(1)
102+
}
103+
]]>
104+
</screen>
105+
</example>
106+
107+
<example>
108+
<title><methodname>BcMath\Number::mod</methodname> example of explicitly specifying <parameter>scale</parameter></title>
109+
<programlisting role="php">
110+
<![CDATA[
111+
<?php
112+
$number = new BcMath\Number('8.3');
113+
114+
$ret1 = $number->mod(new BcMath\Number('2.22'), 1);
115+
$ret2 = $number->mod('8.3', 3);
116+
$ret3 = $number->mod(-5, 0);
117+
118+
var_dump($number, $ret1, $ret2, $ret3);
119+
?>
120+
]]>
121+
</programlisting>
122+
&example.outputs;
123+
<screen>
124+
<![CDATA[
125+
object(BcMath\Number)#1 (2) {
126+
["value"]=>
127+
string(3) "8.3"
128+
["scale"]=>
129+
int(1)
130+
}
131+
object(BcMath\Number)#3 (2) {
132+
["value"]=>
133+
string(3) "1.6"
134+
["scale"]=>
135+
int(1)
136+
}
137+
object(BcMath\Number)#2 (2) {
138+
["value"]=>
139+
string(5) "0.000"
140+
["scale"]=>
141+
int(3)
142+
}
143+
object(BcMath\Number)#4 (2) {
144+
["value"]=>
145+
string(1) "3"
146+
["scale"]=>
147+
int(0)
148+
}
149+
]]>
150+
</screen>
151+
</example>
152+
</refsect1>
153+
154+
<refsect1 role="bc.operatoroverload">
155+
<!-- operator overload title -->
156+
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('bcmath-number.add')/db:refsect1[@role='bc.operatoroverload']/db:title)" />
157+
<simpara>
158+
<classname>BcMath\Number</classname> can perform modulus operation using operators. The calculation with
159+
the operator behaves the same as when the method argument <parameter>scale</parameter> is &null;.
160+
</simpara>
161+
<example>
162+
<title>Example of modulus operation using operators</title>
163+
<programlisting role="php">
164+
<![CDATA[
165+
<?php
166+
$number = new BcMath\Number('-7.1');
167+
168+
$ret1 = $number % new BcMath\Number('-3.22');
169+
$ret2 = $number % '7.1';
170+
$ret3 = $number % 6;
171+
172+
var_dump($number, $ret1, $ret2, $ret3);
173+
?>
174+
]]>
175+
</programlisting>
176+
&example.outputs;
177+
<screen>
178+
<![CDATA[
179+
object(BcMath\Number)#1 (2) {
180+
["value"]=>
181+
string(4) "-7.1"
182+
["scale"]=>
183+
int(1)
184+
}
185+
object(BcMath\Number)#3 (2) {
186+
["value"]=>
187+
string(5) "-0.66"
188+
["scale"]=>
189+
int(2)
190+
}
191+
object(BcMath\Number)#2 (2) {
192+
["value"]=>
193+
string(3) "0.0"
194+
["scale"]=>
195+
int(1)
196+
}
197+
object(BcMath\Number)#4 (2) {
198+
["value"]=>
199+
string(4) "-1.1"
200+
["scale"]=>
201+
int(1)
202+
}
203+
]]>
204+
</screen>
205+
</example>
206+
207+
<example>
208+
<title>Examples of shorthand modulus operation using operators</title>
209+
<programlisting role="php">
210+
<![CDATA[
211+
<?php
212+
$number = new BcMath\Number('3');
213+
$num1 = $number;
214+
215+
$num1 %= new BcMath\Number('2');
216+
217+
var_dump($number, $num1);
218+
?>
219+
]]>
220+
</programlisting>
221+
&example.outputs;
222+
<screen>
223+
<![CDATA[
224+
object(BcMath\Number)#1 (2) {
225+
["value"]=>
226+
string(1) "3"
227+
["scale"]=>
228+
int(0)
229+
}
230+
object(BcMath\Number)#3 (2) {
231+
["value"]=>
232+
string(1) "1"
233+
["scale"]=>
234+
int(0)
235+
}
236+
]]>
237+
</screen>
238+
</example>
239+
</refsect1>
240+
241+
<refsect1 role="seealso">
242+
&reftitle.seealso;
243+
<simplelist>
244+
<member><function>bcmod</function></member>
245+
<member><methodname>BcMath\Number::div</methodname></member>
246+
<member><methodname>BcMath\Number::divmod</methodname></member>
247+
<member><methodname>BcMath\Number::powmod</methodname></member>
248+
</simplelist>
249+
</refsect1>
250+
251+
</refentry>
252+
<!-- Keep this comment at the end of the file
253+
Local variables:
254+
mode: sgml
255+
sgml-omittag:t
256+
sgml-shorttag:t
257+
sgml-minimize-attributes:nil
258+
sgml-always-quote-attributes:t
259+
sgml-indent-step:1
260+
sgml-indent-data:t
261+
indent-tabs-mode:nil
262+
sgml-parent-document:nil
263+
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
264+
sgml-exposed-tags:nil
265+
sgml-local-catalogs:nil
266+
sgml-local-ecat-files:nil
267+
End:
268+
vim600: syn=xml fen fdm=syntax fdl=2 si
269+
vim: et tw=78 syn=sgml
270+
vi: ts=1 sw=1
271+
-->

reference/bc/functions/bcmod.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ echo bcmod('5.7', '1.3'); // 0.5 as of PHP 7.2.0; 0 previously
147147
<simplelist>
148148
<member><function>bcdiv</function></member>
149149
<member><function>bcdivmod</function></member>
150+
<member><methodname>BcMath\Number::mod</methodname></member>
150151
</simplelist>
151152
</para>
152153
</refsect1>

0 commit comments

Comments
 (0)