Skip to content

Commit df9d597

Browse files
committed
Created add()
1 parent 44f5092 commit df9d597

File tree

2 files changed

+296
-0
lines changed

2 files changed

+296
-0
lines changed

reference/bc/bcmath/number/add.xml

Lines changed: 295 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,295 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<refentry xml:id="bcmath-number.add" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
3+
<refnamediv>
4+
<refname>BcMath\Number::add</refname>
5+
<refpurpose>Adds 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::add</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+
Adds this object and <parameter>num</parameter>.
17+
</simpara>
18+
</refsect1>
19+
20+
<refsect1 role="parameters">
21+
&reftitle.parameters;
22+
<variablelist>
23+
<varlistentry>
24+
<term><parameter>num</parameter></term>
25+
<listitem>
26+
<simpara>
27+
The value to add.
28+
</simpara>
29+
</listitem>
30+
</varlistentry>
31+
&bc.number.scale.description;
32+
</variablelist>
33+
</refsect1>
34+
35+
<refsect1 role="returnvalues">
36+
&reftitle.returnvalues;
37+
<simpara>
38+
Returns the result of addition as a new <classname>BcMath\Number</classname> object.
39+
</simpara>
40+
<simpara>
41+
The same is true for calculations using operators that correspond to this method.
42+
</simpara>
43+
</refsect1>
44+
45+
<refsect1 role="errors">
46+
&reftitle.errors;
47+
<para>
48+
This method throws a <exceptionname>ValueError</exceptionname> in the following cases:
49+
<simplelist>
50+
<member><parameter>num</parameter> is <type>string</type> and not a well-formed BCMath numeric string</member>
51+
<member><parameter>scale</parameter> is outside the valid range</member>
52+
</simplelist>
53+
</para>
54+
<simpara>
55+
When using the operator, throw a <exceptionname>ValueError</exceptionname> if one of the operands
56+
is <type>string</type> and not a well-formed BCMath numeric string.
57+
</simpara>
58+
</refsect1>
59+
60+
<refsect1 role="bc.autoscale">
61+
<title>Auto scale</title>
62+
<simpara>
63+
When the <property>BcMath\Number::scale</property> of the result object is automatically set,
64+
the larger <property>BcMath\Number::scale</property> of the two numbers used for addition is used.
65+
</simpara>
66+
<simpara>
67+
That is, if the <property>BcMath\Number::scale</property>s of two values are <literal>2</literal>
68+
and <literal>5</literal> respectively, the <property>BcMath\Number::scale</property> of the result
69+
will be <literal>5</literal>.
70+
</simpara>
71+
<simpara>
72+
If either of the two values is a <type>string</type>, the <property>BcMath\Number::scale</property>
73+
of the <type>string</type> value is automatically obtained. For <type>int</type>, <parameter>scale
74+
</parameter> is treated as <literal>0</literal>.
75+
</simpara>
76+
</refsect1>
77+
78+
<refsect1 role="examples">
79+
&reftitle.examples;
80+
<example>
81+
<title><methodname>BcMath\Number::add</methodname> example when <parameter>scale</parameter> is not specified</title>
82+
<programlisting role="php">
83+
<![CDATA[
84+
<?php
85+
$number = new BcMath\Number('1.234');
86+
87+
$ret1 = $number->add(new BcMath\Number('2.34567'));
88+
$ret2 = $number->add('-3.456');
89+
$ret3 = $number->add(7);
90+
91+
var_dump($number, $ret1, $ret2, $ret3);
92+
?>
93+
]]>
94+
</programlisting>
95+
&example.outputs;
96+
<screen>
97+
<![CDATA[
98+
object(BcMath\Number)#1 (2) {
99+
["value"]=>
100+
string(5) "1.234"
101+
["scale"]=>
102+
int(3)
103+
}
104+
object(BcMath\Number)#3 (2) {
105+
["value"]=>
106+
string(7) "3.57967"
107+
["scale"]=>
108+
int(5)
109+
}
110+
object(BcMath\Number)#2 (2) {
111+
["value"]=>
112+
string(6) "-2.222"
113+
["scale"]=>
114+
int(3)
115+
}
116+
object(BcMath\Number)#4 (2) {
117+
["value"]=>
118+
string(5) "8.234"
119+
["scale"]=>
120+
int(3)
121+
}
122+
]]>
123+
</screen>
124+
</example>
125+
126+
<example>
127+
<title><methodname>BcMath\Number::add</methodname> example of explicitly specifying <parameter>scale</parameter></title>
128+
<programlisting role="php">
129+
<![CDATA[
130+
<?php
131+
$number = new BcMath\Number('1.234');
132+
133+
$ret1 = $number->add(new BcMath\Number('2.34567'), 1);
134+
$ret2 = $number->add('-3.456', 10);
135+
$ret3 = $number->add(7, 0);
136+
137+
var_dump($number, $ret1, $ret2, $ret3);
138+
?>
139+
]]>
140+
</programlisting>
141+
&example.outputs;
142+
<screen>
143+
<![CDATA[
144+
object(BcMath\Number)#1 (2) {
145+
["value"]=>
146+
string(5) "1.234"
147+
["scale"]=>
148+
int(3)
149+
}
150+
object(BcMath\Number)#3 (2) {
151+
["value"]=>
152+
string(3) "3.5"
153+
["scale"]=>
154+
int(1)
155+
}
156+
object(BcMath\Number)#2 (2) {
157+
["value"]=>
158+
string(13) "-2.2220000000"
159+
["scale"]=>
160+
int(10)
161+
}
162+
object(BcMath\Number)#4 (2) {
163+
["value"]=>
164+
string(1) "8"
165+
["scale"]=>
166+
int(0)
167+
}
168+
]]>
169+
</screen>
170+
</example>
171+
</refsect1>
172+
173+
<refsect1 role="bc.operatoroverload">
174+
<title>Operator Overload</title>
175+
<simpara>
176+
<classname>BcMath\Number</classname> can perform addition using operators. The calculation with
177+
the operator behaves the same as when the method argument <parameter>scale</parameter> is &null;.
178+
</simpara>
179+
<example>
180+
<title>Example of addition using operators</title>
181+
<programlisting role="php">
182+
<![CDATA[
183+
<?php
184+
$number = new BcMath\Number('1.234');
185+
186+
$ret1 = $number + new BcMath\Number('2.34567');
187+
$ret2 = $number + '-3.45678';
188+
$ret3 = $number + 7;
189+
190+
var_dump($number, $ret1, $ret2, $ret3);
191+
?>
192+
]]>
193+
</programlisting>
194+
&example.outputs;
195+
<screen>
196+
<![CDATA[
197+
object(BcMath\Number)#1 (2) {
198+
["value"]=>
199+
string(5) "1.234"
200+
["scale"]=>
201+
int(3)
202+
}
203+
object(BcMath\Number)#3 (2) {
204+
["value"]=>
205+
string(7) "3.57967"
206+
["scale"]=>
207+
int(5)
208+
}
209+
object(BcMath\Number)#2 (2) {
210+
["value"]=>
211+
string(8) "-2.22278"
212+
["scale"]=>
213+
int(5)
214+
}
215+
object(BcMath\Number)#4 (2) {
216+
["value"]=>
217+
string(5) "8.234"
218+
["scale"]=>
219+
int(3)
220+
}
221+
]]>
222+
</screen>
223+
</example>
224+
225+
<example>
226+
<title>Examples of increment and shorthand addition using operators</title>
227+
<programlisting role="php">
228+
<![CDATA[
229+
<?php
230+
$number = new BcMath\Number('1.234');
231+
$num1 = $number;
232+
$num2 = $number;
233+
234+
$num1 += new BcMath\Number('2.345');
235+
$num2++;
236+
237+
var_dump($number, $num1, $num2);
238+
?>
239+
]]>
240+
</programlisting>
241+
&example.outputs;
242+
<screen>
243+
<![CDATA[
244+
object(BcMath\Number)#1 (2) {
245+
["value"]=>
246+
string(5) "1.234"
247+
["scale"]=>
248+
int(3)
249+
}
250+
object(BcMath\Number)#3 (2) {
251+
["value"]=>
252+
string(5) "3.579"
253+
["scale"]=>
254+
int(3)
255+
}
256+
object(BcMath\Number)#2 (2) {
257+
["value"]=>
258+
string(5) "2.234"
259+
["scale"]=>
260+
int(3)
261+
}
262+
]]>
263+
</screen>
264+
</example>
265+
</refsect1>
266+
267+
<refsect1 role="seealso">
268+
&reftitle.seealso;
269+
<simplelist>
270+
<member><function>bcadd</function></member>
271+
<member><methodname>BcMath\Number::sub</methodname></member>
272+
</simplelist>
273+
</refsect1>
274+
275+
</refentry>
276+
<!-- Keep this comment at the end of the file
277+
Local variables:
278+
mode: sgml
279+
sgml-omittag:t
280+
sgml-shorttag:t
281+
sgml-minimize-attributes:nil
282+
sgml-always-quote-attributes:t
283+
sgml-indent-step:1
284+
sgml-indent-data:t
285+
indent-tabs-mode:nil
286+
sgml-parent-document:nil
287+
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
288+
sgml-exposed-tags:nil
289+
sgml-local-catalogs:nil
290+
sgml-local-ecat-files:nil
291+
End:
292+
vim600: syn=xml fen fdm=syntax fdl=2 si
293+
vim: et tw=78 syn=sgml
294+
vi: ts=1 sw=1
295+
-->

reference/bc/functions/bcadd.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ echo bcadd($a, $b, 4); // 6.2340
9999
<para>
100100
<simplelist>
101101
<member><function>bcsub</function></member>
102+
<member><methodname>BcMath\Number::add</methodname></member>
102103
</simplelist>
103104
</para>
104105
</refsect1>

0 commit comments

Comments
 (0)