3

In a Russian card game I'm trying to keep statistics of how often a player swears (says "bad words" - and we do have a lot of them in Russian language) in the following PostgreSQL table:

# select * from pref_chat order by swear desc;
           id            | swear | lines
-------------------------+-------+-------
 OK194281930260          |     3 |   153
 OK350321778615          |     2 |   127
 DE12770                 |     2 |   339
 OK122898831181          |     2 |    63
 OK349829847011          |     2 |   126
 OK215240745969          |     1 |    66
 OK459742722980          |     1 |    96

And I need to generate an integer number of this data - between 1 and 100 (overflowing is okay) - so that I can create the following "swear'o'meter:

enter image description here

So I'm trying (with PHP 5.3 at CentOS 6.2):

    $sth = $db->prepare('select swear, lines from pref_chat where id=?');
    $sth->execute(array($id));
    if ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
            $quotient = 100 * floor(20 * $row['swear'] / (1 + $row['lines']));
            print('<p><img src="https://chart.googleapis.com/chart?chs=700x100&cht=gom&chco=00FF00,FFFF00,FF0000&chxt=x&chxl=0:|Swearometer&chd=t:' . $quotient . '" width=700 height=100 alt="Swearometer"></p>)';
    }

Unfortunately I get zero - because PHP is probably doing an "integer division".

I've tried to prepend floor() to $row['swear'] and $row['lines'] to "cast" them to float - but this didn't help.

UPDATE:

Sorry, I had a typo in my original question... The $quotient is really 0, I print it out. I've also tried the following, but still zero:

$quot = 100 * floor(20 * $row['swear'] / (.1 + $row['lines']));
hakre
  • 193,403
  • 52
  • 435
  • 836
Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
  • Consider moving the computation from the PHP side to the database; they are exceptionally powerful tools for data crunching and this is a perfect example. – jmkeyes Mar 17 '12 at 21:24

2 Answers2

1

Well, $row['swear'] / (1 + $row['lines']) would always be < .05 given the numbers you list. Therefore, when you multiply by 20 and then floor you will very correctly get 0.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • No-no, it's a typo in my question text, sorry - I have updated it now. The $quotient is really 0, I have printed it out. – Alexander Farber Mar 17 '12 at 20:26
  • @AlexanderFarber: OK then, but still there's a very reasonable mathematical explanation. – Jon Mar 17 '12 at 20:31
  • Yes obviously, but my question is how to fix this, how to cast to float? – Alexander Farber Mar 17 '12 at 20:33
  • 1
    @AlexanderFarber: Cast what? You are asking for e.g. `floor(0.50)` and correctly getting back `0`. That's all there is to it. Perhaps you meant something like `floor(100 * $row['swear'] / (1 + $row['lines']))`? – Jon Mar 17 '12 at 20:34
0

@AlexanderFarber: No, sorry integer division in PHP is not giving zero may be you are missing something.Just use your usual division.

Khan
  • 16
  • 1