fork download
  1. /**
  2.  * Ephraim Rothschild
  3.  * June 13th 2014
  4.  */
  5. import java.io.FileNotFoundException;
  6. import java.io.File;
  7. import java.io.PrintStream;
  8. import java.text.DecimalFormat;
  9. import java.text.NumberFormat;
  10. import java.util.ArrayList;
  11. import java.util.Scanner;
  12. import java.lang.Math;
  13.  
  14. class Trig {
  15. /**
  16.   *Global Variables
  17.   **/
  18. static final double pi2 = 6.28318530718;
  19. public long totalTime = 0L;
  20. static final double piover2 = 1.57079632679;
  21. static final double plusinfty = Double.POSITIVE_INFINITY;
  22. static final double minusinfty = Double.NEGATIVE_INFINITY;
  23. static final double factoriallist[] =
  24. new double[]{
  25. -6.0,120.0,-5040.0,362880.0,-39916800.0,
  26. 6227020800.0,-1307674368000.0,355687428096000.0,
  27. -121645100408832000.0,51090942171709440000.0,
  28. -25852016738884976640000.0,
  29. 15511210043330985984000000.0,
  30. -10888869450418352160768000000.0,
  31. 8841761993739701954543616000000.0
  32. };
  33. //Begin Program
  34. public static void main(String[] args) {
  35. Trig mytrig = new Trig();
  36. double[] input = mytrig.getInput();
  37. double[][] output = mytrig.calculate(input);
  38. mytrig.OutputResults(output);
  39. Trig testIt = new Trig();
  40. testIt.timeIt(input);
  41. }
  42.  
  43. //Begin Timing
  44. public void timeIt(double[] input) {
  45. double overhead = 0L;
  46. totalTime = 0L;
  47.  
  48. for (int i = 0; i < 1000001; i++) {
  49. calculate(input);
  50. if (i == 0) {
  51. overhead = totalTime;
  52. totalTime = 0L;
  53. }
  54. }
  55. double averageTime = ((Double.valueOf(totalTime-overhead))/1000000.0);
  56. double perAngleTime = averageTime/input.length;
  57. double perOpperationTime = perAngleTime/3;
  58. NumberFormat formatter = new DecimalFormat("0000.0000");
  59. System.out.println("\n-----------------------------------------------------");
  60. System.out.println(" TIME RESULTS: ");
  61. System.out.println("-----------------------------------------------------");
  62. System.out.println("Average Total Runtime:.......... " + formatter.format(averageTime) + " nsec");
  63. System.out.println(" = " + formatter.format(averageTime/1000) + " usec\n");
  64. System.out.println("Average Runtime Per Angle:....... " + formatter.format(perAngleTime) + " nsec");
  65. System.out.println(" = " + formatter.format(perAngleTime/1000) + " usec\n");
  66. System.out.println("Average Runtime Per Opperation:.. " + formatter.format(perOpperationTime) + " nsec");
  67. System.out.println(" = " + formatter.format(perOpperationTime/1000) + " usec");
  68. }
  69.  
  70. //Begin Input
  71. double[] getInput() {
  72. Scanner in;
  73. ArrayList<Double> input = new ArrayList<Double>();
  74. try {
  75. in = new Scanner(new File("trig.in"));
  76. } catch (FileNotFoundException e) {
  77. new FileNotFoundException("Failed to read from file. Reading from stdin instead...").printStackTrace();
  78. in= new Scanner(System.in);
  79. }
  80. while (in.hasNextLine()) {
  81. Double toadd = Double.parseDouble(in.nextLine());
  82. input.add(toadd);
  83. }
  84. in.close();
  85. double[] returnable = new double[input.size()];
  86. for(int i = 0; i < input.size(); i++) {returnable[i] = input.get(i);}
  87. return returnable;
  88. }
  89.  
  90. //Begin OutputStream Choice
  91. void OutputResults(double[][] outList) {
  92. try {
  93. out = new PrintStream("trig.out");
  94. PrintOutput(outList, out);
  95. PrintOutput(outList, System.out);
  96. } catch (FileNotFoundException e) {
  97. new FileNotFoundException("Failed to write to file. Printing to stdout instead...").printStackTrace();
  98. PrintOutput(outList, System.out);
  99. }
  100. }
  101.  
  102. //Begin Output
  103. static void PrintOutput(double[][] outList, PrintStream out) {
  104. /**
  105. *outList[0][i] holds original angles
  106. *outList[1][i] holds sin values
  107. *outList[2][i] holds cos values
  108. *outList[3][i] holds tan values
  109. */
  110. NumberFormat formatter = new DecimalFormat("0.0000000E0");
  111. out.println("-----------------------------------------------------");
  112. out.println(" TRIG RESULTS ");
  113. out.println("-----------------------------------------------------");
  114. for (int i=0; i<outList[0].length; i++) {
  115. out.println("For Angle: " + outList[0][i]);
  116.  
  117. out.println(" sin: " + formatter.format(outList[1][i]));
  118. out.println(" cos: " + formatter.format(outList[2][i]));
  119. if (Double.valueOf(outList[3][i]).isInfinite() || Double.valueOf(outList[3][i]).isNaN()) {
  120. out.println(" tan: " + outList[3][i]);
  121. }
  122. else out.println(" tan: " + formatter.format(outList[3][i]));
  123. }
  124. if (out != System.out) out.close();
  125. }
  126.  
  127. //Begin Calculations
  128. double[][] calculate(double[] IOList) {
  129. double[][] outList = new double[4][IOList.length];
  130. double sin;
  131. double cos;
  132. double tan;
  133. double rads;
  134. int i = 0;
  135. long calctime = 0L;
  136. long startTime;
  137. long endTime;
  138. for (double input : IOList) {
  139. startTime = System.nanoTime();
  140. rads = toRad(input);
  141. sin=sin(rads);
  142. cos = ((piover2-rads)>=0) ? Math.sqrt((1.0-(sin*sin))) : -Math.sqrt((1.0-(sin*sin)));
  143. tan = (cos!=0.0d) ? sin/cos : ((sin>0) ? plusinfty : minusinfty);
  144. endTime = System.nanoTime();
  145. calctime = calctime + endTime - startTime;
  146. outList[0][i] = input;
  147. outList[1][i] = sin;
  148. outList[2][i] = cos;
  149. outList[3][i] = tan;
  150. i++;
  151. }
  152. totalTime = totalTime + calctime;
  153. return outList;
  154. }
  155.  
  156. //Convert Degrees to Radians
  157. double toRad(double deg) {
  158. double rev = deg/360.0;
  159. return (rev>1 || rev<0) ? Math.abs(rev - ((int)rev))*pi2 : rev*pi2;
  160. }
  161.  
  162. //Get sin
  163. double sin(double angle) {
  164. double sqr = angle*angle;
  165. double value = angle;
  166. for (double fct : factoriallist) {
  167. value += ((angle*=sqr)/fct);
  168. }
  169. return ((long)((value + Math.copySign(0.0000000005d, value))*10000000.0))/10000000.0;
  170. }
  171. }
