Ejemplo n.º 1
0
        private void NormalizeDown()
        {
            FactorAccumulator faccu = new FactorAccumulator();
            int    a, b, u;
            double v, factor, lastfactor;

            v = this.range;
            b = this.scale.Length - 1;
            a = this.unitindex;
            u = this.unitindex;

            lastfactor = this.unitindex > 0 ? (double)this.scale[this.unitindex - 1] : 0.0;

            if (++a > b)
            {
                // stop, wenn rechts von der einheit kein faktor mehr
                this.normalizedrange   = v;
                this.normalizedfactors = string.Empty;
                this.normalizedunit    = this.GetFirstSynonymousUnit((string)this.scale[this.unitindex]);
                this.upticks           = lastfactor;
                this.downticks         = 0.0;

                this.prettyfactor = 0.0;
                this.prettyunit   = null;
                return;
            }

            factor = (double)this.scale[a];

            while (Math.Abs(v) < 1.0 && a < b)
            {
                v *= factor;
                faccu.Accumulate(factor);

                ++a;

                if (this.scale[a] is string)
                {
                    u = a;
                    faccu.Reset();

                    if (++a > b)
                    {
                        // stop, wenn rechts von der einheit kein faktor mehr
                        this.normalizedrange   = v;
                        this.normalizedfactors = string.Empty;
                        this.normalizedunit    = this.GetFirstSynonymousUnit((string)this.scale[u]);
                        this.upticks           = factor;
                        this.downticks         = 0.0;

                        this.prettyfactor = 0.0;
                        this.prettyunit   = null;
                        return;
                    }
                }

                lastfactor = factor;
                factor     = (double)this.scale[a];
            }

            this.prettyfactor = 0.0;
            this.prettyunit   = null;

            if (Math.Abs(v) < 1.0)
            {
                // Umgeht Exponentenschreibweise bei größer 0.001
                if (Math.Abs(v) > 0.001)
                {
                    this.prettyfactor = 1.0;
                    this.prettyunit   = this.GetFirstSynonymousUnit((string)this.scale[u]);
                }

                // den letzten faktor ganz rechts durchteilen solange es geht
                while (Math.Abs(v) < 1.0)
                {
                    v *= factor;
                    faccu.Accumulate(factor);
                    this.prettyfactor /= factor;
                }

                lastfactor = factor;
            }
            else
            {
                // schleife oben abgebrochen, weil fertig formatiert, d.h. value >= 1.0

                // PRETTY PRINT
                // ----------------------------------------------------------------
                // man könnte schaun, ob es noch eine weitere kleinere einheit gibt
                // und die zahl mit dieser einheit ausgeben.
                // für solche conv-strings: x 10 10 10 z
                // 0.242 X  =>  2.42 * 10^-1 X  =>  242 Z
                double pf = 1.0;
                double f  = factor;

                while (a < b)
                {
                    pf *= f;
                    ++a;

                    if (this.scale[a] is string)
                    {
                        // gab noch eine kleinere einheit, also damit ausgeben
                        this.prettyunit   = this.GetFirstSynonymousUnit((string)this.scale[a]);
                        this.prettyfactor = pf;
                        break;
                    }

                    f = (double)this.scale[a];
                }
            }

            this.normalizedrange   = v;
            this.normalizedfactors = faccu.FormatNegative();
            this.normalizedunit    = this.GetFirstSynonymousUnit((string)this.scale[u]);
            this.upticks           = lastfactor;
            this.downticks         = factor;
        }
Ejemplo n.º 2
0
        private void NormalizeUp()
        {
            FactorAccumulator faccu = new FactorAccumulator();
            int    a, b, u;
            double v, factor, lastfactor;

            v = this.range;
            b = this.scale.Length - 1;
            a = this.unitindex;
            u = this.unitindex;

            lastfactor = this.unitindex < b ? (double)this.scale[this.unitindex + 1] : 0.0;

            if (--a < 0)
            {
                // stop, wenn links von der einheit kein faktor mehr
                this.normalizedrange   = v;
                this.normalizedfactors = string.Empty;
                this.normalizedunit    = this.GetFirstSynonymousUnit((string)this.scale[this.unitindex]);
                this.upticks           = 0.0;
                this.downticks         = lastfactor;

                this.prettyfactor = 0.0;
                this.prettyunit   = null;
                return;
            }

            factor = (double)this.scale[a];

            while (Math.Abs(v) >= factor && a > 0)
            {
                v /= factor;
                faccu.Accumulate(factor);

                --a;

                if (this.scale[a] is string)
                {
                    u = a;
                    faccu.Reset();

                    if (--a < 0)
                    {
                        // stop, wenn links von der einheit kein faktor mehr
                        this.normalizedrange   = v;
                        this.normalizedfactors = string.Empty;
                        this.normalizedunit    = this.GetFirstSynonymousUnit((string)this.scale[u]);
                        this.upticks           = 0.0;
                        this.downticks         = factor;

                        this.prettyfactor = 0.0;
                        this.prettyunit   = null;
                        return;
                    }
                }

                lastfactor = factor;
                factor     = (double)this.scale[a];
            }

            this.prettyfactor = 0.0;
            this.prettyunit   = null;

            if (Math.Abs(v) >= factor)
            {
                // Umgeht Exponentenschreibweise bei kleiner 1000
                if (Math.Abs(v) <= 10000)
                {
                    this.prettyfactor = 1.0;
                    this.prettyunit   = this.GetFirstSynonymousUnit((string)this.scale[u]);
                }

                // den letzten faktor ganz links durchteilen solange es geht
                while (Math.Abs(v) >= factor)
                {
                    v /= factor;
                    faccu.Accumulate(factor);
                    this.prettyfactor *= factor;
                }

                lastfactor = factor;
            }

            this.normalizedrange   = v;
            this.normalizedfactors = faccu.FormatPositive();
            this.normalizedunit    = this.GetFirstSynonymousUnit((string)this.scale[u]);
            this.upticks           = factor;
            this.downticks         = lastfactor;
        }