13,707,319 members

#### Stats

80.6K views
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.
 ```﻿using System; using System.Text; namespace MathParserNet { [Serializable] public class SimplificationReturnValue { private const int FactorDefault = 100000; public enum ReturnTypes { Float, Integer } /// /// Gets or sets the original equation passed into the parser /// public string OriginalEquation { get; set; } /// /// Gets or sets the value, if the return type is ReturnTypes.Float /// public double DoubleValue { get; set; } /// /// Gets or sets the value, if the return type is ReturnTypes.Integer /// public int IntValue { get; set; } /// /// Gets or sets the return type. Use this to determine if the value is a float, integer or an error occured /// public ReturnTypes ReturnType { get; set; } /// /// 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. /// /// The fraction public string ToFraction() { return ToFraction("/", " ", FactorDefault); } /// /// Converts the return type to a fraction. You must specify a default numerator/denomiator seperator character. /// /// The default numerator/denominator to use as a seperator /// Set to the highest precision you want. The numerator and/or denominator will never be higher than this number. /// The Fraction 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 1) sb.Append(Math.Round(d*bestDenominator)).Append(fractionSeperator).Append(bestDenominator); return sb.ToString().Trim(); } } } ```

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.

## Share

 Architect 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.