Float Rounding

This commit is contained in:
Alex Dyachenko 2014-06-27 09:35:37 -04:00
parent d161281a47
commit 94a9578826
6 changed files with 104 additions and 0 deletions

View File

@ -115,6 +115,9 @@
<Compile Include="..\..\..\mpir.net\mpir.net-tests\HugeFloatTests\IO.cs"> <Compile Include="..\..\..\mpir.net\mpir.net-tests\HugeFloatTests\IO.cs">
<Link>HugeFloatTests\IO.cs</Link> <Link>HugeFloatTests\IO.cs</Link>
</Compile> </Compile>
<Compile Include="..\..\..\mpir.net\mpir.net-tests\HugeFloatTests\Math.cs">
<Link>HugeFloatTests\Math.cs</Link>
</Compile>
<Compile Include="..\..\..\mpir.net\mpir.net-tests\HugeIntTests\Arithmetic.cs"> <Compile Include="..\..\..\mpir.net\mpir.net-tests\HugeIntTests\Arithmetic.cs">
<Link>HugeIntTests\Arithmetic.cs</Link> <Link>HugeIntTests\Arithmetic.cs</Link>
</Compile> </Compile>

View File

@ -34,6 +34,7 @@ namespace MPIR.Tests.HugeFloatTests
} }
var exponent = expected.IndexOf('.'); var exponent = expected.IndexOf('.');
if(exponent < 0) exponent = expected.Length;
expected = expected.Replace(".", ""); expected = expected.Replace(".", "");
var exponentStr = "@" + exponent; var exponentStr = "@" + exponent;

View File

@ -60,6 +60,8 @@ namespace MPIR.Tests.HugeFloatTests
VerifyPartialResult(r, expr, -35); VerifyPartialResult(r, expr, -35);
expr = expr + b.SquareRoot() + HugeFloat.SquareRoot(25) + ((b - 2) ^ 3) - (-b).RelativeDifferenceFrom(a + 1); expr = expr + b.SquareRoot() + HugeFloat.SquareRoot(25) + ((b - 2) ^ 3) - (-b).RelativeDifferenceFrom(a + 1);
VerifyPartialResult(r, expr, -19); VerifyPartialResult(r, expr, -19);
expr = expr - (a / 4).Floor() + (b / 3).Ceiling() - (a / b).Truncate();
VerifyPartialResult(r, expr, -12);
MarkExpressionsUsed(allExpressions, expr); MarkExpressionsUsed(allExpressions, expr);
} }

View File

@ -0,0 +1,66 @@
/*
Copyright 2014 Alex Dyachenko
This file is part of the MPIR Library.
The MPIR 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 3 of the License, or (at
your option) any later version.
The MPIR 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 the MPIR Library. If not, see http://www.gnu.org/licenses/.
*/
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MPIR.Tests.HugeFloatTests
{
[TestClass]
public class MiscMath
{
[TestMethod]
public void FloatFloor()
{
using(var a = new HugeFloat("-9023409872309847509847.9385345098345"))
using(var b = new HugeFloat())
{
b.Value = a.Floor();
FloatAssert.AreEqual("-9023409872309847509848", b);
b.Value = (-a).Floor();
FloatAssert.AreEqual("9023409872309847509847", b);
}
}
[TestMethod]
public void FloatCeiling()
{
using(var a = new HugeFloat("-9023409872309847509847.9385345098345"))
using(var b = new HugeFloat())
{
b.Value = a.Ceiling();
FloatAssert.AreEqual("-9023409872309847509847", b);
b.Value = (-a).Ceiling();
FloatAssert.AreEqual("9023409872309847509848", b);
}
}
[TestMethod]
public void FloatTruncate()
{
using(var a = new HugeFloat("-9023409872309847509847.9385345098345"))
using(var b = new HugeFloat())
{
b.Value = a.Truncate();
FloatAssert.AreEqual("-9023409872309847509847", b);
b.Value = (-a).Truncate();
FloatAssert.AreEqual("9023409872309847509847", b);
}
}
}
}

View File

