private void CalculateFractionRepresentations(JereNumber number) { IsNegative = number._denominator == 0 || number._numerator == 0 ? false : number._numerator * number._denominator < 0; IsZero = number._numerator == 0 && number._denominator != 0; OriginalNumerator = number.OriginalNumerator; OriginalDenominator = number.OriginalDenominator; AsOriginalFraction = $"{OriginalNumerator}/{OriginalDenominator}"; SimplifiedNumerator = number._numerator; SimplifiedDenominator = number._denominator; //undefined already taken care of if (IsZero) { AsFraction = $"{SimplifiedNumerator}/{SimplifiedDenominator}"; AsFractionIsNoLoss = true; AsFractionNoLoss = $"{SimplifiedNumerator}/{SimplifiedDenominator}"; } else { AsFraction = $"{SimplifiedNumerator}/{SimplifiedDenominator}"; if (SimplifiedNumerator.SignificantDigits() < 28 && SimplifiedDenominator.SignificantDigits() < 28) { AsFractionIsNoLoss = true; AsFractionNoLoss = $"{SimplifiedNumerator}/{SimplifiedDenominator}"; } } }
} //todo as more complicated algebraic forms public Expression(string originalForm) { OriginalForm = originalForm; if (RegexPatterns.MixedNumberOrFractionOrNumber.IsMatch(originalForm)) { AsNumber = new JereNumber(originalForm); IsNumber = true; Representation = AsNumber.Representation; } else if (RegexPatterns.Point2DList.IsMatch(originalForm)) { AsCartesian2dPoints = new Cartesian2DPoints(originalForm); IsCartesian2dPoints = true; Representation = AsCartesian2dPoints.ToString(); } else if (RegexPatterns.DisplacementVector.IsMatch(originalForm)) { AsDisplacementVector = new DisplacementVector(originalForm); IsDisplacementVector = true; } else { throw new NotImplementedException("wurkin on it"); } }
private void ProcessDecimal(JereNumber number) { AsDecimal = (decimal)number._numerator / (decimal)number._denominator; //if longer than 28 decimal places = approximate, not rational //if has repeating decimal pattern, and longer than 28 decimal places = approximate, rational //https://stackoverflow.com/questions/1315595/algorithm-for-detecting-repeating-decimals //http://mathworld.wolfram.com/DecimalExpansion.html //https://softwareengineering.stackexchange.com/questions/192070/what-is-a-efficient-way-to-find-repeating-decimal //https://www.exceptionnotfound.net/decimal-vs-double-and-other-tips-about-number-types-in-net/ //https://fiziko.bureau42.com/teaching_tidbits/turning_repeating_decimals_into_fractions.pdf //https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/floating-point-numeric-types //https://docs.microsoft.com/en-us/dotnet/api/system.decimal.compareto?view=netframework-4.8#System_Decimal_CompareTo_System_Decimal_ //.compareTo() and equals() //a decimal may have precision of 28-29, does that include left and right? yes //Precision is 28-29, scale is # of digits after decimal point //Use brute force method for now if (AsDecimal.SignificantDigits() < 28) //No loss { AsDecimalIsNoLoss = true; AsDecimalNoLoss = AsDecimal; ProcessDecimalNoLoss((decimal)AsDecimalNoLoss); } else //All lesser types will be approximate too. { AsInt = (int)(number._numerator / number._denominator); AsLong = (long)(number._numerator / number._denominator); AsDouble = (double)(number._numerator / number._denominator); } }
private bool ProcessFraction(JereNumber number) { //IsNegative = number._denominator == 0 || _numerator == 0 ? false : _numerator * number._denominator < 0; //IsZero = number._numerator == 0 && number._denominator != 0; //OriginalNumerator = number.OriginalNumerator; //OriginalDenominator = number.OriginalDenominator; //AsOriginalFraction = $"{OriginalNumerator}/{OriginalDenominator}"; //SimplifiedNumerator = number._numerator; //SimplifiedDenominator = number._denominator; //SimplifiedAsFraction = $"{SimplifiedNumerator}/{SimplifiedDenominator}"; return(true); }
private void PopulateRepresentations(JereNumber number) { SimplifiedAsFraction = $"{SimplifiedNumerator}/{SimplifiedDenominator}"; OriginalAsFraction = $"{OriginalNumerator}/{OriginalDenominator}"; CalculateFractionRepresentations(number); CalculateMixedNumberRepresentations(); if (IsZero) { DefaultRepresentation = "0"; } else if (AsIntNoLoss.HasValue) { DefaultRepresentation = AsIntNoLoss.ToString(); } else if (AsLongNoLoss.HasValue) { DefaultRepresentation = AsLongNoLoss.ToString(); } else if (AsDoubleNoLoss.HasValue) { DefaultRepresentation = AsDoubleNoLoss.ToString(); } else if (AsDecimalNoLoss.HasValue) { DefaultRepresentation = AsDecimalNoLoss.ToString(); } else if (AsMixedNumberNoLoss != null) { DefaultRepresentation = AsMixedNumberNoLoss; } else if (AsFractionNoLoss != null) { DefaultRepresentation = AsFractionNoLoss; //would this ever hit? } else { DefaultRepresentation = SimplifiedAsFraction; //last one could be approximation if num or denom over 28 Significants long. } }
public Point(Expression x, JereNumber y) { _x = x; _y = y; }
public Metadata(JereNumber number) { ProcessDecimal(number); PopulateRepresentations(number); }
public Variable(Expression numerator, JereNumber denominator, string representation) { Expression = new Expression(new JereNumber(numerator, denominator)); Representation = representation; IsKnown = true; }
public Variable(JereNumber numerator, string representation) { Expression = numerator; Representation = representation; IsKnown = true; }
public Point(long x, JereNumber y) { _x = x; _y = y; }
public Point(int x, JereNumber y) { _x = x; _y = y; }
public Point(JereNumber x, decimal y) { _x = x; _y = y; }
public Point(JereNumber x, Expression y) { _x = x; _y = y; }
public Point(JereNumber x, double y) { _x = x; _y = (decimal)y; }
public Point(JereNumber x, long y) { _x = x; _y = y; }
public Point(JereNumber x, int y) { _x = x; _y = y; }
public Point(decimal x, JereNumber y) { _x = x; _y = y; }
public Point(double x, JereNumber y) { _x = (decimal)x; _y = y; }