1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @summary <rdar://problem/1400112> EXT a2: Math package error is causing this applet to print out the wrong value
  27  * @summary com.apple.junit.java.lang.Math
  28  */
  29 
  30 import junit.framework.*;
  31 
  32 public class R1400112FloatPrecisionTest extends TestCase {
  33     protected static final double kLog10 = Math.log(10);
  34     protected static double ERRMARGIN;
  35     protected double origDouble;
  36     protected double resultingDouble;
  37 
  38     protected void setUp() {
  39         ERRMARGIN = .000000001;
  40         origDouble = -1.56;
  41         resultingDouble    = origDouble;
  42     }
  43 
  44     public static Test suite() {
  45         return new TestSuite(R1400112FloatPrecisionTest.class);
  46     }
  47 
  48     final public static double roundToXSigDigs(double resultingDouble, int sigs) {
  49         double  absDouble = Math.abs(resultingDouble);
  50         double roundFactor = Math.log(absDouble);
  51 
  52         if( absDouble < ERRMARGIN ) { // it's close enough to zero for us
  53             return(0);
  54         }
  55         roundFactor /= kLog10;
  56         roundFactor = Math.floor(roundFactor);
  57         roundFactor = sigs-1-roundFactor;
  58         roundFactor *= kLog10;
  59         roundFactor = Math.exp(roundFactor);
  60         resultingDouble *= roundFactor;
  61         resultingDouble = Math.round(resultingDouble);
  62         resultingDouble /= roundFactor;
  63         return (resultingDouble);
  64     }
  65 
  66     public void testR1400112FloatPrecision() {
  67         resultingDouble = roundToXSigDigs(resultingDouble, 4);
  68         assertTrue ("Math on doubles is functional", Math.abs(resultingDouble - origDouble) <  ERRMARGIN );
  69     }
  70 
  71     public static void main (String[] args) throws RuntimeException {
  72         TestResult tr = junit.textui.TestRunner.run(suite());
  73         if ((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
  74             throw new RuntimeException("### FAILED: unexpected JUnit errors or failures.");
  75         }
  76     }
  77 }
  78