@ -288,6 +288,9 @@ namespace MPIR
MAKE_UNARY_OPERATOR (MPEXPR_NAME, DEFINE, -, Negate, Flt) MAKE_UNARY_OPERATOR (MPEXPR_NAME, DEFINE, -, Negate, Flt)
MAKE_VOID_FUNCTION (MPEXPR_NAME, DEFINE, Abs, Flt) MAKE_VOID_FUNCTION (MPEXPR_NAME, DEFINE, Abs, Flt)
MAKE_VOID_FUNCTION (MPEXPR_NAME, DEFINE, Floor, Flt)
MAKE_VOID_FUNCTION (MPEXPR_NAME, DEFINE, Ceiling, Flt)
MAKE_VOID_FUNCTION (MPEXPR_NAME, DEFINE, Truncate, Flt)
MAKE_VOID_FUNCTION (MPEXPR_NAME, DEFINE, SquareRoot, Flt) MAKE_VOID_FUNCTION (MPEXPR_NAME, DEFINE, SquareRoot, Flt)
MAKE_FUNCTION_WITH_ONE (MPEXPR_NAME, DEFINE, RelativeDifferenceFrom, Flt) MAKE_FUNCTION_WITH_ONE (MPEXPR_NAME, DEFINE, RelativeDifferenceFrom, Flt)
@ -299,6 +302,9 @@ namespace MPIR
DEFINE_UNARY_ASSIGNMENT_REF(Negate, Flt, MP(neg)) DEFINE_UNARY_ASSIGNMENT_REF(Negate, Flt, MP(neg))
DEFINE_UNARY_ASSIGNMENT_REF(Abs, Flt, MP(abs)) DEFINE_UNARY_ASSIGNMENT_REF(Abs, Flt, MP(abs))
DEFINE_UNARY_ASSIGNMENT_REF(Floor, Flt, MP(floor))
DEFINE_UNARY_ASSIGNMENT_REF(Ceiling, Flt, MP(ceil))
DEFINE_UNARY_ASSIGNMENT_REF(Truncate, Flt, MP(trunc))
DEFINE_UNARY_ASSIGNMENT_REF(SquareRoot, Flt, MP(sqrt)) DEFINE_UNARY_ASSIGNMENT_REF(SquareRoot, Flt, MP(sqrt))
DEFINE_UNARY_ASSIGNMENT_VAL(SquareRoot, Ui, MP(sqrt_ui)) DEFINE_UNARY_ASSIGNMENT_VAL(SquareRoot, Ui, MP(sqrt_ui))

View File

@ -293,6 +293,10 @@ namespace MPIR
/// <returns>An expression object that, when assigned to the Value property or consumed by a primitive-returning method, computes the requested operation</returns> /// <returns>An expression object that, when assigned to the Value property or consumed by a primitive-returning method, computes the requested operation</returns>
static MPEXPR_NAME^ operator ^ (MPEXPR_NAME^ a, mpir_ui power); static MPEXPR_NAME^ operator ^ (MPEXPR_NAME^ a, mpir_ui power);
#pragma endregion
#pragma region Math
/// <summary>Computes the absolute value of the source number. /// <summary>Computes the absolute value of the source number.
/// <para>As with all expressions, the result is not computed until the expression is assigned to the Value property or consumed by a method. /// <para>As with all expressions, the result is not computed until the expression is assigned to the Value property or consumed by a method.
/// </para></summary> /// </para></summary>
@ -305,6 +309,24 @@ namespace MPIR
/// <returns>An expression object that, when assigned to the Value property or consumed by a primitive-returning method, computes the requested operation</returns> /// <returns>An expression object that, when assigned to the Value property or consumed by a primitive-returning method, computes the requested operation</returns>
MPEXPR_NAME^ SquareRoot(); MPEXPR_NAME^ SquareRoot();
/// <summary>Rounds the source number down to the next integer.
/// <para>As with all expressions, the result is not computed until the expression is assigned to the Value property or consumed by a method.
/// </para></summary>
/// <returns>An expression object that, when assigned to the Value property or consumed by a primitive-returning method, computes the requested operation</returns>
MPEXPR_NAME^ Floor();
/// <summary>Rounds the source number up to the next integer.
/// <para>As with all expressions, the result is not computed until the expression is assigned to the Value property or consumed by a method.
/// </para></summary>
/// <returns>An expression object that, when assigned to the Value property or consumed by a primitive-returning method, computes the requested operation</returns>
MPEXPR_NAME^ Ceiling();
/// <summary>Rounds the source number to the next integer toward zero.
/// <para>As with all expressions, the result is not computed until the expression is assigned to the Value property or consumed by a method.
/// </para></summary>
/// <returns>An expression object that, when assigned to the Value property or consumed by a primitive-returning method, computes the requested operation</returns>
MPEXPR_NAME^ Truncate();
#pragma endregion #pragma endregion
#pragma region Comparisons #pragma region Comparisons
@ -741,6 +763,10 @@ namespace MPIR
DEFINE_BINARY_EXPRESSION_WITH_TWO (MPEXPR_NAME, RelativeDifferenceFrom, Flt) DEFINE_BINARY_EXPRESSION_WITH_TWO (MPEXPR_NAME, RelativeDifferenceFrom, Flt)
DEFINE_UNARY_EXPRESSION_WITH_ONE (MPEXPR_NAME, Floor, Flt)
DEFINE_UNARY_EXPRESSION_WITH_ONE (MPEXPR_NAME, Ceiling, Flt)
DEFINE_UNARY_EXPRESSION_WITH_ONE (MPEXPR_NAME, Truncate, Flt)
#pragma endregion #pragma endregion
#pragma region HugeFloat class #pragma region HugeFloat class