Simulate floating point operations in J2ME (not accurate enough)
From: http://blog.9cbs.neet/mailbomb
It is well known that CLDC 1.0 does not support floating point numbers, ie, both of FLOAT and DOUBLE, but in the program, we often need to use some floating point numbers. At this time, you can use the Mathfp floating point log, but sometimes we do it very simple. For the requirements of the results, it is not accurate. At this time, we can use the following methods to simulate floating point operations.
The principle of simulating floating point operations is actually very simple, just first enlarges 10 integers of 10 integers and then operates.
For example, you need to calculate a circular perimeter, assuming that the radius of the circle is 6, then the circumference of the circle should be 2 * 6 * 3.14, and there is no floating point in CLDC1.0, and the number 3 is used instead of 3.14 errors. It can be handled like this:
(2 * 6 * 314) / 100
That is, the number of floating point is required first, such as 3.14 is extended by 100 times, and then the addition is 100 at the end of the operation.
The following is a simple implementation code:
/ / Calculate the circular permeth
INT r = 6;
INT pi = 314;
INT L = 0;
L = (2 * 6 * 314) / 100;
Then the variable L is the approximate circumference.
Note that the above calculation uses the math to 1 method, that is, regardless of the number of decimal parts. If you want more accurate (of course, it is still not accurate enough), you can use the four rounds, so the above code can be modified to:
/ / Calculate the circular permeth
INT R = 6;
INT pi = 314;
INT L = 0;
L = (2 * 6 * 314 50) / 100;
On the basis of the calculation results, add 50 to the round. If your decimal is 3, you need to add 500, and push it accordingly.
In this way, by the above manner, approximate floating point operations can be realized, although it is not very accurate, but it can still be applied to applications with low requirements.
Of course, with the popularity of CLDC1.1, these two data types will be brought to J2ME, and this content is not needed.