Uncategorized


PHP floats are not created equal

I was given a bug report today and after digging into the problem I found that two floating point numbers that appeared to be equal were not.

Here is a little code and the output. At the bottom is the actual value of each.

var_dump($paymentAmount);
var_dump($amountDue);

if($paymentAmount > $amountDue) {
echo 'is greater';
} else {
echo 'equal or less';
}
printf("%.40f\n", $paymentAmount) . ' ';
printf("%.40f\n", $amountDue)

float(51.01) 
float(51.01) 
is greater
51.0099999999999980104803398717194795608521 
51.0099999999999909050529822707176208496094

PHP acknowledges the issues here under “comparing floats” http://php.net/manual/en/language.types.float.php

The PHP website provides a way to test the number for equality by limiting them to 5 decimals –

To test floating point values for equality, an upper bound on the relative error due to rounding is used. This value is known as the machine epsilon, or unit roundoff, and is the smallest acceptable difference in calculations.

$a and $b are equal to 5 digits of precision.

$a = 1.23456789;
$b = 1.23456780;
$epsilon = 0.00001;

if(abs($a-$b) < $epsilon) {
    echo "true";
}

RetroPie emulationstation not saving game progress 2

I spent part of my Saturday on this issue where I’d make progress in a game and save it within the game, but upon powering down the raspberry pi and returning, the progress would be gone. The problem is if you’re not exiting the game “cleanly”, the the emulation station will not write the game progress to the SD card, even though you’ve saved it in the game. When you configure a controller with RetroPie, it does not present you with a button combination to exit the game, which will initiate a write to the SD card. You can manually add a button combination for your controller by logging into the terminal and going to /opt/retropie/emulators/RetroArch/configs/ , then selecting your controller config, and adding the below two lines:

input_enable_hotkey_btn = 8
input_exit_emulator_btn = 9

Within the config, you’ll see each button on your controller is assigned a number. On my NES controller the “select” button was 8 and the “start” button was 9. With these lines added to the config file, I now press select and start at the same time to exit the game cleanly (which saves progress to the SD card).

Also, you can configure an automatic write to the SD card at an interval. Edit your retroarch.cfg file and uncomment the line “autosave_interval” and set it’s value to an integer representing a number of seconds. For example

autosave_interval = 5

Will save your progress to the SD card every 5 seconds.


Things I’m reading

http://www.dbasquare.com/2012/04/04/how-important-a-primary-key-can-be-for-mysql-performance/

http://coding.smashingmagazine.com/2012/06/06/all-about-unicode-utf8-character-sets/

http://misko.hevery.com/2008/07/30/top-10-things-which-make-your-code-hard-to-test/

http://www.littlehart.net/atthekeyboard/

http://www.klauskomenda.com/code/javascript-inheritance-by-example/

http://blog.scoutapp.com/articles/2009/07/31/understanding-load-averages

http://martinvalasek.com/blog/pictures-from-a-developers-life


Windows 7 cannot select default program – not working 1

Suddenly today my files lost association to the proper program (zend studio) on my windows 7 machine. I right clicked the file and chose “open with” and navigated to the program, double clicked it. Unfortunately the program never got added to the list of recommended or available programs in the previous window, so I could not assign it as the default program. However, I was able to navigate to other programs and assign them as the default, so the problem existed with the program I was trying to select. I believe there was an issue with the registry keys related to this program. I ended up running the program installer again and doing a “repair” and then it worked.