001/* 002 * Units of Measurement Reference Implementation 003 * Copyright (c) 2005-2017, 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 1.0, October 6, 2016 044 * @since 1.0 045 */ 046public final class PiMultiplierConverter extends AbstractConverter implements ValueSupplier<String> { 047 048 /** 049 * Creates a Pi multiplier converter. 050 */ 051 public PiMultiplierConverter() { 052 } 053 054 @Override 055 public double convert(double value) { 056 return value * PI; 057 } 058 059 // @Override 060 // public BigDecimal convert(BigDecimal value, MathContext ctx) 061 // throws ArithmeticException { 062 // int nbrDigits = ctx.getPrecision(); 063 // if (nbrDigits == 0) 064 // throw new ArithmeticException( 065 // "Pi multiplication with unlimited precision"); 066 // BigDecimal pi = Pi.pi(nbrDigits); 067 // return value.multiply(pi, ctx).scaleByPowerOfTen(1 - nbrDigits); 068 // } 069 @Override 070 public AbstractConverter inverse() { 071 return new PiDivisorConverter(); 072 } 073 074 @Override 075 public final String toString() { 076 return "(?)"; 077 } 078 079 @Override 080 public boolean equals(Object obj) { 081 return (obj instanceof PiMultiplierConverter); 082 } 083 084 @Override 085 public int hashCode() { 086 return 0; 087 } 088 089 public boolean isLinear() { 090 return true; 091 } 092 093 /** 094 * Pi calculation with Machin's formula. 095 * 096 * @see <a href="http://en.literateprograms.org/Pi_with_Machin's_formula_(Java)" >Pi with Machin's formula</a> 097 * 098 */ 099 // static final class Pi { 100 // 101 // private Pi() { 102 // } 103 // 104 // public static Double pi(int numDigits) { 105 // int calcDigits = numDigits + 10; 106 // return 4 * ((4 * (arccot(5, calcDigits))) - arccot(239, calcDigits)); /* 107 // * ) 108 // * . 109 // * setScale 110 // * ( 111 // * numDigits 112 // * , 113 // * RoundingMode 114 // * . 115 // * DOWN 116 // * ) 117 // * ; 118 // */ 119 // } 120 // 121 // private static double arccot(double x, int numDigits) { 122 // double unity = 1; /* 123 // * BigDecimal.ONE.setScale(numDigits, 124 // * RoundingMode.DOWN); 125 // */ 126 // double sum = unity / x; 127 // double xpower = sum; 128 // Double term = null; 129 // boolean add = false; 130 // for (double n = 3; term == null || !term.equals(Double.valueOf(0)); n += 2) { 131 // xpower = xpower / ((long) x ^ 2); 132 // term = xpower / n; 133 // sum = add ? sum + term : sum - term; 134 // add = !add; 135 // } 136 // return sum; 137 // } 138 // } 139 // private static final BigDecimal TWO = new BigDecimal("2"); 140 // private static final BigDecimal FOUR = new BigDecimal("4"); 141 // private static final BigDecimal FIVE = new BigDecimal("5"); 142 // private static final BigDecimal TWO_THIRTY_NINE = new BigDecimal("239"); 143 public String getValue() { 144 return toString(); 145 } 146}