Wednesday, 8 of September of 2010

Tag » short

Concept of float datatype in j2me

Nearly everyone would feel pains for float datatype in j2me. This article will give you a broad concept to implement or more precisely simulate float in j2me.

First of all, how a float data look like?

78.98, 34.98885, 3.141759 …. and like this.

If you look closer, you will find every float has very small variable unit.

Say 78.98. Its unit is 0.01.  Similarly 3.141759. Its unit is 0.000001.

To implement in j2me, we will have smallest unit is 1 and also this is fixed. So what to do?

First of all for every data we will need to pre-assume maximum length. This is so because it will help to choose appropriate type like short, int or long.

int speed = 0;

//Say we need speed similar to 0.5.

Next is enlarge our data as per necessity.

Now,

speed = speed+1;

To get simulated speed of 0.5, we need speed to divide by 2. So the next step will be…

speed = speed / 2;

Actually this will give you 2 consecutive same values like 1,1,2,2,3,3,4,4,….

The same concept will work for larger values.

That’s it for now.


Do your comment