"kbenson" wrote:
"Division [...] is never done at the integer level: when both operators are integers, the operation is done as float with a float result."
[...] can be somewhat problematic due to floating point rounding errors. You CAN get around it, but only by going explicitly to double precision. You will experience the same rounding errors in double precision if your numbers are large enough. I'm not sure how to force integer division.
Yes, quite right you are - in B/S division of integers is always done as floats. So was the case four years ago and so it is today (and likely forever). It's been so long, you likely moved to better pastures and don't care about bitwise operations in BS no more - but i was looking for discussion of right shift in the forum and this is about the only relevant post, so i will drop my note here.
Some right shifts can be done using float division - we just have to be vewy vewy careful. First, how much precision does a float have? An educated guess (confirmed by experiments) would be that a B/S float is an
IEEE-754 "single", which is stored in 4 bytes and has 24 bits of precision.
Here is a demonstration - i am starting with &h1000000, which is the 25-bit number 0b1,00000000,00000000,00000000 - so one bit of precision is bound to be lost if we store it in float - and i also divide by 1 (extreme case that shouldn't have changed the number but forces it into float):
BrightScript Debugger>for i = 0 to 255: x = &h01000000 or i: y = int(x / 1): ? i, y and &hFF, x, y: end for
0 0 16777216 16777216
1 0 16777217 16777216
2 2 16777218 16777218
3 4 16777219 16777220
4 4 16777220 16777220
5 4 16777221 16777220
6 6 16777222 16777222
7 8 16777223 16777224
8 8 16777224 16777224
9 8 16777225 16777224
10 10 16777226 16777226
11 12 16777227 16777228
12 12 16777228 16777228
13 12 16777229 16777228
14 14 16777230 16777230
15 16 16777231 16777232
16 16 16777232 16777232
...
250 250 16777466 16777466
251 252 16777467 16777468
252 252 16777468 16777468
253 252 16777469 16777468
254 254 16777470 16777470
255 0 16777471 16777472
BrightScript Debugger>
Note how all even numbers come true through the ordeal, where all odd numbers come short by 1 (except last one that is bumped +1).
What are the practical implications of this? Well, if i need 24 or less bits from the number after shifting - then division is the right operation. For example, if i need to shift right by 8 bits - "/ &h100"; by 16 - "/ &h10000" and so on. And i bet division will be faster to do than a loop bit by bit. Or you can just use double and rely that its
53 bits of precision will truthfully handle the 32 bits of an int.
One more thing - a new operator MOD was added for Brightscript 3 (sometime in 2011 i believe), it is the "modulo" operation - "x mod y" returns the remainder of dividing x by y; e.g. 8 mod 3 = 2. You can take advantage of that by adding the following two lines near the beginning of your routine - in a way that is "did i get the right result by dividing? if yes - great, if no - let's use alternative way":
total = int(num / mult)
if total * mult + num mod mult = num then return total