reference/bc/book.xml
1d61be1ed9edb9b8191206768f2640aa9c7623ae
...
...
@@ -10,9 +10,39 @@
10
10
<preface xml:id="intro.bc">
11
11
&reftitle.intro;
12
12
<para>
13
-
For arbitrary precision mathematics PHP offers the Binary Calculator which
14
-
supports numbers of any size and precision, represented as strings.
13
+
For arbitrary precision mathematics PHP offers BCMath which
14
+
supports numbers of any size and precision up to <literal>2147483647</literal> (or <literal>0x7FFFFFFF</literal>) decimal digits,
15
+
if there is sufficient memory, represented as strings.
15
16
</para>
17
+
<para>
18
+
Valid (aka. well-formed) BCMath numbers are strings which match the regular expression
19
+
<literal>/^[+-]?[0-9]*(\.[0-9]*)?$/</literal>.
20
+
</para>
21
+
<caution>
22
+
<para>
23
+
Passing values of type <type>float</type> to a BCMath function which expects
24
+
a <type>string</type> as operand may not have the desired effect due to the
25
+
way PHP converts <type>float</type> values to <type>string</type>, namely
26
+
that the <type>string</type> may be in exponential notation (which is not
27
+
supported by BCMath), and that, prior to PHP 8.0.0, the decimal separator is locale dependent
28
+
(while BCMath always expects a decimal point).
29
+
</para>
30
+
<informalexample>
31
+
<programlisting role="php">
32
+
<![CDATA[
33
+
<?php
34
+
$num1 = 0; // (string) 0 => '0'
35
+
$num2 = -0.000005; // (string) -0.000005 => '-5.05E-6'
36
+
echo bcadd($num1, $num2, 6); // => '0.000000'
37
+

38
+
setlocale(LC_NUMERIC, 'de_DE'); // uses a decimal comma
39
+
$num2 = 1.2; // (string) 1.2 => '1,2'
40
+
echo bcsub($num1, $num2, 1); // => '0.0'
41
+
?>
42
+
]]>
43
+
</programlisting>
44
+
</informalexample>
45
+
</caution>
16
46
</preface>
17
47
<!-- }}} -->
18
48
19
49