• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Terabyte88

How does java find sine and cosine?

8 posts in this topic

Ok this question has been bugging me for months now. How does java find sine and cosine? I’m working on trying to make a game that is a simple platformer something like super Mario or Castlevania. I attempted to make a method that would rotate an image for me and then resize the JLabel to fit that image. I found an algorithm that worked and was able to accomplish my goal. However all I did was copy and past the algorithm any one can do that I want to understand the math behind it. So far I have figured everything out except one part. The methods sin and cos in the math class. They work and I can use them but I have no idea how java get its numbers.

 

It would seem there is more then one way to solve this problem for now I’m interested in how java does it. I looked into the taylor series but I’m not sure that is how java does it. But if java does use the taylor series I would like to know how that algorithm is right all the time (I am aware that it is an approximation). I’ve also heard of the CORDIC algorithm but I don’t know much about it as I do with the taylor series which I have programmed into java even though I don’t understand it. If CORDIC is how its done I would like to know how that algorithm is always right. It would seem it is also possible that the java methods are system dependent meaning that the algorithm or code used would differ from system to system. If the methods are system dependent then I would like to know how Windows gets sine and cosine. However if it is the CPU itself that gets the answerer I would like to know what algorithm it is using(I run an AMD Turion II Dual-Core Mobile M520 2.29GHz).

 

I have looked at the score code of the Math class and it points to the StrictMath class. However the StrictMath class only has a comment inside it no code. I have noticed though that the method does use the keyword native. A quick Google search suggest that this keyword enables java to work with other languages and systems supporting the idea that the methods are system dependent. I have looked at the java api for the StrictMath class (http://docs.oracle.com/javase/7/docs/api/java/lang/StrictMath.html) and it mentions something called fdlimb. The link is broken but I was able to Google it (http://www.netlib.org/fdlibm/).

 

It seems to be some sort of package written in C. while I know java I have never learned C so I have been having trouble deciphering it. I started looking up some info about the C language in the hopes of getting to bottom of this but it a slow process. Of cores even if did know C I still don’t know what C file java is using. There seems to be different version of the c methods for different systems and I can’t tell which one is being used. The API suggest it is the "IEEE 754 core function" version (residing in a file whose name begins with the letter e). But I see no sin method in the e files. I have found one that starts with a k which I think is sort for kernel and another that starts with an s which I think is sort for standard. The only e files I found that look similar to sin are e_sinh.c and e_asin.c which I think are different math functions. And that’s the story of my quest to fiend the java algorithms for sine and cosine.

 

Somewhere at some point in the line an algorithm is being called upon to get these numbers and I want to know what it is and why it works(there is no way java just gets these numbers out of thin air).

0

Share this post


Link to post
Share on other sites


DiegoSLTS, on 03 Apr 2014 - 02:16 AM, said:

Anyway, a common way to implement those functions to be faster is by lookup tables, having the sin and cos value for a range of radian values and that range divided in really small steps.

This is no longer true. It is much more costly to perform a lookup on a huge table than it is to calculate a trigonometric function.

 

This +1.  This was true back when wee were using 486s and Pentium 1s but the cache miss that using the lookup table would incur is now more expensive than the calculation.

 

As for how Java calculates sin cos etc.. You simply don't need to know.  If you want to learn the CORDIC algorithm or Taylor series to improve your own mathematical knowlege go ahead but, as far as I know the implementation of sin and cos is platform independant.  The Sun Runtime could be using CORDIC whilst Apples runtime could be using a Taylor Series.  The Windows implementation could be calculated by the OS whilst the Android implementation could be done in hardware. 

Could the results differ from system to system? yes 

Does it matter? No they'll be close enough so that you wouldn't notice.

1

Share this post


Link to post
Share on other sites

But if java does use the taylor series I would like to know how that algorithm is right all the time (I am aware that it is an approximation).

 

The trick here is that all floating point values are approximations.

You just have to make sure your numerical solutions' error is less then the accuracy of the number, and the answer will be "exact" (as exact as it can be)

Edited by Olof Hedman
1

Share this post


Link to post
Share on other sites

This is no longer true. It is much more costly to perform a lookup on a huge table than it is to calculate a trigonometric function.
Not in the case of Java standard Math class. It uses doubles for most of its operations, using a lookup table is actually quite faster than doing a cos/sin on a double. That's the reason why LibGDX has its own sin/cos/atan2 lookup tables (it's even worse on mobile devices).

 

Hell, IIRC that old IdTech inverse square root is faster than doing a Math.sqrt on a double. I'll should test it again sometime (Java micro benchmarks are a PITA to get right).

1

Share this post


Link to post
Share on other sites
I did a bit of research, and it appears that Java SE provides two math libraries: StrictMath and Math. StrictMath uses a software implementation, whereas Math may use intrinsics as dictated by the JVM at runtime. If an intrinsic isn't available for a specific function, Math will call StrictMath's equivalent.

In short, use StrictMath if determinism is more important than speed, or use Math if speed is more important than determinism. Edited by fastcall22
0

Share this post


Link to post
Share on other sites

I did a bit of research, and it appears that Java SE provides two math libraries: StrictMath and Math. StrictMath uses a software implementation, whereas Math may use intrinsics as dictated by the JVM at runtime. If an intrinsic isn't available for a specific function, Math will call StrictMath's equivalent.

In short, use StrictMath if determinism is more important than speed, or use Math if speed is more important than determinism.

 

It is up to the implementation for both classes, the only difference is the required accuracy. (StrictMath must give the exact same result on all platforms(Oracle has a reference library with a compliant software implementation). For the normal math class the error must be less than 0.5-2ulp (varies between methods in the class).

 

on x86 StrictMath uses a software implementation and Math does a software argument reduction (if you pass big arguments to your trig functions) before using the native instruction. a comliant JVM is free to handle things differently on architectures with more accurate trig instructions.

1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0