Miko
Miko

Reputation: 111

How to emulate single precision float operations in PHP?

I need to port a simple C program to PHP. Currently we have to start the process and parse it's output. The program is very trivial but it is important for the algorithm to use float as the errors will sum up and the result will be way off.

C example:

#include <stdio.h>

int main( void ) {
  printf("%f\n", 123456 * (float)0.99524);
  printf("%f\n", 123456 * (double)0.99524);

  return 0;
}

PHP example:

<?php

printf("%f\n", 123456 * 0.99524);

?>

The C example will result in 122868.343750 and 122868.349440 while PHP will end up with 122868.349440.

How do I get the C float result in PHP?

Upvotes: 5

Views: 1100

Answers (3)

Czarek Tomczak
Czarek Tomczak

Reputation: 20675

There is no way you can do this using built in php functions.

The one using "double" gives you the real result, 100% precise. The float one is wrong.

In PHP float and double are the same type, which is double.

If you need high precision results, that always give the same results, try using BC Math module: http://php.net/bcmath

Example code using BC Math:

$result = bcmul("123456", "0.99524", 6); // gives 122868.34944
$result = number_format($result, 6, ".", ""); // 122868.349440 - appending zeros
echo $result;

Output:

122868.349440

If you really, really want the same result as in the C program, then you have 2 options:

  1. Create your own c-like function by writing a php extension: http://www.google.com/search?q=writing+php+extensions

  2. Talk to your C-program from PHP via function proc_open(): http://www.php.net/manual/en/function.proc-open.php (see also popen(), exec() or shell_exec())

Upvotes: 3

Homer6
Homer6

Reputation: 15159

You could always create a PHP module.

Here are a list of resources that I've compiled over time...

http://www.delicious.com/homer6/php+extension

Also, I'd highly recommend reading Sara Goleman's book:

http://blog.simonholywell.com/post/1156691738/15-excellent-resources-for-php-extension-development

Hope that helps...

Upvotes: 0

Raptor
Raptor

Reputation: 54242

Floating point numbers have limited precision. Although it depends on the system, PHP typically uses the IEEE 754 double precision format, which will give a maximum relative error due to rounding in the order of 1.11e-16. Non elementary arithmetic operations may give larger errors, and, of course, error progragation must be considered when several operations are compounded.

Additionally, rational numbers that are exactly representable as floating point numbers in base 10, like 0.1 or 0.7, do not have an exact representation as floating point numbers in base 2, which is used internally, no matter the size of the mantissa. Hence, they cannot be converted into their internal binary counterparts without a small loss of precision. This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8, since the internal representation will be something like 7.9999999999999991118....

So never trust floating number results to the last digit, and never compare floating point numbers for equality. If higher precision is necessary, the arbitrary precision math functions and gmp functions are available.

Quoted from : http://php.net/manual/en/language.types.float.php

To change the precision level of PHP , change the precision settings in php.ini

Upvotes: -2

Related Questions