Dealing cards - possible bug?

BlitzPlus Forums/BlitzPlus Programming/Dealing cards - possible bug?

Martin W(Posted 2010) [#1]
There are NumPlayers% players and NumCards% cards of which 80% are to be dealt evenly to the players, and 20% form a deck to be drawn on later in the game. NumCards% may be any of the values: 60, 80, 105, 135, 165, 200, 240, 280, 325, 375.
NumPlayers% may be 2 to 8. Roughly 80% of the cards are dealt,
i.e. Cards: 48, 64, 84, 108, 132, 160, 192, 224, 260, 300
Per player (ignoring 2, 4, as all are divisible by 2 and 4):
3 players: 16, 21*, 28, 36, 44, 53*, 64, 75*, 87*, 100
5 players: 10*, 13*, 17*,22*, 26*, 32, 38*, 45*, 52 , 60
6 players: 8, 11*, 14, 18, 22, 27*, 32, 37*, 43*, 50
7 players: 7*, 9*, 12, 15*, 19*, 23*, 27*, 32, 37*, 43*
8 players: 6, 8, 11*,14*, 17*, 20, 24, 28, 33*, 38*
* rounded to next higher integer in most cases.
BlitzPlus Code has the numbers to deal in an array calculated as
CardsPerHand% = Int((NumCards%*4)/(NumPlayers%*5)+0.5)
I've tried using Float() to convert the integers to floats and doing the calculation various different ways, but the actial numbers generated are different. I've also put the same coding into a spreadsheet, which calculates the numbers correctly as above,
using a formula =INT(C$2/$A6+0.5) where C$2 is the number of cards to be dealt for the pack of 80 cards and $A6 is the cell for 4 players.
BlitzPlus calculates these numbers differently: for 224 cards to be dealt of the pack of 280 cards, it comes up with 38 instead of 37.
In this case, CardsPerHand% = Int((280*4)/(6*5)+0.5)
=Int ((1120/30)+0.5) = Int(37.3333 + 0.5) = Int(37.8333) which should be 37 but comes out as 38. Is there a bug in BlitzPlus that causes incorrect division or does a "round" instead of an Int" function? If I reduce the 0.5 value I can get to 37, but then other values are wrong. If I change to: CardsPerHand% =
Int((Float(280*4)/Float(6*5)+Float(0.5)) it still produces 38. It only gets to 37 if the constant term is between 0.166 and 0.167, so it suggests that the value rounded up to 37.333+0.166 i.e. 37.499 rounds down, but 37.5003333...) is not truncated by the Int() function but instead rounded up. But Int works properly for other situations and if I change the constant term to 0.1666 to get it right here, it is wrong in numerous other places in the table, so what is going on here? Is there a bug in the Int() function?


Martin W(Posted 2010) [#2]
Additional notes - it's not so much a bug as a difference between the traditional usage of Int() to lop off the decimal part of a positive or negative number and Blitz's usage which is more or less the equivalent of Round() in other languages. I can get the correct result (since all these numbers are positive) by usinf Floor() instead of Int(). But one would have to use Ceil() to get the same result for negative numbers. It is fully explained in the BlitzPlus manual, but is not obvious to a former user of many other BASIC language versions that use the conventional Int() function.


Martin W(Posted 2010) [#3]
Additional Note: Ran through the asterisked subset above using Floor() instead of Int() and the results were 100% correct.


Stamm(Posted 2010) [#4]
yep if you want pecifically to round up or down you use ceil(var) respectively floor(var)