473,769 Members | 6,926 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 17089
On Jan 14, 12:40 pm, Matthew <matt...@spamki ller.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...@spamki ller.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*****@spamki ller.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("precis ion", "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("precis ion", "16");
echo 1.0/3.0, "\n"; # =0.333333333333 3333

ini_set("precis ion", "50");
echo 1.0/3.0, "\n"; # =0.333333333333 333314829616256 247390992939472 19848633

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_prec ision" 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*****@spamki ller.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("precis ion", "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("precis ion", "16");
echo 1.0/3.0, "\n"; # =0.333333333333 3333

ini_set("precis ion", "50");
echo 1.0/3.0, "\n"; # =0.333333333333 333314829616256 247390992939472 19848633

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_prec ision" 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
6262
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
2272
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 problems with floating point arithmetic and I am looking for a way to solve it. See the code below... Given a certain amount of change (below $1.00) the program will tell you how many of each coin you will need to get that amount. The program...
70
3638
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 register or in memory. For example, an implicit spilling of a register is not permitted to alter the value. Also, an explicit store and load is required to round to the precision of the storage type. In particular, casts and assignments are...
39
3579
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 out there understands what happens. Essentially, when I subtract the (double) function value GRID_POINT(2) from a variable which has been assigned the same value before this gives a non-zero result and I really do not understand why.
0
10214
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9996
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5304
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3963
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.