You are here: Re: Changing precision level of floating point variables « PHP Programming Language « IT news, forums, messages
Re: Changing precision level of floating point variables

Posted by Umberto Salsi on 01/14/08 18:26

Matthew <matthew@spamkiller.com> wrote:

> Hi,
>
> I want to change the precision level of floating point variables and
> calculations which is done in php.ini.
>
> However the server I rent for my domain does not give me access to
> php.ini, they say 'for security reasons'.
>
> Can the precision level be changed by PHP code as needed?
>
> Thanks and regards, etc..

You can set this parameters at run-time. 16 digits is usually the maximum
value on most platforms, larger values giving only meaningless or "fictional"
digits:

ini_set("precision", "16");

This parameter only set the number of significant digits used by default
when a binary "float" value gets formatted as a "string", not the internal
precision at which calculations are performed (usually 53 bits). Example:

ini_set("precision", "16");
echo 1.0/3.0, "\n"; # => 0.3333333333333333

ini_set("precision", "50");
echo 1.0/3.0, "\n"; # => 0.33333333333333331482961625624739099293947219848633

Note that "precision" is involved when:

- a float value gets displayed with echo $f and print $f

- performing an explicit type-cast (string) $f

- on values embedded in literal strings: echo "Average value is $f";

- passing a float to any function expecting a string, so for example
strlen($f) may give differen results also with the same number
depending on the "precision" parameter

- serializing variables/arrays/objects containing floating point numbers,
possibly with loss of precision, so in general $f != unserialize(
serialize( $f ) ); this loss of precision may affect also WEB sessions,
since $_SESSION[] gets automatically serialized at the end of the script
(NOTE: as of PHP 4.3.2 another parameter "serialize_precision" was
introduced, specific for serialized data)

If you need a reliable way to handle high precision numbers, extensions
like BCMATH and libraries as BigFloat are available.

Best regards,
___
/_|_\ Umberto Salsi
\/_\/ www.icosaedro.it

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация