Skip to content

ext/bcmath: BcMath\Number class #4187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 42 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
32868b6
created common part
SakiTakamachi Nov 29, 2024
78d52d4
created class
SakiTakamachi Nov 29, 2024
0d4e68e
edit book, ini
SakiTakamachi Nov 29, 2024
824d901
Added the class into versions
SakiTakamachi Nov 29, 2024
44f5092
Created construct, serialize, unserialize, toString
SakiTakamachi Nov 29, 2024
df9d597
Created add()
SakiTakamachi Nov 29, 2024
5a711ab
Created sub()
SakiTakamachi Nov 29, 2024
5a2b677
Created div()
SakiTakamachi Nov 29, 2024
64e3458
Created mul()
SakiTakamachi Nov 29, 2024
3ee4949
Created mod()
SakiTakamachi Nov 29, 2024
34f4a6d
Created pow()
SakiTakamachi Nov 29, 2024
e348369
Created divmod()
SakiTakamachi Nov 29, 2024
be8b29d
Created powmod()
SakiTakamachi Nov 29, 2024
8619f4c
Created ceil()
SakiTakamachi Nov 29, 2024
de38dc7
Created floor()
SakiTakamachi Nov 29, 2024
23ef913
Created round()
SakiTakamachi Nov 29, 2024
67ad21f
Created compare()
SakiTakamachi Nov 29, 2024
3a9547b
Created sqrt()
SakiTakamachi Nov 29, 2024
7b51ff8
address a comment
SakiTakamachi Dec 2, 2024
19d38dd
address a comment
SakiTakamachi Dec 2, 2024
88ca28e
address a comment
SakiTakamachi Dec 2, 2024
1917ae3
address a comment
SakiTakamachi Dec 2, 2024
646fe01
address a comment
SakiTakamachi Dec 2, 2024
228cba8
address a comment
SakiTakamachi Dec 2, 2024
3ffc207
address a comment
SakiTakamachi Dec 2, 2024
22792d5
Address comments and more
SakiTakamachi Dec 2, 2024
10e1602
changed "larger" to "greater" and "smaller than" to "less than"
SakiTakamachi Dec 2, 2024
cafb7ff
typo
SakiTakamachi Dec 2, 2024
3a7ed30
Changed the scale param desctiption
SakiTakamachi Dec 2, 2024
cd6f8d7
Deleted unnecessary descriptions
SakiTakamachi Dec 2, 2024
6899586
Avoid nesting includes
SakiTakamachi Dec 2, 2024
c860c08
address a comment
SakiTakamachi Dec 16, 2024
043e7e2
address a comment
SakiTakamachi Dec 16, 2024
5b1228f
address a comment
SakiTakamachi Dec 16, 2024
d2e7031
address a comment
SakiTakamachi Dec 16, 2024
d480231
address a comment
SakiTakamachi Dec 16, 2024
cb0d697
address a comment
SakiTakamachi Dec 16, 2024
6a35375
address a comment
SakiTakamachi Dec 16, 2024
60a76f1
Moved varlistentry of scale parameter to add()
SakiTakamachi Dec 16, 2024
10b03c4
Added a link to "spaceship operator"
SakiTakamachi Dec 16, 2024
6411e68
fixed "+<literal>10" to "<literal>+10"
SakiTakamachi Dec 16, 2024
57d7592
Moded a example code
SakiTakamachi Dec 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions reference/bc/bcmath.number.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<reference xml:id="class.bcmath-number" role="class" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xi="http://www.w3.org/2001/XInclude">

<title>The BcMath\Number class</title>
<titleabbrev>BcMath\Number</titleabbrev>

<partintro>
<section xml:id="bcmath-number.intro">
&reftitle.intro;
<simpara>
A class for an arbitrary precision number.
These objects support overloaded
<link linkend="language.operators.arithmetic">arithmetic</link> and
<link linkend="language.operators.comparison">comparison</link> operators.
</simpara>

<note>
<simpara>
This class is not affected by the
<link linkend="ini.bcmath.scale">bcmath.scale</link>
INI directive set in &php.ini;.
</simpara>
</note>

<note>
<simpara>
The behavior of an overloaded operator is the same as specifying &null; for the
<parameter>scale</parameter> parameter on the corresponding method.
</simpara>
</note>
</section>

<section xml:id="bcmath-number.synopsis">
&reftitle.classsynopsis;

<classsynopsis class="class">
<ooclass>
<modifier>final</modifier>
<modifier>readonly</modifier>
<classname>BcMath\Number</classname>
</ooclass>

<oointerface>
<modifier>implements</modifier>
<interfacename>Stringable</interfacename>
</oointerface>

<classsynopsisinfo role="comment">&Properties;</classsynopsisinfo>
<fieldsynopsis>
<modifier>public</modifier>
<type>string</type>
<varname linkend="bcmath-number.props.value">value</varname>
</fieldsynopsis>
<fieldsynopsis>
<modifier>public</modifier>
<type>int</type>
<varname linkend="bcmath-number.props.scale">scale</varname>
</fieldsynopsis>

<classsynopsisinfo role="comment">&Methods;</classsynopsisinfo>
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('class.bcmath-number')/db:refentry/db:refsect1[@role='description']/descendant::db:constructorsynopsis[@role='BcMath\\Number'])">
<xi:fallback/>
</xi:include>
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('class.bcmath-number')/db:refentry/db:refsect1[@role='description']/descendant::db:methodsynopsis[@role='BcMath\\Number'])">
<xi:fallback/>
</xi:include>
</classsynopsis>
</section>

