Changing precision level of floating point variables

Changing precision level of floating point variables

am 14.01.2008 18:40:00 von matthew

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..

Re: Changing precision level of floating point variables

am 14.01.2008 18:44:30 von zeldorblat

On Jan 14, 12:40 pm, Matthew 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..

According to this manual page:



the "precision" directive is changeable PHP_INI_ALL which, according
to that same page, means that the "entry can be set anywhere"
including ini_set():

Re: Changing precision level of floating point variables

am 14.01.2008 19:03:14 von matthew

ZeldorBlat emailed this:
> On Jan 14, 12:40 pm, Matthew 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..
>
> According to this manual page:
>
>
>
> the "precision" directive is changeable PHP_INI_ALL which, according
> to that same page, means that the "entry can be set anywhere"
> including ini_set():
>
>

Thanks.

I've just noticed that the manual says:

"precision integer: The number of significant digits displayed in floating
point numbers."

'Displayed' being the significant word - does this mean setting the
precision to 30 (my system defaults to 12) will not increase the accuracy
of my floating point calculations and will change only what will be
displayed if I output the value?

I want to use pi to 30 decimal places, php's pi has a max of 20, for some
complex long/lat calculations.

Thanks.

Re: Changing precision level of floating point variables

am 14.01.2008 19:13:59 von ivansanchez-alg

Matthew wrote:

> I want to use pi to 30 decimal places, php's pi has a max of 20, for some
> complex long/lat calculations.

Use arbitrary precision math libraries.

Alternatively, use the proj4 library (calling the "proj" executable from
php) for your geographical projection needs.


Cheers,
--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Troubled day for virgins over 16 who are beautiful and wealthy and live
in eucalyptus trees.

Re: Changing precision level of floating point variables

am 14.01.2008 19:26:42 von Umberto Salsi

Matthew 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

Re: Changing precision level of floating point variables

am 14.01.2008 20:05:17 von matthew

Umberto Salsi emailed this:
> Matthew 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.

Thanks Umberto. I don't need to display beyond 16 decimal places so that
will do. I thought I could increase the level of precision of the
calculations but I guess not.

Neither BCMath nor BigFloat seem to have trig functions which I'd need if
using them, so I'll just stick to what I have.

Thanks again.

Re: Changing precision level of floating point variables

am 14.01.2008 20:06:08 von matthew

Iván Sánchez Ortega emailed this:
> Matthew wrote:
>
>> I want to use pi to 30 decimal places, php's pi has a max of 20, for some
>> complex long/lat calculations.
>
> Use arbitrary precision math libraries.
>
> Alternatively, use the proj4 library (calling the "proj" executable from
> php) for your geographical projection needs.

Thanks for the pointer Ivan - I'll have a look at proj4.
Cheers.