geirm 01/06/19 04:44:16 Added: rupert/src/java/org/apache/commons/rupert/nathan MathTool.java Log: Initial commit of contribution from Nathan Bubna <[EMAIL PROTECTED]> Revision Changes Path 1.1 jakarta-commons-sandbox/rupert/src/java/org/apache/commons/rupert/nathan/MathTool.java Index: MathTool.java =================================================================== /* * * The Apache Software License, Version 1.1 * * Copyright (c) 2001 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Commons", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */ package org.apache.commons.rupert.nathan; import java.lang.Math; /** * Tool for performing floating point math in Velocity * * All methods are static. Most take Objects as parameters * because Velocity wouldn't find the methods otherwise. * * @author Nathan Bubna * @version $Id: MathTool.java,v 1.1 2001/06/19 11:44:15 geirm Exp $ */ public class MathTool { /************* basic math methods ***************/ public static double add(Object num1, Object num2) { return toDouble(num1) + toDouble(num2); } public static double sub(Object num1, Object num2) { return toDouble(num1) - toDouble(num2); } public static double mul(Object num1, Object num2) { return toDouble(num1) * toDouble(num2); } public static double div(Object num1, Object num2) { double d1 = toDouble(num1); double d2 = toDouble(num2); if (d2 == 0) { throw new IllegalArgumentException("Divide by zero!? Shame on you!"); } return d1 / d2; } /************ accessors for some java.lang.Math methods *********/ public static double max(Object num1, Object num2) { return Math.max(toDouble(num1), toDouble(num2)); } public static double min(Object num1, Object num2) { return Math.min(toDouble(num1), toDouble(num2)); } public static double abs(Object num) { return Math.abs(toDouble(num)); } /** * The toDouble method is a bit of hack used because Velocity * seems to be passing in literal numbers and Strings representing numbers * as members of the Object class. * * If the Object passed in is not a valid number, a NumberFormatException will be thrown. * * converts an object with a numeric value into a java double * will return 0.0 if the object does not contain a numeric value. */ public static double toDouble(Object num) { double value = 0.0; if (num instanceof Number) { if (num != null) value = ((Number)num).doubleValue(); } else if (num instanceof String) { value = Double.parseDouble((String)num); } else { String num_string = String.valueOf(num); value = Double.parseDouble(num_string); } return value; } /** * The toInt method is a bit of hack used because Velocity * seems to be passing in literal numbers and Strings representing numbers * as members of the Object class. * * If the Object passed in is not a valid number, a NumberFormatException will be thrown. * * converts an object with a numeric value into a java int * will return 0 if the object does not contain a numeric value. */ public static int toInt(Object num) { int value = 0; if (num instanceof Number) { if (num != null) value = ((Number)num).intValue(); } else if (num instanceof String) { value = Integer.parseInt((String)num); } else { String num_string = String.valueOf(num); value = Integer.parseInt(num_string); } return value; } /** * rounds a double to the specified number of decimal places */ public static double roundTo(int decimals, double value) { if (decimals == 0) { value = (int)(value + .5); } else { double shift = Math.pow(10, decimals); value = value * shift; value = (int)(value + .5); value = value / shift; } return value; } public static double roundTo(Object decimals, Object num) { return roundTo(toInt(decimals), toDouble(num)); } private static int percent (double value) { value = roundTo (2, value); value *= 100; return (int) value; } }