<section xml:id="bcmath-number.props">
&reftitle.properties;
<variablelist>
<varlistentry xml:id="bcmath-number.props.value">
<term><varname>value</varname></term>
<listitem>
<simpara>
A string representation of an arbitrary precision number.
</simpara>
</listitem>
</varlistentry>
<varlistentry xml:id="bcmath-number.props.scale">
<term><varname>scale</varname></term>
<listitem>
<simpara>
The scale value currently set on the object.
For objects resulting from calculations, this value is automatically computed and set,
unless the <parameter>scale</parameter> parameter was set in the calculation method.
</simpara>
</listitem>
</varlistentry>
</variablelist>
</section>

</partintro>

&reference.bc.bcmath.entities.number;

</reference>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
193 changes: 193 additions & 0 deletions reference/bc/bcmath/number/add.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
<?xml version="1.0" encoding="utf-8"?>
<refentry xml:id="bcmath-number.add" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>BcMath\Number::add</refname>
<refpurpose>Adds an arbitrary precision number</refpurpose>
</refnamediv>

<refsect1 role="description">
&reftitle.description;
<methodsynopsis role="BcMath\\Number">
<modifier>public</modifier> <type>BcMath\Number</type><methodname>BcMath\Number::add</methodname>
<methodparam><type class="union"><type>BcMath\Number</type><type>string</type><type>int</type></type><parameter>num</parameter></methodparam>
<methodparam choice="opt"><type class="union"><type>int</type><type>null</type></type><parameter>scale</parameter><initializer>&null;</initializer></methodparam>
</methodsynopsis>
<simpara>
Adds <varname>$this</varname> and <parameter>num</parameter>.
</simpara>
</refsect1>

<refsect1 role="parameters">
&reftitle.parameters;
<variablelist>
<varlistentry>
<term><parameter>num</parameter></term>
<listitem>
<simpara>
The value to add.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>scale</parameter></term>
<listitem>
<simpara>
<parameter>scale</parameter> explicitly specified for calculation results.
If &null;, the <parameter>scale</parameter> of the calculation result will be set automatically.
</simpara>
</listitem>
</varlistentry>
</variablelist>
</refsect1>

<refsect1 role="returnvalues">
&reftitle.returnvalues;
<simpara>
Returns the result of addition as a new <classname>BcMath\Number</classname> object.
</simpara>
<simpara>
When the <property>BcMath\Number::scale</property> of the result object is automatically set,
the greater <property>BcMath\Number::scale</property> of the two numbers used for addition is used.
</simpara>
<simpara>
That is, if the <property>BcMath\Number::scale</property>s of two values are <literal>2</literal>
and <literal>5</literal> respectively, the <property>BcMath\Number::scale</property> of the result
will be <literal>5</literal>.
</simpara>
</refsect1>

<refsect1 role="errors">
&reftitle.errors;
<para>
This method throws a <exceptionname>ValueError</exceptionname> in the following cases:
<simplelist>
<member><parameter>num</parameter> is <type>string</type> and not a well-formed BCMath numeric string</member>
<member><parameter>scale</parameter> is outside the valid range</member>
</simplelist>
</para>
</refsect1>

<refsect1 role="examples">
&reftitle.examples;
<example>
<title><methodname>BcMath\Number::add</methodname> example when <parameter>scale</parameter> is not specified</title>
<programlisting role="php">
<![CDATA[
<?php
$number = new BcMath\Number('1.234');

$ret1 = $number->add(new BcMath\Number('2.34567'));
$ret2 = $number->add('-3.456');
$ret3 = $number->add(7);

var_dump($number, $ret1, $ret2, $ret3);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
object(BcMath\Number)#1 (2) {
["value"]=>
string(5) "1.234"
["scale"]=>
int(3)
}
object(BcMath\Number)#3 (2) {
["value"]=>
string(7) "3.57967"
["scale"]=>
int(5)
}
object(BcMath\Number)#2 (2) {
["value"]=>
string(6) "-2.222"
["scale"]=>
int(3)
}
object(BcMath\Number)#4 (2) {
["value"]=>
string(5) "8.234"
["scale"]=>
int(3)
}
]]>
</screen>
</example>

<example>
<title><methodname>BcMath\Number::add</methodname> example of explicitly specifying <parameter>scale</parameter></title>
<programlisting role="php">
<![CDATA[
<?php
$number = new BcMath\Number('1.234');

$ret1 = $number->add(new BcMath\Number('2.34567'), 1);
$ret2 = $number->add('-3.456', 10);
$ret3 = $number->add(7, 0);

var_dump($number, $ret1, $ret2, $ret3);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
object(BcMath\Number)#1 (2) {
["value"]=>
string(5) "1.234"
["scale"]=>
int(3)
}
object(BcMath\Number)#3 (2) {
["value"]=>
string(3) "3.5"
["scale"]=>
int(1)
}
object(BcMath\Number)#2 (2) {
["value"]=>
string(13) "-2.2220000000"
["scale"]=>
int(10)
}
object(BcMath\Number)#4 (2) {
["value"]=>
string(1) "8"
["scale"]=>
int(0)
}
]]>
</screen>
</example>
</refsect1>

<refsect1 role="seealso">
&reftitle.seealso;
<simplelist>
<member><function>bcadd</function></member>
<member><methodname>BcMath\Number::sub</methodname></member>
</simplelist>
</refsect1>

</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
Loading
Loading