I'm kinda new to Java so bear with the ignorance as I'm still in the "experimental" stage (currently experimenting with eclipse and how inheritance in Java works).

I generally use floats(or Fixed-point) for all my gaming data but noticed that almost all Math.* constructs return doubles. I know that on 32-bit machines, floats are generally faster than doubles (I have a 32bit machine) but I don't know if this is true in Java being emulated (or even on jit). I also need a lot of float cast to make things work. Does casting have a speed penalty in Java since it's emulated?

I don't know why the Math.* stuff returns mostly doubles, but generally we all simply cast those doubles into floats.

With "creating your own Math class" Mac70 probably means, you could create your own Math class ontop of Math. So it uses Math.sin, but actually caches the results in an array, so you could access them without actually computing them. LibGDX has done something similar: "MathUtils" on github

You mean LUTs(LookUp Tables)? I used to do that during the DOS days and when coding for the Nintendo DS but I thought FPU calls are now much faster than RAM access these days. I'm assuming that Math.* directly calls the FPU for trig funks.

Thanks!

Just found out that casting from a double to float has a speed penalty. ;*(

@matheus23: kids and their angles. I don't see why you need a table lookup for atan2.

@SwampChicken: Basic ALU ops are very fast. Memory access is very slow. The fastest memory read is cached 32-bit data chunk from L1 into a register, which takes 5 cycles. A cache miss might (there a abillion factors here) cost you around 500 cycles. Take a sandybridge machine which (again this is horseshoes and hand-grenades) can retire 4 ops per cycle, you perform 20 basic ops for the first case (simple load of 32-bits of cached data) and around 2000 in the second.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org