0

my request input 0.08 but shows 0.08000000000000000166533453693773481063544750213623046875 on building var_dump why?

when I pass 50 it shows 50 and it works perfectly.

$result_query->where("height, ">=",0.08);
$result_query->where("height", "<=",99.99);
$result = $result_query->count();
echo "<pre>";
$data = var_dump($result_query->getBindings());

print_r($data);

I already passed 0.08 as the float value.

array(7) {
  [0]=>
  float(0.08000000000000000166533453693773481063544750213623046875)
  [1]=>
  int(50)
  [2]=>
  float(0.08000000000000000166533453693773481063544750213623046875)
  [3]=>
  float(99.9899999999999948840923025272786617279052734375)
  [4]=>
  int(1)
  [5]=>
  string(1) "0"
  [6]=>
  int(0)
}

Expected

array(7) {
  [0]=>
  float(0.08)
  [1]=>
  int(50)
  [2]=>
  float(0.08)
  [3]=>
  float(99.99)
  [4]=>
  int(1)
  [5]=>
  string(1) "0"
  [6]=>
  int(0)
}

I have also set. ini_set('precision', 8);

my hosting is in sitegroud cloud server.

Nilesh patel
  • 1,216
  • 2
  • 14
  • 38
  • 3
    There is no way to define 0.08 or 99.99 in floating point without an error margin. – trincot Mar 29 '23 at 19:54
  • @trincot what do you mean by error margin? – Nilesh patel Mar 30 '23 at 08:57
  • I mean that floating point can only represent a finite number of numbers, and this is notably a "problem" when the binary representation of a non-integer number has a periodic repetition of digits. Like 0.1 in binary is 0.00011001100110011... going on infinitely. A floating point representation will only represent up to a certain maximum number of these binary digits (53 in 64-bit floating point), and so it doesn't actually represent the intended number, but a number "close" to it. – trincot Mar 30 '23 at 09:08
  • @trincot how can we solve this, i also set PHP variable precision to 8, this in PHP Laravel and this is not cause problem in my local server. – Nilesh patel Mar 30 '23 at 09:23
  • Either change your logic so it works with integers only (best option), or ignore the output you get from printing the values with `print_r` and the like, and when do want proper output, make sure to format them, like with `number_format`. – trincot Mar 30 '23 at 09:37
  • @trincot problem is height stored in mysql database as float(16,2) while I write a query in Laravel php it gives a result but not whose height is 0.08 because of 0.08000000000000000166533453693773481063544750213623046875 – Nilesh patel Mar 30 '23 at 10:06
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/252873/discussion-between-nilesh-patel-and-trincot). – Nilesh patel Mar 30 '23 at 10:10

0 Answers0