summaryrefslogtreecommitdiff
path: root/Source/Basetypes/BigFloat.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Source/Basetypes/BigFloat.cs')
-rw-r--r--Source/Basetypes/BigFloat.cs250
1 files changed, 114 insertions, 136 deletions
diff --git a/Source/Basetypes/BigFloat.cs b/Source/Basetypes/BigFloat.cs
index 5b169263..8cde2cb9 100644
--- a/Source/Basetypes/BigFloat.cs
+++ b/Source/Basetypes/BigFloat.cs
@@ -17,7 +17,7 @@ namespace Microsoft.Basetypes
/// <summary>
/// A representation of a 32-bit floating point value
- /// Note that this value has a 1-bit sign, 8-bit exponent, and 24-bit mantissa
+ /// Note that this value has a 1-bit sign, 8-bit exponent, and 24-bit significand
/// </summary>
public struct BigFloat
{
@@ -25,33 +25,33 @@ namespace Microsoft.Basetypes
// the internal representation
[Rep]
- internal readonly long mantissa; //Note that the mantissa arrangement matches standard fp arrangement (most significant bit is farthest left)
+ internal readonly BigNum significand; //Note that the significand arrangement matches standard fp arrangement (most significant bit is farthest left)
[Rep]
- internal readonly int mantissaSize;
+ internal readonly int significandSize;
[Rep]
- internal readonly int exponent; //The value of the exponent is always positive as per fp representation requirements
+ internal readonly BigNum exponent; //The value of the exponent is always positive as per fp representation requirements
[Rep]
internal readonly int exponentSize; //The bit size of the exponent
[Rep]
- internal readonly String dec_value;
+ internal readonly String value; //Only used with second syntax
[Rep]
- internal readonly bool isDec;
+ internal readonly bool isNeg;
- public long Mantissa {
+ public BigNum Significand {
get {
- return mantissa;
+ return significand;
}
}
- public int Exponent {
+ public BigNum Exponent {
get {
return exponent;
}
}
- public int MantissaSize {
+ public int SignificandSize {
get {
- return mantissaSize;
+ return significandSize;
}
}
@@ -61,25 +61,26 @@ namespace Microsoft.Basetypes
}
}
- public String Decimal {
+ public bool IsNegative {
get {
- return dec_value;
+ return this.isNeg;
}
}
- public bool IsDec {
+ public String Value {
get {
- return isDec;
+ return value;
}
}
- public static BigFloat ZERO(int mantissaSize, int exponentSize) { return new BigFloat(0, 0, mantissaSize, exponentSize); } //Does not include negative zero
+ public static BigFloat ZERO(int exponentSize, int significandSize) { return new BigFloat(false, BigNum.ZERO, BigNum.ZERO, exponentSize, significandSize); } //Does not include negative zero
- private static readonly BIM two = new BIM(2);
- private static long two_n(int n) {
- long toReturn = 1;
+ private static readonly BigNum two = new BigNum(2);
+ private static readonly BigNum one = new BigNum(1);
+ private static BigNum two_n(int n) {
+ BigNum toReturn = one;
for (int i = 0; i < n; i++)
- toReturn = toReturn * 2;
+ toReturn = toReturn * two;
return toReturn;
}
@@ -95,18 +96,18 @@ namespace Microsoft.Basetypes
return new BigFloat(v.ToString(), 8, 24);
}
- public static BigFloat FromInt(int v, int exponentSize, int mantissaSize)
+ public static BigFloat FromInt(int v, int exponentSize, int significandSize)
{
- return new BigFloat(v.ToString(), exponentSize, mantissaSize);
+ return new BigFloat(v.ToString(), exponentSize, significandSize);
}
public static BigFloat FromBigInt(BIM v) {
return new BigFloat(v.ToString(), 8, 24);
}
- public static BigFloat FromBigInt(BIM v, int exponentSize, int mantissaSize)
+ public static BigFloat FromBigInt(BIM v, int exponentSize, int significandSize)
{
- return new BigFloat(v.ToString(), exponentSize, mantissaSize);
+ return new BigFloat(v.ToString(), exponentSize, significandSize);
}
public static BigFloat FromBigDec(BigDec v)
@@ -114,84 +115,67 @@ namespace Microsoft.Basetypes
return new BigFloat(v.ToDecimalString(), 8, 24);
}
- public static BigFloat FromBigDec(BigDec v, int exponentSize, int mantissaSize)
+ public static BigFloat FromBigDec(BigDec v, int exponentSize, int significandSize)
{
- return new BigFloat(v.ToDecimalString(), exponentSize, mantissaSize);
+ return new BigFloat(v.ToDecimalString(), exponentSize, significandSize);
}
[Pure]
- public static BigFloat FromString(string v) {
- String[] vals = v.Split(' ');
- if (vals.Length == 0 || vals.Length > 4)
- throw new FormatException();
- try
- {
- switch (vals.Length) {
- case 1:
- return new BigFloat(vals[0], 8, 24);
- case 2:
- return new BigFloat(Int32.Parse(vals[0]), Int64.Parse(vals[1]), 8, 24);
- case 3:
- return new BigFloat(vals[0], Int32.Parse(vals[1]), Int32.Parse(vals[2]));
- case 4:
- return new BigFloat(Int32.Parse(vals[0]), Int64.Parse(vals[1]), Int32.Parse(vals[2]), Int32.Parse(vals[3]));
- default:
- throw new FormatException(); //Unreachable
- }
- }
- catch (Exception) { //Catch parsing errors
- throw new FormatException();
- }
+ public static BigFloat FromString(String v, int exp, int sig) { //String must be
+ return new BigFloat(v, exp, sig);
}
- internal BigFloat(int exponent, long mantissa, int exponentSize, int mantissaSize) {
+ public BigFloat(bool sign, BigNum exponent, BigNum significand, int exponentSize, int significandSize) {
this.exponentSize = exponentSize;
this.exponent = exponent;
- this.mantissa = mantissa;
- this.mantissaSize = mantissaSize;
- this.isDec = false;
- this.dec_value = "";
+ this.significand = significand;
+ this.significandSize = significandSize;
+ this.isNeg = sign;
+ this.value = "";
}
- internal BigFloat(String dec_value, int exponentSize, int mantissaSize) {
+ public BigFloat(String value, int exponentSize, int significandSize) {
this.exponentSize = exponentSize;
- this.mantissaSize = mantissaSize;
- this.exponent = 0;
- this.mantissa = 0;
- this.isDec = true;
- this.dec_value = dec_value;
+ this.significandSize = significandSize;
+ this.exponent = BigNum.ZERO;
+ this.significand = BigNum.ZERO;
+ this.value = value;
+ this.isNeg = value[0] == '-';
//Special cases:
- if (this.dec_value.Equals("+oo") || this.dec_value.Equals("-oo") || this.dec_value.Equals("-zero"))
+ if (this.value.Equals("+oo") || this.value.Equals("-oo") || this.value.Equals("-zero"))
return;
- if (this.dec_value.ToLower().Equals("nan")) {
- this.dec_value = "NaN";
+ if (this.value.ToLower().Equals("nan")) {
+ this.value = "NaN";
return;
}
- if (this.dec_value.Equals("INF") || this.dec_value.Equals("+INF")) {
- this.dec_value = "+oo";
+ if (this.value.Equals("INF") || this.value.Equals("+INF"))
+ {
+ this.value = "+oo";
return;
}
- if (this.dec_value.Equals("-INF")) {
- this.dec_value = "-oo";
+ if (this.value.Equals("-INF"))
+ {
+ this.value = "-oo";
return;
}
- if (this.dec_value.Equals("+zero")) {
- this.dec_value = "0.0";
+ if (this.value.Equals("+zero"))
+ {
+ this.value = "0.0";
return;
}
//End special cases
- if (this.dec_value.IndexOf('.') == -1 && this.dec_value.IndexOf('e') == -1)
- this.dec_value += ".0"; //Assures that the decimal value is a "real" number
- if (this.dec_value.IndexOf('.') == 0)
- this.dec_value = "0" + this.dec_value; //Assures that decimals always have a 0 in front
+ if (this.value.IndexOf('.') == -1 && this.value.IndexOf('e') == -1)
+ this.value += ".0"; //Assures that the decimal value is a "real" number
+ if (this.value.IndexOf('.') == 0)
+ this.value = "0" + this.value; //Assures that decimals always have a 0 in front
}
- private BIM maxMantissa()
+ private BigNum maxsignificand()
{
- BIM result = new BIM(1);
- for (int i = 0; i < mantissaSize; i++)
+ BigNum result = one;
+ for (int i = 0; i < significandSize; i++)
result = result * two;
- return result - 1;
+ return result - one;
}
private int maxExponent() { return (int)Math.Pow(2, exponentSize) - 1; }
@@ -213,13 +197,13 @@ namespace Microsoft.Basetypes
[Pure]
public override int GetHashCode() {
- return Mantissa.GetHashCode() * 13 + Exponent.GetHashCode();
+ return significand.GetHashCode() * 13 + Exponent.GetHashCode();
}
[Pure]
public override string/*!*/ ToString() {
Contract.Ensures(Contract.Result<string>() != null);
- return isDec ? dec_value : String.Format("{0}x2^{1}", Mantissa.ToString(), Exponent.ToString());
+ return value=="" ? String.Format("{0}x2^{1}", significand.ToString(), Exponent.ToString()) : value;
}
@@ -229,41 +213,41 @@ namespace Microsoft.Basetypes
/// <summary>
/// NOTE: THIS METHOD MAY NOT WORK AS EXPECTED!!!
/// Converts the given decimal value (provided as a string) to the nearest floating point approximation
- /// the returned fp assumes the given mantissa and exponent size
+ /// the returned fp assumes the given significand and exponent size
/// </summary>
/// <param name="value"></param>
- /// <param name="mantissaSize"></param>
+ /// <param name="significandSize"></param>
/// <param name="exponentSize"></param>
/// <returns></returns>
- public static BigFloat Round(String value, int exponentSize, int mantissaSize)
+ public static BigFloat Round(String value, int exponentSize, int significandSize)
{
int i = value.IndexOf('.');
if (i == -1)
- return Round(BIM.Parse(value), 0, exponentSize, mantissaSize);
- return Round(i == 0 ? 0 : BIM.Parse(value.Substring(0, i)), BIM.Parse(value.Substring(i + 1, value.Length - i - 1)), exponentSize, mantissaSize);
+ return Round(BigNum.FromString(value), BigNum.ZERO, exponentSize, significandSize);
+ return Round(i == 0 ? BigNum.ZERO : BigNum.FromString(value.Substring(0, i)), BigNum.FromString(value.Substring(i + 1, value.Length - i - 1)), exponentSize, significandSize);
}
/// <summary>
/// NOTE: THIS METHOD MAY NOT WORK AS EXPECTED!!!!
- /// Converts value.dec_value to a the closest float approximation with the given mantissaSize, exponentSize
+ /// Converts value.dec_value to a the closest float approximation with the given significandSize, exponentSize
/// Returns the result of this calculation
/// </summary>
/// <param name="value"></param>
/// <param name="power"></param>
- /// <param name="mantissaSize"></param>
+ /// <param name="significandSize"></param>
/// <param name="exponentSize"></param>
/// <returns></returns>
- public static BigFloat Round(BIM value, BIM dec_value, int exponentSize, int mantissaSize)
+ public static BigFloat Round(BigNum value, BigNum dec_value, int exponentSize, int significandSize)
{
int exp = 0;
- BIM one = new BIM(1);
- BIM ten = new BIM(10);
- BIM dec_max = new BIM(0); //represents the order of magnitude of dec_value for carrying during calculations
+ BigNum one = new BigNum(1);
+ BigNum ten = new BigNum(10);
+ BigNum dec_max = new BigNum(0); //represents the order of magnitude of dec_value for carrying during calculations
//First, determine the exponent
while (value > one) { //Divide by two, increment exponent by 1
if (!(value % two).IsZero) { //Add "1.0" to the decimal
- dec_max = new BIM(10);
+ dec_max = new BigNum(10);
while (dec_max < dec_value)
dec_max = dec_max * ten;
dec_value = dec_value + dec_max;
@@ -275,7 +259,7 @@ namespace Microsoft.Basetypes
exp++;
}
if (value.IsZero && !dec_value.IsZero) {
- dec_max = new BIM(10);
+ dec_max = new BigNum(10);
while (dec_max < dec_value)
dec_max = dec_max * ten;
while (value.IsZero) {//Multiply by two, decrement exponent by 1
@@ -288,12 +272,12 @@ namespace Microsoft.Basetypes
}
}
- //Second, calculate the mantissa
- value = new BIM(0); //remove implicit bit
- dec_max = new BIM(10);
+ //Second, calculate the significand
+ value = new BigNum(0); //remove implicit bit
+ dec_max = new BigNum(10);
while (dec_max < dec_value)
dec_max = dec_max * ten;
- for (int i = mantissaSize; i > 0 && !dec_value.IsZero; i--) { //Multiply by two until the mantissa is fully calculated
+ for (int i = significandSize; i > 0 && !dec_value.IsZero; i--) { //Multiply by two until the significand is fully calculated
dec_value = dec_value * two;
if (dec_value >= dec_max) {
dec_value = dec_value - dec_max;
@@ -301,7 +285,7 @@ namespace Microsoft.Basetypes
}
}
- return new BigFloat(exp, Int64.Parse(value.ToString()), exponentSize, mantissaSize);
+ return new BigFloat(false, BigNum.ZERO, BigNum.FromString(value.ToString()), exponentSize, significandSize); //Sign not actually checked...
}
// ``floor`` rounds towards negative infinity (like SMT-LIBv2's to_int).
@@ -312,30 +296,32 @@ namespace Microsoft.Basetypes
/// </summary>
/// <param name="floor">The Floor (rounded towards negative infinity)</param>
/// <param name="ceiling">Ceiling (rounded towards positive infinity)</param>
- public void FloorCeiling(out BIM floor, out BIM ceiling) {
+ public void FloorCeiling(out BigNum floor, out BigNum ceiling) {
//TODO: fix for fp functionality
- BIM n = Mantissa;
- int e = Exponent;
+ BigNum n = Significand;
+ BigNum e = Exponent;
if (n.IsZero) {
floor = ceiling = n;
- } else if (0 <= e) {
+ } else if (BigNum.ZERO <= e) {
// it's an integer
- for (; 0 < e; e--) {
+ for (; BigNum.ZERO < e; e = e - one)
+ {
n = n * two;
}
floor = ceiling = n;
} else {
// it's a non-zero integer, so the ceiling is one more than the floor
- for (; e < 0 && !n.IsZero; e++) {
+ for (; BigNum.ZERO < e && !n.IsZero; e = e + one)
+ {
n = n / two; // Division rounds towards negative infinity
}
if (!IsNegative) {
floor = n;
- ceiling = n + 1;
+ ceiling = n + one;
} else {
ceiling = n;
- floor = n - 1;
+ floor = n - one;
}
}
Debug.Assert(floor <= ceiling, "Invariant was not maintained");
@@ -352,7 +338,7 @@ namespace Microsoft.Basetypes
[Pure]
public string ToDecimalString() {
Contract.Ensures(Contract.Result<string>() != null);
- return isDec ? dec_value : String.Format("{0}x2^{1}", Mantissa.ToString(), Exponent.ToString());
+ return value=="" ? String.Format("{0}x2^{1}", significand.ToString(), Exponent.ToString()) : value;
}
[Pure]
@@ -380,18 +366,16 @@ namespace Microsoft.Basetypes
[Pure]
public BigFloat Abs {
get {
- if (IsDec)
- return dec_value[0] == '-' ? new BigFloat(dec_value.Remove(0, 1), ExponentSize, MantissaSize) : this;
- return new BigFloat(Exponent, Math.Abs(Mantissa), ExponentSize, MantissaSize);
+ return new BigFloat(true, Exponent, Significand, ExponentSize, SignificandSize);
}
}
[Pure]
public BigFloat Negate {
get {
- if (IsDec)
- return dec_value[0] == '-' ? new BigFloat(dec_value.Remove(0, 1), ExponentSize, MantissaSize) : new BigFloat("-" + dec_value, ExponentSize, MantissaSize);
- return new BigFloat(Exponent, -Mantissa, ExponentSize, MantissaSize);
+ if (value != "")
+ return value[0] == '-' ? new BigFloat(value.Remove(0, 1), ExponentSize, significandSize) : new BigFloat("-" + value, ExponentSize, significandSize);
+ return new BigFloat(!isNeg, Exponent, Significand, ExponentSize, SignificandSize);
}
}
@@ -404,26 +388,26 @@ namespace Microsoft.Basetypes
public static BigFloat operator +(BigFloat x, BigFloat y) {
//TODO: Modify for correct fp functionality
Contract.Requires(x.ExponentSize == y.ExponentSize);
- Contract.Requires(x.MantissaSize == y.MantissaSize);
- long m1 = x.Mantissa;
- int e1 = x.Exponent;
- long m2 = y.Mantissa;
- int e2 = y.Exponent;
- m1 = m1 + two_n(x.mantissaSize + 1); //Add implicit bit
- m2 = m2 + two_n(y.mantissaSize + 1);
+ Contract.Requires(x.significandSize == y.significandSize);
+ BigNum m1 = x.significand;
+ BigNum e1 = x.Exponent;
+ BigNum m2 = y.significand;
+ BigNum e2 = y.Exponent;
+ m1 = m1 + two_n(x.significandSize + 1); //Add implicit bit
+ m2 = m2 + two_n(y.significandSize + 1);
if (e2 > e1) {
- m1 = y.Mantissa;
+ m1 = y.significand;
e1 = y.Exponent;
- m2 = x.Mantissa;
+ m2 = x.significand;
e2 = x.Exponent;
}
while (e2 < e1) {
- m2 = m2 / 2;
- e2++;
+ m2 = m2 / two;
+ e2 = e2 + one;
}
- return new BigFloat(e1, m1 + m2, x.MantissaSize, x.ExponentSize);
+ return new BigFloat(false, e1, m1 + m2, x.significandSize, x.ExponentSize);
}
[Pure]
@@ -434,8 +418,8 @@ namespace Microsoft.Basetypes
[Pure]
public static BigFloat operator *(BigFloat x, BigFloat y) {
Contract.Requires(x.ExponentSize == y.ExponentSize);
- Contract.Requires(x.MantissaSize == y.MantissaSize);
- return new BigFloat(x.Exponent + y.Exponent, x.Mantissa * y.Mantissa, x.MantissaSize, x.ExponentSize);
+ Contract.Requires(x.significandSize == y.significandSize);
+ return new BigFloat(x.isNeg ^ y.isNeg, x.Exponent + y.Exponent, x.significand * y.significand, x.significandSize, x.ExponentSize);
}
@@ -444,9 +428,9 @@ namespace Microsoft.Basetypes
public bool IsSpecialType {
get {
- if (!IsDec)
+ if (value == "")
return false;
- return (dec_value.Equals("NaN") || dec_value.Equals("+oo") || dec_value.Equals("-oo") || dec_value.Equals("-zero"));
+ return (value.Equals("NaN") || value.Equals("+oo") || value.Equals("-oo") || value.Equals("-zero"));
}
}
@@ -456,15 +440,9 @@ namespace Microsoft.Basetypes
}
}
- public bool IsNegative {
- get {
- return (isDec && dec_value[0] == '-') || mantissa < 0;
- }
- }
-
public bool IsZero {
get {
- return Mantissa == 0 && Exponent == 0;
+ return significand.Equals(BigNum.ZERO) && Exponent == BigNum.ZERO;
}
}
@@ -474,9 +452,9 @@ namespace Microsoft.Basetypes
return 1;
if (this.exponent < that.exponent)
return -1;
- if (this.Mantissa == that.Mantissa)
+ if (this.significand == that.significand)
return 0;
- return this.Mantissa > that.Mantissa ? 1 : -1;
+ return this.significand > that.significand ? 1 : -1;
}
[Pure]