mirror of https://github.com/apache/poi.git
partial implementation FLOOR.MATH function (needs more testing and bad param support)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1901171 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
592bac02e1
commit
176f64cfe3
|
@ -112,6 +112,7 @@ public final class AnalysisToolPak implements UDFFinder {
|
||||||
r(m, "ERF", null);
|
r(m, "ERF", null);
|
||||||
r(m, "ERFC", null);
|
r(m, "ERFC", null);
|
||||||
r(m, "FACTDOUBLE", FactDouble.instance);
|
r(m, "FACTDOUBLE", FactDouble.instance);
|
||||||
|
r(m, "FLOOR.MATH", FloorMath.instance);
|
||||||
r(m, "FVSCHEDULE", null);
|
r(m, "FVSCHEDULE", null);
|
||||||
r(m, "GCD", Gcd.instance);
|
r(m, "GCD", Gcd.instance);
|
||||||
r(m, "GESTEP", null);
|
r(m, "GESTEP", null);
|
||||||
|
|
|
@ -24,9 +24,10 @@ import org.apache.poi.ss.formula.eval.NumberEval;
|
||||||
import org.apache.poi.ss.formula.eval.OperandResolver;
|
import org.apache.poi.ss.formula.eval.OperandResolver;
|
||||||
import org.apache.poi.ss.formula.eval.ValueEval;
|
import org.apache.poi.ss.formula.eval.ValueEval;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
|
|
||||||
|
import static org.apache.poi.ss.formula.functions.MathX.scaledRoundUsingBigDecimal;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementation for Excel CEILING.MATH() function.
|
* Implementation for Excel CEILING.MATH() function.
|
||||||
* <ul>
|
* <ul>
|
||||||
|
@ -61,12 +62,12 @@ public final class CeilingMath implements FreeRefFunction {
|
||||||
}
|
}
|
||||||
if (roundNegativeNumsDown && xval < 0.0) {
|
if (roundNegativeNumsDown && xval < 0.0) {
|
||||||
if (multiplier != 1.0) {
|
if (multiplier != 1.0) {
|
||||||
return new NumberEval(roundUsingBigDecimal(xval, multiplier, RoundingMode.FLOOR));
|
return new NumberEval(scaledRoundUsingBigDecimal(xval, multiplier, RoundingMode.FLOOR));
|
||||||
}
|
}
|
||||||
return new NumberEval(Math.floor(xval));
|
return new NumberEval(Math.floor(xval));
|
||||||
}
|
}
|
||||||
if (multiplier != 1.0) {
|
if (multiplier != 1.0) {
|
||||||
return new NumberEval(roundUsingBigDecimal(xval, multiplier, RoundingMode.CEILING));
|
return new NumberEval(scaledRoundUsingBigDecimal(xval, multiplier, RoundingMode.CEILING));
|
||||||
}
|
}
|
||||||
return new NumberEval(Math.ceil(xval));
|
return new NumberEval(Math.ceil(xval));
|
||||||
} catch (EvaluationException evaluationException) {
|
} catch (EvaluationException evaluationException) {
|
||||||
|
@ -74,14 +75,6 @@ public final class CeilingMath implements FreeRefFunction {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private double roundUsingBigDecimal(double xval, double multiplier, RoundingMode mode) {
|
|
||||||
BigDecimal multiplierDecimal = BigDecimal.valueOf(multiplier);
|
|
||||||
BigDecimal bd = BigDecimal.valueOf(xval).divide(multiplierDecimal)
|
|
||||||
.setScale(0, mode)
|
|
||||||
.multiply(multiplierDecimal);
|
|
||||||
return bd.doubleValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Double evaluateValue(ValueEval arg, int srcRowIndex, int srcColumnIndex) throws EvaluationException {
|
private static Double evaluateValue(ValueEval arg, int srcRowIndex, int srcColumnIndex) throws EvaluationException {
|
||||||
ValueEval veText = OperandResolver.getSingleValue(arg, srcRowIndex, srcColumnIndex);
|
ValueEval veText = OperandResolver.getSingleValue(arg, srcRowIndex, srcColumnIndex);
|
||||||
String strText1 = OperandResolver.coerceValueToString(veText);
|
String strText1 = OperandResolver.coerceValueToString(veText);
|
||||||
|
|
|
@ -0,0 +1,83 @@
|
||||||
|
/* ====================================================================
|
||||||
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
contributor license agreements. See the NOTICE file distributed with
|
||||||
|
this work for additional information regarding copyright ownership.
|
||||||
|
The ASF licenses this file to You under the Apache License, Version 2.0
|
||||||
|
(the "License"); you may not use this file except in compliance with
|
||||||
|
the License. You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
==================================================================== */
|
||||||
|
|
||||||
|
package org.apache.poi.ss.formula.functions;
|
||||||
|
|
||||||
|
import org.apache.poi.ss.formula.OperationEvaluationContext;
|
||||||
|
import org.apache.poi.ss.formula.eval.ErrorEval;
|
||||||
|
import org.apache.poi.ss.formula.eval.EvaluationException;
|
||||||
|
import org.apache.poi.ss.formula.eval.NumberEval;
|
||||||
|
import org.apache.poi.ss.formula.eval.OperandResolver;
|
||||||
|
import org.apache.poi.ss.formula.eval.ValueEval;
|
||||||
|
|
||||||
|
import java.math.RoundingMode;
|
||||||
|
|
||||||
|
import static org.apache.poi.ss.formula.functions.MathX.scaledRoundUsingBigDecimal;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implementation for Excel FLOOR.MATH() function.
|
||||||
|
* <ul>
|
||||||
|
* <li>https://support.microsoft.com/en-us/office/floor-math-function-c302b599-fbdb-4177-ba19-2c2b1249a2f5</li>
|
||||||
|
* </ul>
|
||||||
|
*/
|
||||||
|
public final class FloorMath implements FreeRefFunction {
|
||||||
|
|
||||||
|
public static final FloorMath instance = new FloorMath();
|
||||||
|
|
||||||
|
private FloorMath() {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
|
||||||
|
if (args.length == 0) {
|
||||||
|
return ErrorEval.VALUE_INVALID;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Double xval = evaluateValue(args[0], ec.getRowIndex(), ec.getColumnIndex());
|
||||||
|
if (xval == null) {
|
||||||
|
return ErrorEval.NUM_ERROR;
|
||||||
|
}
|
||||||
|
double multiplier = 1.0;
|
||||||
|
if (args.length > 1) {
|
||||||
|
Double arg1Val = evaluateValue(args[1], ec.getRowIndex(), ec.getColumnIndex());
|
||||||
|
multiplier = arg1Val != null ? arg1Val.doubleValue() : 1.0;
|
||||||
|
}
|
||||||
|
boolean roundNegativeNumsDown = false;
|
||||||
|
if (args.length > 2) {
|
||||||
|
Double arg2Val = evaluateValue(args[2], ec.getRowIndex(), ec.getColumnIndex());
|
||||||
|
roundNegativeNumsDown = arg2Val != null && arg2Val.doubleValue() < 0.0;
|
||||||
|
}
|
||||||
|
if (roundNegativeNumsDown && xval < 0.0) {
|
||||||
|
if (multiplier != 1.0) {
|
||||||
|
return new NumberEval(scaledRoundUsingBigDecimal(xval, multiplier, RoundingMode.CEILING));
|
||||||
|
}
|
||||||
|
return new NumberEval(Math.ceil(xval));
|
||||||
|
}
|
||||||
|
if (multiplier != 1.0) {
|
||||||
|
return new NumberEval(scaledRoundUsingBigDecimal(xval, multiplier, RoundingMode.FLOOR));
|
||||||
|
}
|
||||||
|
return new NumberEval(Math.floor(xval));
|
||||||
|
} catch (EvaluationException evaluationException) {
|
||||||
|
return evaluationException.getErrorEval();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Double evaluateValue(ValueEval arg, int srcRowIndex, int srcColumnIndex) throws EvaluationException {
|
||||||
|
ValueEval veText = OperandResolver.getSingleValue(arg, srcRowIndex, srcColumnIndex);
|
||||||
|
String strText1 = OperandResolver.coerceValueToString(veText);
|
||||||
|
return OperandResolver.parseDouble(strText1);
|
||||||
|
}
|
||||||
|
}
|
|
@ -19,6 +19,9 @@ package org.apache.poi.ss.formula.functions;
|
||||||
|
|
||||||
import org.apache.poi.ss.util.NumberToTextConverter;
|
import org.apache.poi.ss.util.NumberToTextConverter;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.math.RoundingMode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class is an extension to the standard math library
|
* This class is an extension to the standard math library
|
||||||
* provided by java.lang.Math class. It follows the Math class
|
* provided by java.lang.Math class. It follows the Math class
|
||||||
|
@ -231,7 +234,13 @@ final class MathX {
|
||||||
if (s==0 && n!=0) {
|
if (s==0 && n!=0) {
|
||||||
return Double.NaN;
|
return Double.NaN;
|
||||||
} else {
|
} else {
|
||||||
return (n==0 || s==0) ? 0 : Math.floor(n/s) * s;
|
if (n == 0.0 || s == 0.0) {
|
||||||
|
return 0.0;
|
||||||
|
} else if (s == 1.0) {
|
||||||
|
return Math.floor(n);
|
||||||
|
} else {
|
||||||
|
return scaledRoundUsingBigDecimal(n, s, RoundingMode.FLOOR);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -254,10 +263,24 @@ final class MathX {
|
||||||
if (n>0 && s<0) {
|
if (n>0 && s<0) {
|
||||||
return Double.NaN;
|
return Double.NaN;
|
||||||
} else {
|
} else {
|
||||||
return (n == 0 || s == 0) ? 0 : Math.ceil(n/s) * s;
|
if (n == 0.0 || s == 0.0) {
|
||||||
|
return 0.0;
|
||||||
|
} else if (s == 1.0) {
|
||||||
|
return Math.ceil(n);
|
||||||
|
} else {
|
||||||
|
return scaledRoundUsingBigDecimal(n, s, RoundingMode.CEILING);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static double scaledRoundUsingBigDecimal(double xval, double multiplier, RoundingMode mode) {
|
||||||
|
BigDecimal multiplierDecimal = BigDecimal.valueOf(multiplier);
|
||||||
|
BigDecimal bd = BigDecimal.valueOf(xval).divide(multiplierDecimal)
|
||||||
|
.setScale(0, mode)
|
||||||
|
.multiply(multiplierDecimal);
|
||||||
|
return bd.doubleValue();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <br> for all n >= 1; factorial n = n * (n-1) * (n-2) * ... * 1
|
* <br> for all n >= 1; factorial n = n * (n-1) * (n-2) * ... * 1
|
||||||
* <br> else if n == 0; factorial n = 1
|
* <br> else if n == 0; factorial n = 1
|
||||||
|
|
|
@ -22,8 +22,6 @@ import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
|
||||||
import org.apache.poi.hssf.usermodel.HSSFRow;
|
import org.apache.poi.hssf.usermodel.HSSFRow;
|
||||||
import org.apache.poi.hssf.usermodel.HSSFSheet;
|
import org.apache.poi.hssf.usermodel.HSSFSheet;
|
||||||
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||||||
import org.apache.poi.ss.formula.OperationEvaluationContext;
|
|
||||||
import org.apache.poi.ss.formula.eval.ErrorEval;
|
|
||||||
import org.apache.poi.ss.usermodel.FormulaError;
|
import org.apache.poi.ss.usermodel.FormulaError;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
@ -37,8 +35,6 @@ import static org.apache.poi.ss.util.Utils.assertError;
|
||||||
*/
|
*/
|
||||||
final class TestCeilingMath {
|
final class TestCeilingMath {
|
||||||
|
|
||||||
private static final OperationEvaluationContext ec = new OperationEvaluationContext(null, null, 0, 0, 0, null);
|
|
||||||
|
|
||||||
//https://support.microsoft.com/en-us/office/ceiling-math-function-80f95d2f-b499-4eee-9f16-f795a8e306c8
|
//https://support.microsoft.com/en-us/office/ceiling-math-function-80f95d2f-b499-4eee-9f16-f795a8e306c8
|
||||||
@Test
|
@Test
|
||||||
void testMicrosoftExamples() throws IOException {
|
void testMicrosoftExamples() throws IOException {
|
||||||
|
|
|
@ -0,0 +1,74 @@
|
||||||
|
/* ====================================================================
|
||||||
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
contributor license agreements. See the NOTICE file distributed with
|
||||||
|
this work for additional information regarding copyright ownership.
|
||||||
|
The ASF licenses this file to You under the Apache License, Version 2.0
|
||||||
|
(the "License"); you may not use this file except in compliance with
|
||||||
|
the License. You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
==================================================================== */
|
||||||
|
|
||||||
|
package org.apache.poi.ss.formula.functions;
|
||||||
|
|
||||||
|
import org.apache.poi.hssf.usermodel.HSSFCell;
|
||||||
|
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
|
||||||
|
import org.apache.poi.hssf.usermodel.HSSFRow;
|
||||||
|
import org.apache.poi.hssf.usermodel.HSSFSheet;
|
||||||
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||||||
|
import org.apache.poi.ss.usermodel.FormulaError;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import static org.apache.poi.ss.util.Utils.assertDouble;
|
||||||
|
import static org.apache.poi.ss.util.Utils.assertError;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link FloorMath}
|
||||||
|
*/
|
||||||
|
final class TestFloorMath {
|
||||||
|
|
||||||
|
//https://support.microsoft.com/en-us/office/floor-math-function-c302b599-fbdb-4177-ba19-2c2b1249a2f5
|
||||||
|
@Test
|
||||||
|
void testMicrosoftExamples() throws IOException {
|
||||||
|
try (HSSFWorkbook wb = new HSSFWorkbook()) {
|
||||||
|
HSSFSheet sheet = wb.createSheet();
|
||||||
|
HSSFRow row = sheet.createRow(0);
|
||||||
|
HSSFCell cell = row.createCell(0);
|
||||||
|
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
|
||||||
|
assertDouble(fe, cell, "FLOOR.MATH(24.3,5)", 20.0, 0.00000000000001);
|
||||||
|
assertDouble(fe, cell, "FLOOR.MATH(6.7)", 6.0, 0.00000000000001);
|
||||||
|
assertDouble(fe, cell, "FLOOR.MATH(-8.1,2)", -10.0, 0.00000000000001);
|
||||||
|
assertDouble(fe, cell, "FLOOR.MATH(-5.5,2,-1)", -4.0, 0.00000000000001);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testInvalid() throws IOException {
|
||||||
|
try (HSSFWorkbook wb = new HSSFWorkbook()) {
|
||||||
|
HSSFSheet sheet = wb.createSheet();
|
||||||
|
HSSFRow row = sheet.createRow(0);
|
||||||
|
HSSFCell cell = row.createCell(0);
|
||||||
|
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
|
||||||
|
assertError(fe, cell, "FLOOR.MATH()", FormulaError.VALUE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testNumError() throws IOException {
|
||||||
|
try (HSSFWorkbook wb = new HSSFWorkbook()) {
|
||||||
|
HSSFSheet sheet = wb.createSheet();
|
||||||
|
HSSFRow row = sheet.createRow(0);
|
||||||
|
HSSFCell cell = row.createCell(0);
|
||||||
|
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
|
||||||
|
assertError(fe, cell, "FLOOR.MATH(\"abc\")", FormulaError.NUM);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue