|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionA four line algorithm in MC++ for converting a decimal value to three separate pieces: units, numerator, denominator. Their results are suitable to format a string. FormulaThe basic formula is to divide the right side of the decimal value by the decimal equivalent of the fractional measure and round to an integer. This becomes the numerator over the desired denominator (of the conversion fraction). Thus, for converting to an eighth, as 1/8 is .125, one divides the
I use if (Math::Sign(remainder) == -1) remainder = 1 + remainder
Due to this check of the sign the algorithm only works for positive numbers. The Algorithm:(The decimal and denominator are input) Single remainder = Math::IEEERemainder(decimal, 1.0); if (Math::Sign(remainder) == -1) remainder = 1 + remainder; Int32 units = Convert::ToInt32(Math::Floor(decimal)); Int32 numerator = Convert::ToInt32(Math::Round(remainder/denominator)); Other considerations;For flexibility, one would prefer to provide an integer specifying the desired conversion rather than hard code, say, .125 as the denominator. Thus input an integer numerator and compute the divisor. // compute the fractions numerator Single divisor = Convert::ToSingle(1)/Convert::ToSingle(denominator); Int32 numerator = Convert::ToInt32(Math::Round(remainder/divisor)); The algorithm only works for positive decimals, thus one needs to test for flag, correct and restore negativity. Further, problems that need to be considered are the rounding down to zero and rounding up to the next unit, and reduction of the fraction. The following code accounts for these. The following code was written for a very specific purpose: to convert English inch measurement fractions, specifically, the common fractions 1/8, 1/4 and 1/2. (Although I tested to 1/32.) I was not interested in fractions like 1/5 or 1/7 or 1/324 whatever. The algorithm may be useful to help those, but not the example function. The code is not generalized. But the algorithm is. The code is only provided as a wrapper example. Code:#pragma warning( disable : 4244 ) // possible loss of data due to conversion
// Convert a Single to a string fraction of the
// form "integer numerator/denominator"
String* Utils::Form1::SingleToStringFraction(Single decimal, Int32 denominator)
{
// Input must be positive so save and restore the negative if necessary.
bool isneg = (Math::Sign(decimal) == -1) ? true : false;
if (isneg) decimal *= -1;
// obtain the decimal and units parts of the input number
Single remainder = Math::IEEERemainder(decimal, 1.0);
if (Math::Sign(remainder) == -1) remainder = 1 + remainder;
Int32 units = Convert::ToInt32(Math::Floor(decimal));
// compute the fractions numerator
Single divisor = Convert::ToSingle(1)/Convert::ToSingle(denominator);
Int32 numerator = Convert::ToInt32(Math::Round(remainder/divisor));
String* fraction;
// Handle an error or condition or reduce the fraction
// and convert to a string for the return.
if ((numerator > 0) && (numerator == denominator))
{
// than fraction is one full unit
units++;
fraction = S"";
}
else if (numerator == 0)
{
// as numerator is 0, no fraction
fraction = S"";
}
else
{
// reduce
while (numerator%2 == 0)
{
numerator /= 2;
denominator /= 2;
}
fraction = String::Format(" {0}/{1}",
numerator.ToString(), denominator.ToString());
}
// restore negativity
if (isneg) units *= -1;
#ifdef _DEBUG_CUT
String* rtnstr;
if (isneg) decimal *= -1;
rtnstr = String::Format("{0}{1}", units.ToString(), fraction);
Diagnostics::Trace::WriteLine(rtnstr, decimal.ToString());
#endif
return String::Format("{0}{1}", units.ToString(), fraction);
}
#pragma warning( default : 4244 )
CaveatI have never claimed to know everything. And, my MC++ skills may be lacking. If you know of a better and more efficient algorithm, or can improve on the quality of the above code, please comment. In the program that I am working on, I will be going back and forth from fractions to decimals regularly. Efficiency would be nice. Oh yes, one could simply convert to a string and use split on S"."; but what fun in that? And using
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||