<HTML ><HEAD ><TITLE >Floating point numbers</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Types" HREF="language.types.html"><LINK REL="PREVIOUS" TITLE="Integers" HREF="language.types.integer.html"><LINK REL="NEXT" TITLE="Strings" HREF="language.types.string.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="sect1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="language.types.integer.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 7. Types</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="language.types.string.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="language.types.float" ></A >Floating point numbers</H1 ><P > Floating point numbers (AKA "floats", "doubles" or "real numbers") can be specified using any of the following syntaxes: <DIV CLASS="informalexample" ><A NAME="AEN3373" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $a = 1.234; $b = 1.2e3; $c = 7E-10; ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > Formally: <DIV CLASS="informalexample" ><A NAME="AEN3375" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >LNUM [0-9]+ DNUM ([0-9]*[\.]{LNUM}) | ({LNUM}[\.][0-9]*) EXPONENT_DNUM ( ({LNUM} | {DNUM}) [eE][+-]? {LNUM})</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > The size of a float is platform-dependent, although a maximum of ~1.8e308 with a precision of roughly 14 decimal digits is a common value (that's 64 bit IEEE format). </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B ><A NAME="warn.float-precision" ></A >Floating point precision</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > It is quite usual that simple decimal fractions like <TT CLASS="literal" >0.1</TT > or <TT CLASS="literal" >0.7</TT > cannot be converted into their internal binary counterparts without a little loss of precision. This can lead to confusing results: for example, <TT CLASS="literal" >floor((0.1+0.7)*10)</TT > will usually return <TT CLASS="literal" >7</TT > instead of the expected <TT CLASS="literal" >8</TT > as the result of the internal representation really being something like <TT CLASS="literal" >7.9999999999...</TT >. </P ><P > This is related to the fact that it is impossible to exactly express some fractions in decimal notation with a finite number of digits. For instance, <TT CLASS="literal" >1/3</TT > in decimal form becomes <TT CLASS="literal" >0.3333333. . .</TT >. </P ><P > So never trust floating number results to the last digit and never compare floating point numbers for equality. If you really need higher precision, you should use the <A HREF="ref.bc.html" >arbitrary precision math functions</A > or <A HREF="ref.gmp.html" >gmp</A > functions instead. </P ></TD ></TR ></TABLE ></DIV ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="language.types.float.casting" ></A >Converting to float</H2 ><P > For information on when and how strings are converted to floats, see the section titled <A HREF="language.types.string.html#language.types.string.conversion" >String conversion to numbers</A >. For values of other types, the conversion is the same as if the value would have been converted to integer and then to float. See the <A HREF="language.types.integer.html#language.types.integer.casting" >Converting to integer</A > section for more information. </P ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="language.types.integer.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="language.types.string.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Integers</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="language.types.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Strings</TD ></TR ></TABLE ></DIV ></BODY ></HTML >