Click here to Skip to main content
13,799,517 members
Click here to Skip to main content

Tagged as

Stats

82.6K views
2.4K downloads
92 bookmarked
Posted 19 Sep 2012
Licenced CPOL

Yet Another Math Parser (YAMP)

, 30 Sep 2012
Constructing a fast math parser using Reflection to do numerics like Matlab.
YAMP
YAMPCompare
LLMathParser
MathFormula
MathParser
MathParserNet
Exceptions
MathParserTK
YAMPCompare.pidb
YAMPConsole
YAMPConsole.pidb
Exceptions
Expressions
Functions
ArgumentFunctions
LinearAlgebra
LogicFunctions
Spectroscopy
StandardFunctions
Trigonometric
Interfaces
Numerics
Decompositions
Integration
Interpolations
ODE
Optimization
Others
Solvers
Operators
AssigmentOperators
BinaryOperators
DotOperators
LogicOperators
UnaryOperators
Values
YAMP.csproj.user
YAMP.pidb
YAMP.csproj.user
YAMP.pidb
using System;
using System.Text;

namespace MathParserNet
{
    [Serializable]
    public class SimplificationReturnValue
    {
        private const int FactorDefault = 100000;

        public enum ReturnTypes
        {
            Float,
            Integer
        }

        /// <summary>
        /// Gets or sets the original equation passed into the parser
        /// </summary>
        public string OriginalEquation
        {
            get;
            set; 
        }

        /// <summary>
        /// Gets or sets the value, if the return type is ReturnTypes.Float
        /// </summary>
        public double DoubleValue
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the value, if the return type is ReturnTypes.Integer
        /// </summary>
        public int IntValue
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the return type. Use this to determine if the value is a float, integer or an error occured
        /// </summary>
        public ReturnTypes ReturnType
        {
            get;
            set;
        }

        /// <summary>
        /// Converts the return type to a fraction. Uses a slash "/" as the default numerator/denominator seperator.
        /// Uses a space " " as the default whole number/fracational seperator.
        /// </summary>
        /// <returns>The fraction</returns>
        public string ToFraction()
        {
            return ToFraction("/", " ", FactorDefault);
        }

        /// <summary>
        /// Converts the return type to a fraction. You must specify a default numerator/denomiator seperator character.
        /// </summary>
        /// <param name="fractionSeperator">The default numerator/denominator to use as a seperator</param>
        /// <param name="factor">Set to the highest precision you want. The numerator and/or denominator will never be higher than this number.</param>
        /// <returns>The Fraction</returns>
        public string ToFraction(string fractionSeperator, int factor)
        {
            return ToFraction(fractionSeperator, " ", factor);
        }

        public string ToFraction(string fractionSeperator)
        {
            return ToFraction(fractionSeperator, " ", FactorDefault);
        }

        public string ToFraction(string fractionSeperator, string wholeNumberSeperator)
        {
            return ToFraction(fractionSeperator, wholeNumberSeperator, FactorDefault);
        }

        public string ToFraction(string fractionSeperator, string wholeNumberSeperator, int factor)
        {
            var sb = new StringBuilder();
            double d = 0f;

            if (ReturnType == ReturnTypes.Float)
                d = DoubleValue;
            if (ReturnType == ReturnTypes.Integer)
                d = IntValue;

            if (d<0)
            {
                sb.Append("-");
                d = -d;
            }
            var l = (long)d;
            if (l != 0)
            {
                sb.Append(l);
                sb.Append(wholeNumberSeperator);
            }
            d -= l;
            double error = Math.Abs(d);
            int bestDenominator = 1;
            for (int i=2;i<=factor;i++)
            {
                double error2 = Math.Abs(d - Math.Round(d*i)/i);
                if (error2<error)
                {
                    error = error2;
                    bestDenominator = i;
                }
            }
            if (bestDenominator > 1)
                sb.Append(Math.Round(d*bestDenominator)).Append(fractionSeperator).Append(bestDenominator);
            return sb.ToString().Trim();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Florian Rappl
Architect
Germany Germany
Florian lives in Munich, Germany. He started his programming career with Perl. After programming C/C++ for some years he discovered his favorite programming language C#. He did work at Siemens as a programmer until he decided to study Physics.

During his studies he worked as an IT consultant for various companies. After graduating with a PhD in theoretical particle Physics he is working as a senior technical consultant in the field of home automation and IoT.

Florian has been giving lectures in C#, HTML5 with CSS3 and JavaScript, software design, and other topics. He is regularly giving talks at user groups, conferences, and companies. He is actively contributing to open-source projects. Florian is the maintainer of AngleSharp, a completely managed browser engine.

You may also be interested in...

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.181207.3 | Last Updated 30 Sep 2012
Article Copyright 2012 by Florian Rappl
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid