diff --git a/core/src/main/java/lucee/runtime/functions/math/Cos.java b/core/src/main/java/lucee/runtime/functions/math/Cos.java index bdb4b04af5..148539ad23 100644 --- a/core/src/main/java/lucee/runtime/functions/math/Cos.java +++ b/core/src/main/java/lucee/runtime/functions/math/Cos.java @@ -21,7 +21,6 @@ */ package lucee.runtime.functions.math; -import java.math.BigDecimal; import java.math.MathContext; import java.math.RoundingMode; @@ -37,44 +36,9 @@ public final class Cos implements Function { public static Number call(PageContext pc, Number number) { if (ThreadLocalPageContext.preciseMath(pc)) { - return cosine(Caster.toBigDecimal(number)); + return Caster.toBigDecimal(StrictMath.cos(Caster.toDoubleValue(number))); } - return StrictMath.cos(Caster.toDoubleValue(number)); - } - - public static BigDecimal cosine(BigDecimal x) { - BigDecimal sum = BigDecimal.ZERO; - BigDecimal term; - int maxIterations = 100; // More iterations, more precision - - for (int n = 0; n <= maxIterations; n++) { - BigDecimal numerator = pow(x, 2 * n).multiply(BigDecimal.valueOf(Math.pow(-1, n)), mc); - BigDecimal denominator = factorial(2 * n); - term = numerator.divide(denominator, mc); + return Caster.toDouble(StrictMath.cos(Caster.toDoubleValue(number))); - sum = sum.add(term, mc); - - // Check if the term is negligible - if (term.abs().compareTo(new BigDecimal("1E-29", mc)) < 0) { - break; - } - } - return sum; - } - - private static BigDecimal pow(BigDecimal base, int exponent) { - BigDecimal result = BigDecimal.ONE; - for (int i = 1; i <= exponent; i++) { - result = result.multiply(base, mc); - } - return result; - } - - private static BigDecimal factorial(int n) { - BigDecimal result = BigDecimal.ONE; - for (int i = 2; i <= n; i++) { - result = result.multiply(BigDecimal.valueOf(i), mc); - } - return result; } } \ No newline at end of file diff --git a/core/src/main/java/lucee/runtime/functions/math/RandRange.java b/core/src/main/java/lucee/runtime/functions/math/RandRange.java index 949143fb25..29328a8845 100644 --- a/core/src/main/java/lucee/runtime/functions/math/RandRange.java +++ b/core/src/main/java/lucee/runtime/functions/math/RandRange.java @@ -1,54 +1,46 @@ +/** + * + * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + */ package lucee.runtime.functions.math; -import java.math.BigDecimal; - import lucee.runtime.PageContext; -import lucee.runtime.engine.ThreadLocalPageContext; import lucee.runtime.exp.ExpressionException; import lucee.runtime.ext.function.Function; import lucee.runtime.op.Caster; public final class RandRange implements Function { + private static final long serialVersionUID = 2380288324240209290L; - private static final long serialVersionUID = -695896210547038106L; - - public static Number call(PageContext pc, Number number1, Number number2) throws ExpressionException { + public static double call(PageContext pc, Number number1, Number number2) throws ExpressionException { return call(pc, number1, number2, "cfmx_compat"); } - public static Number call(PageContext pc, Number number1, Number number2, String algo) throws ExpressionException { - // Handle BigDecimal for precise math - if (ThreadLocalPageContext.preciseMath(pc)) { - BigDecimal min = Caster.toBigDecimal(number1); - BigDecimal max = Caster.toBigDecimal(number2); - - if (min.compareTo(max) > 0) { - BigDecimal tmp = min; - min = max; - max = tmp; - } - - BigDecimal diff = max.subtract(min); - BigDecimal randValue = Caster.toBigDecimal(Rand.call(pc, algo)).multiply(diff.add(BigDecimal.ONE)); - - return randValue.add(min); // Can return as BigDecimal - } + public static double call(PageContext pc, Number number1, Number number2, String algo) throws ExpressionException { - // Handle double for non-precise math - double min = number1.doubleValue(); - double max = number2.doubleValue(); + int min = Caster.toIntValue(number1); + int max = Caster.toIntValue(number2); - // Swap min and max if necessary if (min > max) { - double tmp = min; + int tmp = min; min = max; max = tmp; } - - // Calculate the difference and random value - int diff = (int) (max - min); - return ((int) (Rand.call(pc, algo).doubleValue() * (diff + 1))) + (int) min; - + int diff = max - min; + return (Caster.toIntValue(Rand.call(pc, algo)) * (diff + 1)) + min; } public static int invoke(int min, int max) throws ExpressionException { @@ -58,9 +50,8 @@ public static int invoke(int min, int max) throws ExpressionException { min = max; max = tmp; } - - // Calculate the difference and random value int diff = max - min; - return ((int) (Rand.call(null, "cfmx_compat").doubleValue() * (diff + 1))) + min; + return (Caster.toIntValue(Rand.call(null, "cfmx_compat")) * (diff + 1)) + min; } -} + +} \ No newline at end of file diff --git a/test/functions/Atn.cfc b/test/functions/Atn.cfc index 05efab310f..70caf72b23 100644 --- a/test/functions/Atn.cfc +++ b/test/functions/Atn.cfc @@ -2,9 +2,9 @@ component extends="org.lucee.cfml.test.LuceeTestCase"{ function run( testResults , testBox ) { describe( title="Test suite for Atn()", body=function() { it(title="Checking Atn() function", body = function( currentSpec ) { - assertEquals("0.291456794478",tostring(atn(0.3))); - assertEquals("0.915100700553",tostring(atn(1.3))); - assertEquals("-1.560796660108",tostring(atn(-100))); + assertEquals("0.2914567944778671",tostring(atn(0.3))); + assertEquals("0.9151007005533605",tostring(atn(1.3))); + assertEquals("-1.5607966601082315",tostring(atn(-100))); }); }); }