001/* 002 * Units of Measurement Reference Implementation 003 * Copyright (c) 2005-2016, Jean-Marie Dautelle, Werner Keil, V2COM. 004 * 005 * All rights reserved. 006 * 007 * Redistribution and use in source and binary forms, with or without modification, 008 * are permitted provided that the following conditions are met: 009 * 010 * 1. Redistributions of source code must retain the above copyright notice, 011 * this list of conditions and the following disclaimer. 012 * 013 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions 014 * and the following disclaimer in the documentation and/or other materials provided with the distribution. 015 * 016 * 3. Neither the name of JSR-363 nor the names of its contributors may be used to endorse or promote products 017 * derived from this software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 021 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 023 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 026 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 028 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package tec.units.ri.function; 031 032import tec.units.ri.AbstractConverter; 033import tec.uom.lib.common.function.ValueSupplier; 034 035/** 036 * <p> 037 * This class represents a converter multiplying numeric values by ? (Pi). 038 * </p> 039 * 040 * @see <a href="http://en.wikipedia.org/wiki/Pi"> Wikipedia: Pi</a> 041 * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a> 042 * @author <a href="mailto:units@catmedia.us">Werner Keil</a> 043 * @version 0.4, December 28, 2015 044 */ 045public final class PiMultiplierConverter extends AbstractConverter implements ValueSupplier<String> { 046 047 /** 048 * Creates a Pi multiplier converter. 049 */ 050 public PiMultiplierConverter() { 051 } 052 053 @Override 054 public double convert(double value) { 055 return value * PI; 056 } 057 058 // @Override 059 // public BigDecimal convert(BigDecimal value, MathContext ctx) 060 // throws ArithmeticException { 061 // int nbrDigits = ctx.getPrecision(); 062 // if (nbrDigits == 0) 063 // throw new ArithmeticException( 064 // "Pi multiplication with unlimited precision"); 065 // BigDecimal pi = Pi.pi(nbrDigits); 066 // return value.multiply(pi, ctx).scaleByPowerOfTen(1 - nbrDigits); 067 // } 068 @Override 069 public AbstractConverter inverse() { 070 return new PiDivisorConverter(); 071 } 072 073 @Override 074 public final String toString() { 075 return "(?)"; 076 } 077 078 @Override 079 public boolean equals(Object obj) { 080 return (obj instanceof PiMultiplierConverter); 081 } 082 083 @Override 084 public int hashCode() { 085 return 0; 086 } 087 088 public boolean isLinear() { 089 return true; 090 } 091 092 /** 093 * Pi calculation with Machin's formula. 094 * 095 * @see <a href="http://en.literateprograms.org/Pi_with_Machin's_formula_(Java)" >Pi with Machin's formula</a> 096 * 097 */ 098 // static final class Pi { 099 // 100 // private Pi() { 101 // } 102 // 103 // public static Double pi(int numDigits) { 104 // int calcDigits = numDigits + 10; 105 // return 4 * ((4 * (arccot(5, calcDigits))) - arccot(239, calcDigits)); /* 106 // * ) 107 // * . 108 // * setScale 109 // * ( 110 // * numDigits 111 // * , 112 // * RoundingMode 113 // * . 114 // * DOWN 115 // * ) 116 // * ; 117 // */ 118 // } 119 // 120 // private static double arccot(double x, int numDigits) { 121 // double unity = 1; /* 122 // * BigDecimal.ONE.setScale(numDigits, 123 // * RoundingMode.DOWN); 124 // */ 125 // double sum = unity / x; 126 // double xpower = sum; 127 // Double term = null; 128 // boolean add = false; 129 // for (double n = 3; term == null || !term.equals(Double.valueOf(0)); n += 2) { 130 // xpower = xpower / ((long) x ^ 2); 131 // term = xpower / n; 132 // sum = add ? sum + term : sum - term; 133 // add = !add; 134 // } 135 // return sum; 136 // } 137 // } 138 // private static final BigDecimal TWO = new BigDecimal("2"); 139 // private static final BigDecimal FOUR = new BigDecimal("4"); 140 // private static final BigDecimal FIVE = new BigDecimal("5"); 141 // private static final BigDecimal TWO_THIRTY_NINE = new BigDecimal("239"); 142 public String getValue() { 143 return toString(); 144 } 145}