language/types/float.xml
e587d0655e426f97b3fcb431453da5030e743b23
...
...
@@ -29,7 +29,7 @@ $d = 1_234.567; // as of PHP 7.4.0
29
29
<programlisting>
30
30
<![CDATA[
31
31
LNUM [0-9]+(_[0-9]+)*
32
-
DNUM ([0-9]*(_[0-9]+)*[\.]{LNUM}) | ({LNUM}[\.][0-9]*(_[0-9]+)*)
32
+
DNUM ({LNUM}?"."{LNUM}) | ({LNUM}"."{LNUM}?)
33
33
EXPONENT_DNUM (({LNUM} | {DNUM}) [eE][+-]? {LNUM})
34
34
]]>
35
35
</programlisting>
...
...
@@ -82,17 +82,36 @@ EXPONENT_DNUM (({LNUM} | {DNUM}) [eE][+-]? {LNUM})
82
82
<sect2 xml:id="language.types.float.casting">
83
83
<title>Converting to float</title>
84
84

85
-
<para>
86
-
For information on converting <type>string</type>s to <type>float</type>, see
87
-
<link linkend="language.types.string.conversion">String conversion to
88
-
numbers</link>. For values of other types, the conversion is performed by
89
-
converting the value to <type>integer</type> first and then to
90
-
<type>float</type>. See
91
-
<link linkend="language.types.integer.casting">Converting to integer</link>
92
-
for more information. As of PHP 5, a notice is thrown if an
93
-
<type>object</type> is converted to <type>float</type>.
94
-
</para>
85
+
<sect3 xml:id="language.types.float.casting.from-string">
86
+
<title>From strings</title>
95
87

88
+
<simpara>
89
+
If the string is
90
+
<link linkend="language.types.numeric-strings">numeric</link>
91
+
or leading numeric then it will resolve to the
92
+
corresponding float value, otherwise it is converted to zero
93
+
(<literal>0</literal>).
94
+
</simpara>
95
+
</sect3>
96
+

97
+
<sect3 xml:id="language.types.float.casting.from-other">
98
+
<title>From other types</title>
99
+

100
+
<para>
101
+
For values of other types, the conversion is performed by converting the
102
+
value to <type>int</type> first and then to <type>float</type>. See
103
+
<link linkend="language.types.integer.casting">Converting to integer</link>
104
+
for more information.
105
+
</para>
106
+

107
+
<note>
108
+
<para>
109
+
As certain types have undefined behavior when converting to
110
+
<type>int</type>, this is also the case when converting to
111
+
<type>float</type>.
112
+
</para>
113
+
</note>
114
+
</sect3>
96
115
</sect2>
97
116

98
117
<sect2 xml:id="language.types.float.comparison">
...
...
@@ -111,11 +130,13 @@ EXPONENT_DNUM (({LNUM} | {DNUM}) [eE][+-]? {LNUM})
111
130
or unit roundoff, and is the smallest acceptable difference in calculations.
112
131
</para>
113
132

114
-
<informalexample>
115
-
<simpara>
116
-
<varname>$a</varname> and <varname>$b</varname> are equal to 5 digits of
117
-
precision.
118
-
</simpara>
133
+
<para>
134
+
<varname>$a</varname> and <varname>$b</varname> are equal to 5 digits of
135
+
precision.
136
+
</para>
137
+

138
+
<example>
139
+
<title>Comparing Floats</title>
119
140
<programlisting role="php">
120
141
<![CDATA[
121
142
<?php
...
...
@@ -123,13 +144,13 @@ $a = 1.23456789;
123
144
$b = 1.23456780;
124
145
$epsilon = 0.00001;
125
146

126
-
if(abs($a-$b) < $epsilon) {
147
+
if (abs($a - $b) < $epsilon) {
127
148
echo "true";
128
149
}
129
150
?>
130
151
]]>
131
152
</programlisting>
132
-
</informalexample>
153
+
</example>
133
154
</sect2>
134
155

135
156
<sect2 xml:id="language.types.float.nan">
136
157