Java, 120 characters
package p;class C{double r;double c(double x,double c,long d,int i){return i>0?r+=c(x,-c/x/x,d/(4*i*i-2*i),--i)+c/d:r;}}
With comments and whitespaces:
C.java
/*
* Class 'C' (for 'Cosine').
* Copyright (C) 2017 Gerold 'Geri' Broser (geribro@users.sourceforge.net)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package p;
/** Class 'C' (for Cosine).
*
* @author Gerold 'Geri' Broser
* @version 17.04.21
* @see https://codegolf.stackexchange.com/questions/116705/the-pedants-cosine
*/
class C {
/** The added up results of the polynomial's terms.
*/
double r;
/** This one-liner method 'c' (for calculate) returns the part (i=n-1) of a
* cosine of a given angle 'x' that's calculated from the second term of a
* Taylor series of n polynomial terms onwards (or backwards until the
* second term [i=1], to be precise, see below).
*
* It achieves this by doing the following:
*
* ● It doesn't calculate the first term since it is always 1 anyway.
*
* ● It uses recursion for calculating the terms of the polynomial.
*
* ● It calculates from the rightmost term back to the leftmost. Such avoiding
* to keep the upper boundary stored till the end for the recursion's stop
* condition.
*
* ● It is supplied with values for the counter and denominator of the
* rightmost term at invocation. Such also the user can decide which
* library to take for power and factorial.
*
* ● It calculates the counters and denominators for each term from scratch
* at each recursion but uses the values calculated at the previous
* recursion. Such the new values can be calculated by using trivial
* parenthesis, division, multiplication, decrement and negation only.
* This doesn't only save characters but probably is also faster than
* power and factorial.
* [It could be made be even faster for x=2^n, n ∈ N, because we can
* use the unsigned right shift operator '>>>' instead of divisions then
* (see method 'calculateForXisPowerOfTwo()' of class 'Cosine').]
*
* @param x angle (independent variable) in radians
* @param c counter of last term specified by index 'i' including proper sign
* (see class 'CTest')
* @param d denominator of last term specified by index 'i' (see class 'CTest')
* @param i index of last term used in the calculation (=number of terms 'n'
* minus 1; Σ's upper boundary)
*
* @see https://en.wikipedia.org/wiki/Taylor_series#Trigonometric_functions
*/
// Copy the following three lines to immediately after the function header for testing:
// System.out.printf(
// "c(): x:%4.1f c:%24.17f d:%,19d i:%2d t:%40.35f%n",
// x, c, d, i, c / d);
// Position of 'i' is relevant here, since it is prefix-decremented inline!
double c( double x, double c, long d, int i ) {
return i > 0
? r += c(
x,
-c / x / x,
d / (4 * i * i - 2 * i),
--i )
+ c / d
: r;
} // c()
} // C
CTest.java
/*
* Class 'CTest' (for 'Cosine Test').
* Copyright (C) 2017 Gerold 'Geri' Broser (geribro@users.sourceforge.net)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package p;
import org.apache.commons.math3.util.CombinatoricsUtils;
/** Test class for methods 'c' (for calculate) of class 'C' (for Cosine).
*
* @author Gerold 'Geri' Broser
* @version 17.04.21
* @see https://codegolf.stackexchange.com/questions/116705/the-pedants-cosine
*/
public class CTest {
/** The Unicode character 'ₛ' is the subscript character for 's'. It stands for the plural 's'
* in the array's names. ('xs' wasn't matching my BMI. 'ns' looked like a system that we have
* overcome since decades and like the German abbreviation for...I'm leaving that one out now.)
*
* @param args command line arguments
*/
public static void main( String[] args ) {
double[] xₛ = { .0, .5, .5, .5, .5, 2., 2. };
int[] nₛ = { 1, 1, 2, 4, 9, 2, 5 };
System.out.println(
"┌────┬─────┬───┬───────────────────────┐\n" +
"│ No │ x │ n │ cos(x) │\n" +
"├────┤─────┼───┼───────────────────────┤" );
for ( int i = 0; i < xₛ.length; i++ ) {
System.out.printf( "│ %d. │ %2.1f │ %d │ %,21.18f │%n",
i + 1,
xₛ[i], // x (angle)
nₛ[i]--, // n (number of terms of the polynomial, decreased for further processing as index)
1.0 + new C().c(
xₛ[i], // x (angle in radians)
Math.pow( -1, nₛ[i] ) // negate alternating starting with minus for the second term
* Math.pow( xₛ[i], 2 * nₛ[i] ), // counter for the rightmost term (c)
CombinatoricsUtils.factorial( 2 * nₛ[i] ), // denominator for the rightmost term (d)
nₛ[i] // index of last term of the polynomial
) );
} // for ( C test case )
System.out.println( "└────┴─────┴───┴───────────────────────┘" );
} // main()
} // CTest
Output
┌────┬─────┬───┬───────────────────────┐
│ No │ x │ n │ cos(x) │
├────┤─────┼───┼───────────────────────┤
│ 1. │ 0,0 │ 1 │ 1,000000000000000000 │
│ 2. │ 0,5 │ 1 │ 1,000000000000000000 │
│ 3. │ 0,5 │ 2 │ 0,875000000000000000 │
│ 4. │ 0,5 │ 4 │ 0,877582465277777700 │
│ 5. │ 0,5 │ 9 │ 0,877582561890372800 │
│ 6. │ 2,0 │ 2 │ -1,000000000000000000 │
│ 7. │ 2,0 │ 5 │ -0,415873015873015950 │
└────┴─────┴───┴───────────────────────┘
1I'm assuming that
n
is also greater than0
? – GamrCorps – 2017-04-16T18:24:29.633@GamrCorps
n
will be greater than zero, yes. – Silvio Mayolo – 2017-04-16T20:02:22.520@RosLuP I'm not sure I understand your question, but the challenge is to produce the sum of the first n terms of the Taylor series stated in the problem, so the Taylor series about x = 2 or any other point will not suffice. – Silvio Mayolo – 2017-04-16T20:03:07.953
@SilvioMayolo ok i make one error, not rember well, yes sin(x) or cos(x) Taylor series would be ok in all R – RosLuP – 2017-04-16T20:14:18.780
8I'd say they technically isn't what pedant means, but that would be too meta. – PyRulez – 2017-04-17T02:05:34.073
8If your Boss wants you write a good or at least readable function, you're in the wrong place. – Roman Gräf – 2017-04-17T08:25:49.953
2A truly picky boss would want to calculate cosine using something a little more efficient (and accurate) than Taylor series... – PM 2Ring – 2017-04-18T11:49:37.223
6@PM2Ring That would not be picky, that would be being reasonable. Taylor series is really the crudest option. – user1997744 – 2017-04-18T15:51:43.620
If you are rounding, you shouldn't use
...
. If you are going to use...
, use the exact digits up until that point. – HyperNeutrino – 2017-04-19T13:10:26.447