Success #stdin #stdout #stderr 3.42s 380928KB
stdin
90.00000000
74.54390000
175.5000000
3600000.000
stdout
-----------------------------------------------------
                    TRIG RESULTS                     
-----------------------------------------------------
For Angle: 90.0
      sin: 1.0000000E0
      cos: -0.0000000E0
      tan: Infinity
For Angle: 74.5439
      sin: 9.6383490E-1
      cos: 2.6650007E-1
      tan: 3.6166404E0
For Angle: 175.5
      sin: 7.8459000E-2
      cos: -9.9691734E-1
      tan: -7.8701610E-2
For Angle: 3600000.0
      sin: 0.0000000E0
      cos: 1.0000000E0
      tan: 0.0000000E0

-----------------------------------------------------
                    TIME RESULTS:                    
-----------------------------------------------------
Average Total  Runtime:.......... 1876.6348 nsec
                                = 0001.8766 usec

Average Runtime Per Angle:....... 0469.1587 nsec
                                = 0000.4692 usec

Average Runtime Per Opperation:.. 0156.3862 nsec
                                = 0000.1564 usec
stderr
java.io.FileNotFoundException: Failed to read from file. Reading from stdin instead...
	at Trig.getInput(Main.java:71)
	at Trig.main(Main.java:33)
java.io.FileNotFoundException: Failed to write to file. Printing to stdout instead...
	at Trig.OutputResults(Main.java:91)
	at Trig.main(Main.java:35)