Monday August 01, 2005
There's a piece of historical baggage in the definition of sin/cos
that is much older than just "whatever the 8087 chip happened to do": why
are the sin and cos functions defined based on parameters
that are periodic in a transcendental number? Having the period be 2*pi
is based on decades (centuries!) of mathematical standard practice. This
whole problem with sin and cos would go away if only the
period had a nice clean representation in floating point. For example,
either degrees or turns (1 turn == 360 degrees) would be great. In
particular, if the parameter to sin/cos were turns then argument
reduction would be easy: just throw away the integer bits. Then you could
do a table lookup based on extracting mantissa bits.
What is especially ironic is that if you grep through piles of source code you'll find a huge number of calls that look roughly like sin(angle*(2*Math.PI/360)). This has a necessarily slightly inaccurate value of pi, which is likely to be exactly the same value as the one in the 8087's sin/cos implementation - the two errors come pretty close to canceling since the argument is divided by the value of pi used in the implementation of sin/cos.
Update: Yes, I understand that there are lots of deep, important mathematical reasons that sin & cos are defined in terms of 2*pi. The point I was trying to make is that if you peel open the covers of an implementation of sin/cos, 2*pi is a very difficult period to cope with. At the same time, if you look at software that uses sin/cos, you'll find that a huge fraction of them use something other than 2*pi (degrees are real common), and convert when invoking sin/cos.
Posted by Stephen Booth on August 01, 2005 at 02:09 PM PDT #
Posted by simpleguy on August 01, 2005 at 05:43 PM PDT #
Most of the time I really enjoy reading Gosling's blogs, but this one is different. There are natural mathematical reasons why sin/cos are defined with periods of 2*PI. It wasn't that a bunch of people sat around and said, "How can we define these functions so that we can confuse everyone." If you redefine them differently, then much of the rest of mathematics dealing with these functions changes, including the simple fact that the derivative of the sine function is cosine function.
If you want a different period, it is certainly reasonable to add new functions to the MATH API, but just don't use them to replace the standard trig functions that have stood the test of time and that everyone has learned in school for many, many years (before you were born).
Posted by John Moore on August 02, 2005 at 03:59 AM PDT #
Posted by Carl on August 02, 2005 at 12:08 PM PDT #
Posted by Ranjit Mathew on August 04, 2005 at 10:38 PM PDT #
Posted by Marcel Lanz on August 08, 2005 at 10:47 PM PDT #
I agree. 8087 designers were not very smart implementing things using radians. They should have used, say, binadians (define: 2**X binadians = 2*PI radians). Then shifting to [-PI/4,PI/4] would be a piece of cake and things should be faster and more accurate.
Anyway the problem is making computer maths look like real maths. Accuracy is important, but correctness is much more important in computer maths. After all you want to be sure that that huge set of Java (Fortran/C/C++) files are really solving your equations.
That's what we need in Fortress. And, yes, Fortress should use binadians internally, but should also let us use radians in our source code.
Posted by Antonio on August 11, 2005 at 01:26 AM PDT #
Thus, it so happened, in 1896, that a physician of Indiana in his Solitude had given much thought to the age-old problem of quadrature of the circle. One of his thoughts was that this task might be simplified if one could only find a more propitious value for Pi, such as 3.2.
The good doctor wanted to make his great invention available to all schoolchildren, real estate agents, bureaucrats, and their likes, in Indiana and in the Great World outside – for a consideration, of course.
With visions in his mind of great tsunamis of dollars rolling in, he let it be known – being a good patriot – that he intended not to levy any fees on the use of his Pi in his home state, reserving for himself only a small cut of monies accruing from abroad.
Moving in the very best circles in Indiana, Doc Pi managed to square, if not actually a geometric circle, at least some exalted personage to launch a state of Indiana bill laying down that Pi be henceforth equal to 3.2, exactly.
The bill was carried unanimously by a 67 to 0 vote in the House of Representatives, and thus was on its way to becoming law – to wit, a law of Indiana, not a law of nature.
By a fortunate circumstance, a Purdue University professor of mathematics happened to be visiting the statehouse to lobby for an appropriation for his alma mater when the Senate was about to debate the proposed mathematic lagislation.
During a lull in the debate he succeeded in convincing some of the senators of the horrendous absurdity of the bill – they might just as well pass a law that the Earth is flat (at least in Indiana) – and the debate of the bill was deferred „until a later date“.
</em> -- from „Mathematics: From the Birth of Numbers“ by Jan Gullberg
Posted by eigo on August 14, 2005 at 09:19 AM PDT #
package fasttrig; import static fasttrig.Time.*; import static java.lang.Math.*; public class Main { private static final int nLoops = 10000000; private static final int sinScale = 100; private static final int[] sinTable = new int[ sinScale ]; static { for ( int i = 0; i < sinScale; i++ ) { sinTable[ i ] = (int)(sin( i * PI / sinScale ) * sinScale); } } public static final Timable accurate = new Timable() { public double time() { double sum = 0; for ( int i = 0; i < nLoops; i++ ) { sum += sin( i * PI / nLoops ); } return sum; } public String toString() { return "Math.sin"; } }; public static final Timable fast = new Timable() { public double time() { double sum = 0; for ( int i = 0; i < nLoops; i++ ) { final int index = i * sinScale / nLoops; final int sinApprox = sinTable[ index ]; sum += (double)sinApprox / sinScale; } return sum; } public String toString() { return "Table"; } }; public static void main( final String[] notUsed ) { time( accurate ); time( fast ); } }The output is: The fast version runs about ten times quicker on my machine (but is 14 places less accurate). I haven't shown the source for <code>Time</code> and <code>Timable</code>, to keep the code short, but you can guess them :) Again taking the lead from embedded applications, if it is OK for trig to be 5 digits then the rest can be 5 digits. Therefore recode the application in fixed point (scalled int) and it will be much faster.Posted by Howard Lovatt on August 15, 2005 at 07:59 PM PDT #
Posted by John Harby on August 18, 2005 at 07:10 AM PDT #
Posted by 1 on August 20, 2005 at 11:07 AM PDT #
For the purposes of math, sin/cos really *need* to have a 2pi period. The simplest way to see this is that (IIRC):
That little relation neatly sums up the relationship between i, e, and pi. It's right at the heart of what complex numbers are, and how they work; if cos and sin have any period other than 2pi, calculus (or at least complex analysis) becomes utterly hideous. It's really not fair to say that the motivation for having sin and cos in radians is merely "historical;" "mathematically natural" would be better.But in software, what's mathematically natural isn't always what's most useful; we usually use sin and cos for doing polar coordinate sorts of things, and the period is arbitrary.
It might make sense for math libraries to define cos1() / sin1(), whose periods are both 1 (e.g. cos1(x) == cos(x * 2pi)). If it's really a performance problem, I wonder why they don't already?
Posted by Paul Cantrell on August 22, 2005 at 10:52 PM PDT #
Posted by 84.218.62.105 on August 29, 2005 at 12:55 PM PDT #
Posted by Harjit on September 01, 2005 at 08:05 AM PDT #
But I think I might be dividing by zero.
Get it? wink, wink.
mary
Posted by mary on September 07, 2005 at 02:14 PM PDT #
Posted by Joel Berman on September 17, 2005 at 08:50 AM PDT #
Posted by Antonio on September 17, 2005 at 09:54 AM PDT #
It's not that he doesn't understand what radians are or their importance to mathemetics. He's saying that there are a significant number of programs that naturally work with non-radian angular measurements (degrees, grads, revolutions, etc), and only convert to radians in order to access Java's trig functions.
For those programs, argument reduction into the range required by Java (i.e. [-90,90] degrees) can be achieved with simple integer computations BEFORE they apply the appropriate conversion factor (e.g. pi/180) and call the trig function.
Applying the conversion factor first, makes the argument reduction (WRT a transcendental modulus) extremely difficult. So much so that applications skip that step, forcing Java's library functions to deal with it.
If Java were to provide alternative trig functions (e.g. cos_degrees(), tan_degrees(), etc) which performed fast argument reduction MOD 360, then programmers could call the new trig functions for speed, and the current trig function for accuracy.
Furthermore, the fast-vs-accurate choice is not mutually exclusive. Applications which naturally use non-radian units will call the degree-based functions and get both.
Posted by Glynne Casteel on September 17, 2005 at 12:08 PM PDT #
Posted by thiyagu on September 20, 2005 at 08:46 AM PDT #
Posted by George Colpitts on October 08, 2005 at 03:59 PM PDT #
Posted by George Colpitts on October 03, 2006 at 01:25 PM PDT #
http://www.nizikaikun.com/
http://www.ms-online.co.jp/eshop/goods/ona_hole.php
http://www.ms-online.co.jp/eshop/goods/costume.php
http://www.ms-online.co.jp/eshop/goods/vibe.php
http://www.ms-shop.co.jp/shop/goods/goods.asp?category=5308
http://www.omochacha.com/
http://www.av-one.jp/zero/top.html
http://www.a-world.co.jp/
http://www.a-toy.ne.jp/
http://www.s-one-company.jp/
http://www.ec-life.co.jp/bath/index2.html
http://www.tbnetjapan.com/medlegal/
http://adultshop.co.jp/omocha.html
http://adultshop.co.jp/adultshop.html
http://adultshop.co.jp/enemagra.html
http://adultshop.co.jp/onahole.html
http://adultshop.co.jp/houkei.html
http://adultshop.co.jp/anal.html
http://adultshop.co.jp/denma.html
http://www.nicolas-dogs.com/
http://www.aqua01.net/
http://www.kabudayo.com/
http://www.fxf-business.com/
http://kaketayo.sakura.ne.jp/
http://www.11cash.net/
http://telink.jp/
http://www.complete-watch.com/
http://adultshop.co.jp/dutch.html
http://www.blyjapon.com/
http://www.achelabo.jp/
http://umanity.jp/
http://www.worldflower.net/rs/
http://furniture.michiookamoto.com/
http://www.blyjapon.com/
http://www.achelabo.jp/
http://www.open-japan.com/
http://www.open-japan.com/ideabox/index.php?category=beauty#top
http://www.open-japan.com/ideabox/index.php?category=dress#top
http://www.eic-av.com/
http://www.eic-av.com/list/fileIndex
http://www.saimu0.jp/
http://www.chasetokyo.com/charge.html
http://www.chasetokyo.com/whereabouts.html
http://www.chasetokyo.com/action.html
http://www.chasetokyo.com/immorality.html
http://www.chasetokyo.com/philippines.html
http://www.sigmac.jp/
http://www.tokei-biho.com/
http://www.rmtplusone.com/lineage2/
http://www.takumi-pg.com/
http://www.webtravel.co.jp/asia/chaina/
http://www.webtravel.co.jp/
http://www.trivy-system.com/Kekkon.htm
http://www.trivy-system.com/Sinyou.htm
Posted by thanhvn on December 18, 2008 at 07:15 AM PST #
http://www.0120128888.com/
http://www.wonder-up.net/
http://www.akasaka-hisyo.com/
http://www.ran-s.jp/
http://www.tokyo-living.com/jp.html
http://www.firstep.jp/
http://www.tb-k.com/
http://www.kw-ai.com/
Posted by sasasas on July 22, 2009 at 10:41 PM PDT #