1

I have a problem with the filter_var_array function, in particular with the 'FILTER_VALIDATE_FLOAT' flag, which returns me floats with way too many decimal places

Code first:

if (property_exists($this->rawRequest, $varName)) {
    $res = filter_var_array($this->rawRequest->$varName, FILTER_VALIDATE_FLOAT);
}

This is my code, where I validate parameters that I received from a client via JSON
The rawRequest is a stdClass, like the following (printed with var_export):

stdClass::__set_state(
     array(
        'Action' => 'CreateObject',
        'Template' => 'none',
        'LatLng' => array (
            0 => '48.14914653539144',
            1 => '11.577623201171932',
        )
)

After the 'LatLng' parameter was filtered with filter_var_array, the result looks like the following:

'LatLng' => array (
    0 => 48.149146535391433732957206666469573974609375,
    1 => 11.5776232011719315551090403459966182708740234375,
)

Why does this happen?

I even can not fix this with round(), typecasting to float, to string and back to float, I tried to subtract 0.00000001 and then adding 0.00000001 again, nothing helps

UPDATE:
I wrote following little test script on the same machine, same apache server:

<?php

$test = '48.158308230848306';
$test2 = (float) $test;
$test3 = floatval($test);

echo($test2);
echo('<br />');
echo($test3);
exit(0);

output is here:

48.158308230848
48.158308230848

THAT is what I expect
I retested it with my prod code and used floatval() and typecasting to float, but it DID NOT work. I don't know, the only difference is that in the prod code the input comes as json and I parse it with the following code:

json_decode(file_get_contents('php://input'))
skaldesh
  • 1,325
  • 3
  • 15
  • 37
  • This is what floating-point numbers do. If you want to store 48.14914653539144 as a floating-point value, it cannot be stored exactly, and instead you get another number. Duplicate of [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Thomas Padron-McCarthy Mar 17 '17 at 19:26
  • But, looking more closely at your numbers, are those correctly copied? Because your output numbers are very different from your input numbers, more than can be expected from normal floating-point representation errors. – Thomas Padron-McCarthy Mar 17 '17 at 19:33
  • oh no sorry, fixed the numbers, they were from different tests – skaldesh Mar 22 '17 at 18:54
  • I dont think it is a duplicate, I am well aware of this question. I am ready to hear how to solve my issue, since filter_var_array is just corrupting my string input and nothing I do afterwards fixes this weird float output I get – skaldesh Mar 22 '17 at 18:58
  • I know that floating point numbers are just approximations of decimals, I know that, but how do I round it up? – skaldesh Mar 22 '17 at 19:05

1 Answers1

3

So, I finally found the answer to my problem

The problem was never the filter_var_array() function, actually, the problem occurred far earlier.
After retrieving the POST parameters via file_get_contents("php://input") everything is still fine.
But when I decode the returned string with json_decode(), the float values are serialized in this process, resulting in the higher precision.

I found two ways to "solve" this:

  • Change the "serialize_precision" parameter in the php.ini (mine was per default 100, setting it to the same as my "precision" parameter was successful)
    I DO NOT RECOMMEND this approach, since the effects of changing this parameter might be devastating somewhere else
  • Enclose the float in the request in quotes
    So change in your JSON {"test": 48.58495135846} to {"test": "48.58495135846"}
    This prevents the serialization of the float, as it is effectively a string now

Hope this helps someone else, maybe this is already common knowledge, but I was tearing my hair out about this.

skaldesh
  • 1,325
  • 3
  • 15
  • 37