Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
UBYTE/FOR loop issue?
#3
Aha, I see - thanks for that. The thought had occurred to me that perhaps this was unavoidable, but it's nice to know for sure. Thanks for the info and the pointers!


While we're here and talking about types, could anyone tell me if there's a more elegant way to do this?

I have three UBYTES with values. I want to add them up, divide by three to get an average, and put the result into another UBYTE. However, I can't just do the maths with the UBYTES because the addition takes the value > 255 and information is lost.

I came up with this solution:
Code:
DIM x, y, z, q as UBYTE
DIM bigger as UINTEGER

x = 57
y = 127
z = 204

bigger = cast(UINTEGER, x)
bigger = (bigger + y + z) / 3
q = cast(UBYTE, bigger)
print q

... but it seems kind of messy. Is there a way to do that better?

I suppose the easiest way would be to store the three values as UINTEGERs to begin with and then cast to UBYTE at last minute, after the calculation?
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)