posted 13 years ago

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!

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!

Dave Taubler<br />Specializing in <a href="http://taubler.com/articles/" target="_blank" rel="nofollow">Java and Web Development</a>

posted 13 years ago

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.

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.

A good workman is known by his tools.

dave taubler

Ranch Hand

Posts: 132