tags:

views:

21

answers:

2

How could I use Math.asin() (.. etc.) in a J2ME application?

(I've looked at Real Java (and it looks like it could do this) but it says I should avoid converting from a string. How could I create a new Real from a double value?)

A: 

It depends on version of target CLDC API.

  1. CLDC 1.0 doesn't supports any floating point operations (not saying asin/acos/atan). But there are some 3rd party developed packages/API's which supports FP operations e.g. MicroFloat
  2. CLDC 1.1 support FP operations, but still it's lacking asin/acos/atan. You can implement it yourself - it's relatively easy. Try to google and find alternate java sources for acos/atan/asin
barmaley
A: 

Since MIDP 2.0 this should work:

public static double asin(double a)
{
    // -1 < a < 1
    // The function isn't very precise
    final double epsilon=1.0E-7; // Use this to adjust precision
    double x=a;
    // Newton's iterative method
    do x-=(Math.sin(x)-a)/Math.cos(x);
    while (Math.abs(Math.sin(x)-a)>epsilon);
    return x;
    // returned angle is in radians
}

But hey, that Real - Java looks very nice. You definitely should use it.
If you assign the number using String only one or a few times, that won't affect your application's speed.

BlaXpirit