Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Trig functions

Greenhorn
Posts: 10
Ok, I've been trying to to find a way of using trig functions in my code, but I've been having nothing but problems.
I've been using the henson Float class, available at:
http://henson.newmail.ru/j2me/Float.htm

But the answers it gives me for the cos and sin functions are pure nonsense. I'm trying to convert the degrees to radians and then run the cos and sin functions on them.
It seemed to work fine for a test value of 60 degrees (1.047 radians), giving me the approximately correct results of cos 1.047 = 0.5001 and sin 1.047 = 0.866
But when I try it with anything else the results are utterly wrong. For example 45 degrees (2 radians), instead of getting 0.707 for both it gives me cos 2 = -0.416 and sin 2 = 0.909 ?

Why does it work for only the one value ?
Has anybody got this float class to work, ever ?
Has anybody got any alternatives ?
Please help me , I'm getting annoyed and desperate for a solution. I've also tried using MathFP, but that kept giving me wrong class name error.

Ranch Hand
Posts: 704
Why not use the toRadians(double angdeg) method of the java.lang.Math class ? There also happen to be cos(double a) and sin(double a) methods in this class.
What is the reason for calculating using floats instead of doubles ?

Andy Robinson
Greenhorn
Posts: 10
I was under the impression that j2me didn't support the double data type, although I can't for the life of me remember why . Thanks for the suggestion I'll give it a try.

Ranch Hand
Posts: 3061
You are correct that CLDC 1.0 and MIDP 1.0 don't have support for float or double values. I haven't worked with J2ME for a while, though, so I don't know if MIDP 2.0 (or more recent versions) have support for double or not.

Layne

Layne Lund
Ranch Hand
Posts: 3061
After a little more search, I found out that it is CLDC 1.0 that specifically rules out floating point arithmetic. Since MIDP is built on CLDC, it just inherits this limitation. Perhaps more recent versions of CLDC have introduced floating point arithmetic, though.

Sorry that this doesn't answer your quesiton completely, but I just wanted to clarify that such limitations DO exist.

Layne

Andy Robinson
Greenhorn
Posts: 10
Thanks for the help guys but I managed to sort out my original problem.
It was just a stupid little error on my part. From now on all by variables are getting very distinctive names :roll:

 Don't get me started about those stupid light bulbs.