• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Lack of floating point, so multiply by powers of 2

 
dave taubler
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd seen a few posts here and there lamenting the lack of floating-point support in J2ME, particularly in CLDC 1.0, and fact that this can cause problems doing division (i.e. often you'll get results of 0 or 1 when you really wanted a decimal.) In response, there have been suggestions to multiply the numerator by a large power of 2 (e.g. 2048), and when you're done, divide the result by that power of 2.

I'm curious as to why a power of 2 should by used, as opposed to, say, a power of 10, or really just any large number. Does anyone have any insight here?

Thanks!
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since power of 2 don't need to do multiplication or division, but much more efficient shift operation.
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, if you multiply something by say, 2-to-the-5th power, you are shifting the bits 5 positions to the left. Hopefully we don't end up with too big a number for our primitive to hold!!! Yikes!

Dividing moves the bits back to the right. That will round everything down.

This trick will give extra precision. Not using a power of 2 could throw off the integrity of the precision.

Using the bit-shift operators would be even more efficient. I'm not sure if those are included in J2ME though.
 
dave taubler
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool; thanks for the explanation and extra advice! Trying to do trig within my cellphone apps is making do this:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic