473,323 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Changing precision level of floating point variables

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..
Jan 14 '08 #1
6 16994
On Jan 14, 12:40 pm, Matthew <matt...@spamkiller.comwrote:
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:

<http://www.php.net/manual/en/ini.php>

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():

<http://www.php.net/ini_set>
Jan 14 '08 #2
ZeldorBlat emailed this:
On Jan 14, 12:40 pm, Matthew <matt...@spamkiller.comwrote:
>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:

<http://www.php.net/manual/en/ini.php>

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():

<http://www.php.net/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.
Jan 14 '08 #3
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.

Jan 14 '08 #4
Matthew <ma*****@spamkiller.comwrote:
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.33333333333333331482961625624739099293947219848 633

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

Jan 14 '08 #5
Umberto Salsi emailed this:
Matthew <ma*****@spamkiller.comwrote:
>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.33333333333333331482961625624739099293947219848 633

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.

Jan 14 '08 #6
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.
Jan 14 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: BigMan | last post by:
How can I check if assignment of a float to a double (or vice versa) will result in loss of precision?
10
by: Shawn | last post by:
Hello all, I apologize as I am sure this has probably been dealth with before... but I am doing an exercise from "Practical C Programming" and I have been unable to get it to work perfectly due to...
70
by: Robert Gamble | last post by:
9899:1999 5.1.2.3 Example 4 reads: "EXAMPLE 4 Implementations employing wide registers have to take care to honor appropriate semantics. Values are independent of whether they are represented in a...
39
by: rembremading | last post by:
Hi all! The following piece of code has (for me) completely unexpected behaviour. (I compile it with gcc-Version 4.0.3) Something goes wrong with the integer to float conversion. Maybe somebody...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shćllîpôpď 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.