001package de.hdm_stuttgart.sd1.rounding;
002
003/**
004 * Creating a math table providing sin values
005 * in the range [0, 355] for every 5 degrees
006 *
007 */
008public class MathTable {
009   
010   private final static int numberOfFractionDigits = 3; // Show this number of fractional digits
011
012  /**
013   * @param args unused
014   */
015  public static void main(String[] args) {
016
017     int precision = 1;
018     for (int i = 0; i < numberOfFractionDigits; i++) {
019        precision *= 10;
020     }
021     
022    System.out.println("  x | sin(x)");
023    System.out.println("----+------");
024    for (int i = 0; i < 360; i += 5) {
025      
026      final double
027        sinValue = Math.sin(i * Math.PI / 180); // Turning degrees into radians
028      
029      final long
030        // Rounding to the appropriate number of decimal places
031        sinValueRounded = Math.round(precision *  sinValue);
032      
033      final double roundedValue = ((double)sinValueRounded) / precision; // double cast: avoid 223 / 1000 == 0
034      
035      final int totalNumberOfDigits = 3 + // Optional '-' + 1 digit + '.' + 
036                                       numberOfFractionDigits;
037      
038      System.out.format(" %3d|%" + totalNumberOfDigits + "." + numberOfFractionDigits + "f %n", i, roundedValue);
039      
040      if (0 < i && 0 == i % 20) { // Add extra separator every four lines
041        System.out.println("----+------");
042      }
043    }
044  }
045}