Skip to content

Instantly share code, notes, and snippets.

@bagart
Created March 1, 2018 21:25
Show Gist options
  • Save bagart/13067cdda2730dbd26e59fffdf5a8c21 to your computer and use it in GitHub Desktop.
Save bagart/13067cdda2730dbd26e59fffdf5a8c21 to your computer and use it in GitHub Desktop.
work with float
<?php
// https://3v4l.org/0Fonp
$x = 1.999999999999999;
$y = 2;
var_dump([
'float: 1.999999999999999' => [
'serilize' => [$x, 'orig' => serialize($x), 'int' => serialize((int)$x), 'round' => serialize(round($x)), $x < 2],
'int' => [(int)$x, (int)$x < 2],
'string'=>[(string)$x, (string)$x < 2],
'round' => [round($x), round($x) < 2],
],
'int: 2' => [
'serilize' => [$y, 'orig' => serialize($y), 'int' => serialize((int)$y), 'round' => serialize(round($y)), $y < 2],
'int' => [(int)$y, (int)$y < 2],
'string'=>[(string)$y, (string)$y < 2],
'round' => [round($y), round($y) < 2],
],
]);
@bagart
Copy link
Author

bagart commented Mar 1, 2018

Output for 7.1.0 - 7.2.2
array(2) {
  ["float: 1.999999999999999"]=>
  array(4) {
    ["serilize"]=>
    array(5) {
      [0]=>
      float(2)
      ["orig"]=>
      string(20) "d:1.999999999999999;"
      ["int"]=>
      string(4) "i:1;"
      ["round"]=>
      string(4) "d:2;"
      [1]=>
      bool(true)
    }
    ["int"]=>
    array(2) {
      [0]=>
      int(1)
      [1]=>
      bool(true)
    }
    ["string"]=>
    array(2) {
      [0]=>
      string(1) "2"
      [1]=>
      bool(false)
    }
    ["round"]=>
    array(2) {
      [0]=>
      float(2)
      [1]=>
      bool(false)
    }
  }
  ["int: 2"]=>
  array(4) {
    ["serilize"]=>
    array(5) {
      [0]=>
      int(2)
      ["orig"]=>
      string(4) "i:2;"
      ["int"]=>
      string(4) "i:2;"
      ["round"]=>
      string(4) "d:2;"
      [1]=>
      bool(false)
    }
    ["int"]=>
    array(2) {
      [0]=>
      int(2)
      [1]=>
      bool(false)
    }
    ["string"]=>
    array(2) {
      [0]=>
      string(1) "2"
      [1]=>
      bool(false)
    }
    ["round"]=>
    array(2) {
      [0]=>
      float(2)
      [1]=>
      bool(false)
    }
  }
}

Older php version - other result:

      string(21) "d:1.9999999999999989;"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment