Digit to fraction converter6/29/2023 Both arguments are 0, this should not happen.") Private static long getGreatestCommonDivisor(long i1, long i2)ĭebug.Assert(i1 != 0 || i2 != 0, "Whoops!. Result = new RationalNumber(left.numerator * (lcm / left.Denominator) + right.numerator * (lcm / right.Denominator), lcm) Long lcm = RationalNumber.getLeastCommonMultiple(left.Denominator, right.Denominator) Result = new RationalNumber(numerator,denominator) Long denominator = checked(left.Denominator * right.Denominator) Long numerator = checked(left.numerator * right.Denominator + right.numerator * left.Denominator) Cost penalty should be minimal as adding numbers that cause an overflow should be very rare. will be up to the consumer to decide what he will do with it. If an overflow occurs we use the least common multiple and return the result. First we try directly adding in a checked context. Public static RationalNumber operator +(RationalNumber left, RationalNumber right) Return (double)value.numerator / value.Denominator Public static explicit operator double(RationalNumber value) / A double-precision floating-point number that contains the resulting value of dividing the rational number's numerator by it's denominator. / The value to convert to a double-precision floating-point number. / Defines an explicit conversion of a rational number to a double-precision floating-point number. Public static implicit operator RationalNumber(long value) / A rational number that contains the value of the value parameter as its numerator and 1 as its denominator. / The value to convert to a rational number. / Defines an implicit conversion of a 64-bit signed integer to a rational number. Public RationalNumber(long numerator, long denominator) Private const int MAXITERATIONCOUNT = 20 Public struct RationalNumber : IComparable, IFormattable, IConvertible, IComparable, IEquatable / Represents a rational number with 64-bit signed integer numerator and denominator. Note that the implementation will give you the closest approximation with the required precision. It's a relatively simple algorithm that works reasonably well for any rational number approximation. In my case, converting from decimal to rational number had to be a little more mathematically correct so I ended up implementing a Continued Fraction algorithm.Īlthough it is tailored made to my concrete implementation of RationalNumber, you should get the idea. Recently I had to code a similar scenario. Long divisor = GCD(numerator, denominator) Long denominator = (long)Math.Pow(10, parts.Length) Here is my code, I'm wondering if there is a better approach, or if there is someway to safely convert these to longs without losing the precision needed for a correct result: public static String DecimalToFraction(double dec) This gets converted to a double, but cannot be converted back, because in my approach, the numerator causes an integer overflow. I have code now that works for the vast majority of numbers, but occasionally I will get a tiny fraction like 1/321. This way, I can iterate through the list and find the closest fraction, and simply display that. I have code which generates the decimal representation of every fraction 0->64 over 1->64, and saves the most simplified form. SO I have devised a very hacky way to do this. The code for accepting fractions and turning them in to a double works, but when I want to display that value, I need to convert back to a fractional representation.ĮDIT: I have fixed the overflow problem, but that didnt solve fractions like 1/3 or 5/6. Get the numerator by multiplying the decimal amount of the number by 10000Ġ.3685 * 10000 = 3685 3685 is the numerator of this fraction.ġ2.I am working on a system that needs to accept and display complex fractions. The denominator consists of a one plus four zero's The fourth digit, the 5, is in the fourth spot of the decimal part of the number. Get the numerator by multiplying the decimal amount of the number by 1000Ġ.875 * 1000 = 875 875 is the numerator of this fraction. The denominator consists of a one plus three zero's The third digit, the 5, is in the third spot of the decimal part of the number. This whole number is the numerator of the fraction. To get the fraction's numerator, multiply the decimal part of the number by the denominator to get a whole number. Note the denominator is always a one with as many 0's as there are digits behind the decimal point.
0 Comments
Leave a Reply. |