Skip to content

Instantly share code, notes, and snippets.

@bcremer
Last active January 29, 2019 14:59
Show Gist options
  • Save bcremer/80d1d53aca40aebe12422ae1c0dc4a96 to your computer and use it in GitHub Desktop.
Save bcremer/80d1d53aca40aebe12422ae1c0dc4a96 to your computer and use it in GitHub Desktop.
Why IEEE 754 floating point numbers are a mess to debug
<?php
// Live Demo: https://3v4l.org/GWJFZ
// see also: http://0.30000000000000004.com/
var_dump(0.1 + 0.2); // float(0.3)
var_dump(0.3); // float(0.3)
var_dump(0.1 + 0.2 == 0.3); // bool(false)
// Set the number of significant digits displayed in floating point numbers.
ini_set('precision', 18);
var_dump(0.1 + 0.2); // float(0.300000000000000044)
var_dump(0.3); // float(0.299999999999999989)
var_dump(0.1 + 0.2 == 0.3); // bool(false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment