8 added 43 characters in body edited Apr 23 '17 at 22:01 G.Broser says Reinstate Monica 11311 silver badge55 bronze badges /* * 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 . */ 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 calculate 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  /* * 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 . */ 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 calculate 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  /* * 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 . */ 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 calculate 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  7 added 101 characters in body edited Apr 23 '17 at 21:02 G.Broser says Reinstate Monica 11311 silver badge55 bronze badges /* * 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 . */ 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 cosinepart (i=n-1) of a given * cosine of *a given angle x'x' that's calculated withfrom anthe (incomplete,second seeterm below)of a * Taylor series of  n polynomial terms onwards (or *backwards until ithe (=n-1)  polynomial* terms. 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 * (hence the 'incomplete' above). * * ● 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. * 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 Itfor doesn'tpower haveand tofactorial. * * ● It calculate the counters and denominators for each   term from scratch   * term from scratch at each recursion but uses the values calculated at the previous   * the previous recursion. Such the new values can be calculated by using trivial   * trivial parenthesis, division, multiplication, decrement and negation only. * only. This doesn't only save characters but probably is also faster than power * power and factorial. * [It *could be made [It'sbe even faster iffor x=2^n, n ∈ N, because we can use the unsigned * *use the unsigned right shift operator '>>>' instead of divisions then (see method * * (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 minus'n' 1,* Σ's upper boundary) *   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  /* * 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 . */ 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 cosine of a given * angle x calculated with an (incomplete, see below) Taylor series of   * i (=n-1) polynomial terms. *   * It achieves this by doing the following: * * ● It doesn't calculate the first term since it is always 1 anyway * (hence the 'incomplete' above). * * ● 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 stop condition. * * ● It is supplied with values for the counter and denominator of the * rightmost term at invocation.   * * ● It doesn't have to calculate 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 is also faster than power * and factorial. * [It's even faster if 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 including proper sign (see class 'CTest') * @param d denominator of last term (see class 'CTest') * @param i index of last term used in the calculation (=number of terms 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 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  /* * 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 . */ 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 calculate 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  6 adds GNU GPLv3 edited Apr 23 '17 at 12:32 G.Broser says Reinstate Monica 11311 silver badge55 bronze badges /* * 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 . */ 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 cosine of a given * angle x calculated with an (incomplete, see below) Taylor series of * i (=n-1) polynomial terms. * * It achieves this by doing the following: * * ● It doesn't calculate the first term since it is always 1 anyway * (hence the 'incomplete' above). * * ● 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 stop condition. * * ● It is supplied with values for the counter and denominator of the * rightmost term at invocation. * * ● It doesn't have to calculate 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 is also faster than power * and factorial. * [It's even faster if 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 including proper sign (see class 'CTest') * @param d denominator of last term (see class 'CTest') * @param i index of last term used in the calculation (=number of terms 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 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 /* * 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 . */ 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  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 cosine of a given * angle x calculated with an (incomplete, see below) Taylor series of * i (=n-1) polynomial terms. * * It achieves this by doing the following: * * ● It doesn't calculate the first term since it is always 1 anyway * (hence the 'incomplete' above). * * ● 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 stop condition. * * ● It is supplied with values for the counter and denominator of the * rightmost term at invocation. * * ● It doesn't have to calculate 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 is also faster than power * and factorial. * [It's even faster if 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 including proper sign (see class 'CTest') * @param d denominator of last term (see class 'CTest') * @param i index of last term used in the calculation (=number of terms 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 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 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  /* * 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 . */ 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 cosine of a given * angle x calculated with an (incomplete, see below) Taylor series of * i (=n-1) polynomial terms. * * It achieves this by doing the following: * * ● It doesn't calculate the first term since it is always 1 anyway * (hence the 'incomplete' above). * * ● 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 stop condition. * * ● It is supplied with values for the counter and denominator of the * rightmost term at invocation. * * ● It doesn't have to calculate 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 is also faster than power * and factorial. * [It's even faster if 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 including proper sign (see class 'CTest') * @param d denominator of last term (see class 'CTest') * @param i index of last term used in the calculation (=number of terms 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 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 /* * 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 . */ 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  5 deleted 5 characters in body edited Apr 21 '17 at 1:58 G.Broser says Reinstate Monica 11311 silver badge55 bronze badges 4 added 4 characters in body edited Apr 21 '17 at 1:43 G.Broser says Reinstate Monica 11311 silver badge55 bronze badges 3 added 23 characters in body edited Apr 21 '17 at 1:37 G.Broser says Reinstate Monica 11311 silver badge55 bronze badges Post Undeleted by G.Broser says Reinstate Monica occurred Apr 21 '17 at 1:26 2 deleted 62 characters in body edited Apr 21 '17 at 1:26 G.Broser says Reinstate Monica 11311 silver badge55 bronze badges Post Deleted by G.Broser says Reinstate Monica occurred Apr 20 '17 at 13:48 1 answered Apr 20 '17 at 13:46 G.Broser says Reinstate Monica 11311 silver badge55 bronze badges