tools4j / decimal4j

Java library for fast fixed-point arithmetic based on longs with support for up to 18 decimal places.
decimal4j.org
MIT License
156 stars 17 forks source link

Trig functions: More a question of advice than an issue.... #9

Closed Cakey123445 closed 8 years ago

Cakey123445 commented 8 years ago

Hi, I'm looking to speed up a simulation style game and need to do some basic trigonometry sin/cos/tan, pythagoras/etc.

There didn't look to be any mention of how to go about this (sin/cos/tan) and so I was looking for some advice....

The app would store X/Y/Z positions in Decimal16f and then do various transformations/rotations (mostly 2d). I was thinking to pre-build simple sin/cos/tan lookup tables based around Decimal8f to keep the size of the tables down. 2*PI in Decimal8f would be just over 1600 elements per table and give enough accuracy for what I need(400 angles over 90 degrees).

I could store the angles in Decimal16f and go to double, through the normal math.sin/etc and back but that seems to defeat the object of the fixed point.

So the question is.... is there a better way I'm missing?

terzerm commented 8 years ago

Thanks for your interest in decimal4j.

The big question for me is --- why are you doing fixed point? For trigonometric functions and sort etc you have to round anyway. What is the benefit of fixed/decimal point over float/double with binary fraction?

In terms of implementation --- yes I can also think of the two ways you have proposed. (a) table lookup or (b) conversion to double, calculate function, convert back. There may be more efficient variants but that would be quite tricky to implement I assume.

rendaw commented 5 years ago

Maybe predictable calculation results? This could also probably be done by including your own double trig function implementations, but if the double format differed between architectures you might get different results anyways.

rendaw commented 5 years ago

I don't personally need this though since I'm only doing trig functions in transit.