RMorrison Posted December 5, 2014 Share Posted December 5, 2014 I've written a small ELO class for calculating a players rating for a game league, and for the life of me I don't know why these calculations are coming up wrong. The Class: <?php class elo { function get_rating($current_rating, $expected, $win = true) { if ($win === true) { $r = $current_rating; $add = 32; $multiplier = 1 - $expected; $add = $add*$multiplier; $r = $current_rating + $add; return round($r,0); } else { $r = $current_rating; $add = 32; $multiplier = 0 - $expected; $add = $add*$multiplier; $r = $current_rating + $add; return round($r,0); } } function get_expected($player1_rating, $player2_rating) { $e = 1; $divider = 1; $power_of = $player2_rating - $player1_rating; $power_of = $power_of / 400; $power_of = 10^($power_of); $divider = $divider + $power_of; $e = $e/$divider; return round($e,2); } } Code for testing: $elo = new elo(); $a = 1000; $b = 1000; $chance['a'] = $elo->get_expected($a,$b); $chance['b'] = $elo->get_expected($b,$a); $chance['%a'] = $chance['a'] * 100; $chance['%b'] = $chance['b'] * 100; echo "Player A ({$a}) has a " . $chance['%a'] . "% Chance of winning.<br/>"; //should be 50% (0.5) echo "Player B ({$b}) has a " . $chance['%b'] . "% chance of winning.<br/>"; //should be 50% (0.5) $winpoints['a'] = $elo->get_rating($a, $chance['a']); //should return 1016 $winpoints['b'] = $elo->get_rating($b, $chance['b']); //should return 1016 echo "Player A's new rating will be {$winpoints['a']} if they win.<br/>"; echo "Player B's new rating will be {$winpoints['b']} if they win.<br/>"; $losspoints['a'] = $elo->get_rating($a, $chance['a'], false); //should return 984 $losspoints['b'] = $elo->get_rating($b, $chance['b'], false); //should return 984 echo "Player A's new rating will be {$losspoints['a']} if they lose.<br/>"; echo "Player B's new rating will be {$losspoints['b']} if they lose.<br/>"; The output i'm getting is as follows, when they should match what i've stated above (as done on paper with a calculator) Player A (1000) has a 9% Chance of winning. Player B (1000) has a 9% chance of winning. Player A's new rating will be 1029 if they win. Player B's new rating will be 1029 if they win. Player A's new rating will be 997 if they lose. Player B's new rating will be 997 if they lose. I've tried re-writing the calculation over and over but just can't get it. I used the Wikipedia page to get my calculations. I've not added K Factor yet but I will be at some point. Quote Link to comment Share on other sites More sharing options...
Solution mac_gyver Posted December 5, 2014 Solution Share Posted December 5, 2014 10^($power_of) the problem is most likely in that bit of coding. in programming languages, and specifically php, the ^ is not a exponential operator, it is in fact a bit wise exclusive or operator. you would need to consult the php documentation to find the proper exponential operator. Quote Link to comment Share on other sites More sharing options...
RMorrison Posted December 5, 2014 Author Share Posted December 5, 2014 Thanks I never thought of that. For reference for future people the function is pow() Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.