CommonBits.cs :  » GIS » DeepEarth » GisSharpBlog » NetTopologySuite » Precision » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » GIS » DeepEarth 
DeepEarth » GisSharpBlog » NetTopologySuite » Precision » CommonBits.cs
using GisSharpBlog.NetTopologySuite.Utilities;
using BitConverterSystem.BitConverter;

namespace GisSharpBlog.NetTopologySuite.Precision{
    /// <summary> 
    /// Determines the maximum number of common most-significant
    /// bits in the mantissa of one or numbers.
    /// Can be used to compute the double-precision number which
    /// is represented by the common bits.
    /// If there are no common bits, the number computed is 0.0.
    /// </summary>
    public class CommonBits
    {
        /// <summary>
        /// Computes the bit pattern for the sign and exponent of a
        /// double-precision number.
        /// </summary>
        /// <param name="num"></param>
        /// <returns>The bit pattern for the sign and exponent.</returns>
        public static long SignExpBits(long num)
        {
            return num >> 52;
        }

        /// <summary>
        /// This computes the number of common most-significant bits in the mantissas
        /// of two double-precision numbers.
        /// It does not count the hidden bit, which is always 1.
        /// It does not determine whether the numbers have the same exponent - if they do
        /// not, the value computed by this function is meaningless.
        /// </summary>
        /// <param name="num1"></param>
        /// /// <param name="num2"></param>
        /// <returns>The number of common most-significant mantissa bits.</returns>
        public static int NumCommonMostSigMantissaBits(long num1, long num2)
        {
            int count = 0;
            for (int i = 52; i >= 0; i--)
            {
                if (GetBit(num1, i) != GetBit(num2, i))
                    return count;
                count++;
            }
            return 52;
        }

        /// <summary>
        /// Zeroes the lower n bits of a bitstring.
        /// </summary>
        /// <param name="bits">The bitstring to alter.</param>
        /// <param name="nBits">the number of bits to zero.</param>
        /// <returns>The zeroed bitstring.</returns>
        public static long ZeroLowerBits(long bits, int nBits)
        {
            long invMask = (1L << nBits) - 1L;
            long mask = ~invMask;
            long zeroed = bits & mask;
            return zeroed;
        }

        /// <summary>
        /// Extracts the i'th bit of a bitstring.
        /// </summary>
        /// <param name="bits">The bitstring to extract from.</param>
        /// <param name="i">The bit to extract.</param>
        /// <returns>The value of the extracted bit.</returns>
        public static int GetBit(long bits, int i)
        {
            long mask = (1L << i);
            return (bits & mask) != 0 ? 1 : 0;
        }

        private bool isFirst = true;
        private int commonMantissaBitsCount = 53;
        private long commonBits = 0;
        private long commonSignExp;

        /// <summary>
        /// 
        /// </summary>
        public CommonBits() { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="num"></param>
        public void Add(double num)
        {
#if !SILVERLIGHT
            long numBits = BitConverter.DoubleToInt64Bits(num);            
#else
      long numBits = GisSharpBlog.NetTopologySuite.Utilities.BitConverter.DoubleToInt64Bits(num);
#endif
            if (isFirst)
            {
                commonBits = numBits;
                commonSignExp = SignExpBits(commonBits);
                isFirst = false;
                return;
            }

            long numSignExp = SignExpBits(numBits);
            if (numSignExp != commonSignExp)
            {
                commonBits = 0;
                return;
            }            
            commonMantissaBitsCount = NumCommonMostSigMantissaBits(commonBits, numBits);
            commonBits = ZeroLowerBits(commonBits, 64 - (12 + commonMantissaBitsCount));
        }

        /// <summary>
        /// 
        /// </summary>
        public double Common
        {
            get
            {
#if !SILVERLIGHT
                return BitConverter.Int64BitsToDouble(commonBits);
#else
        return GisSharpBlog.NetTopologySuite.Utilities.BitConverter.Int64BitsToDouble(commonBits);
#endif
            }
        }

        /// <summary>
        /// A representation of the Double bits formatted for easy readability
        /// </summary>
        /// <param name="bits"></param>
        /// <returns></returns>
        public string ToString(long bits)
        {
#if !SILVERLIGHT
            double x = BitConverter.Int64BitsToDouble(bits);
#else
      double x = GisSharpBlog.NetTopologySuite.Utilities.BitConverter.Int64BitsToDouble(bits);
#endif
            string numStr = HexConverter.ConvertAny2Any(bits.ToString(), 10, 2);            
            string padStr = "0000000000000000000000000000000000000000000000000000000000000000" + numStr;
            string bitStr = padStr.Substring(padStr.Length - 64);
            string str = bitStr.Substring(0, 1) + "  " + bitStr.Substring(1, 12) + "(exp) "
                         + bitStr.Substring(12) + " [ " + x + " ]";
            return str;
        }
    }
}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.