Jump to content
Server Maintenance This Week. ×

Calc to reduce fractions?


This topic is 6165 days old. Please don't post here. Open a new topic instead.

Recommended Posts

I was wondering if anyone would know of a calculation that could reduce fractions to whole numbers and fractions after a fraction has been multiplied. As an example... 1/3 x 5 = 1 2/3

Thank you for your help.

Mitch

Link to comment
Share on other sites

You can use this formula to convert a decimal number (from field 'Num') to the simplest whole number and fraction:

Let ( [precision=100;

p0=Abs(Num);n0=Mod(p0;1);

p1=1/Case(n0 ;n0;1);n1=Mod(p1;1);

p2=1/Case(n1 and (p1/n1<precision);n1;1);n2=Mod(p2;1);

p3=1/Case(n2 and (p1*p2/n2<precision);n2;1);n3=Mod(p3;1);

p4=1/Case(n3 and (p1*p2*p3/n3<precision);n3;1);n4=Mod(p4;1);

p5=1/Case(n4 and (p1*p2*p3*p4/n4<precision);n4;1);n5=Mod(p5;1);

p6=1/Case(n5 and (p1*p2*p3*p4*p5/n5<precision);n5;1);n6=Mod(p6;1);

p7=1/Case(n6 and (p1*p2*p3*p4*p5*p6/n6<precision);n6;1);

d=Round(p1*p2*p3*p4*p5*p6*P7;0); n=Round(p0*d;0);nf=Round(n0*d;0);

sgn=Choose(Sign(Num)+1;"-";"";"");

whole=Div(n;d);

fraction=Case(d=1;"";nf;nf&"/"&d;"")];

Trim(sgn&Case(not p0;0;whole;whole&" ";"")&fraction))

With 'precision' set to 100 it will find the simplest fraction that is accurate to within 1/100. Changing that value will change the required precision of the result accordingly.

< edited: corrected a problem when the input value was zero. >

Link to comment
Share on other sites

Thank you so much for this great calculation, but I'm not sure how to use it. Currently I'm trying to use it with three fields called "Fraction", "FractionMultiplier" and "FractionResult". Would I need more than these three fields? How would this be written?

Thank you so much for your help!

Mitch

Link to comment
Share on other sites

You're not saying much about the rules of such multiplication. Based on your single example, I'm guessing you want something like this:

FractionResult (result is text) =

Let ( [

pos = Position ( Fraction ; "/" ; 1 ; 1 ) ;

num = Left ( Fraction ; pos - 1 ) ;

denom = Right ( Fraction ; length ( Fraction ) - pos ) ;

integer = Div ( num * FractionMultiplier ; denom ) ;

num1 = Mod ( num * FractionMultiplier ; denom )

] ;

Case ( integer ; integer & " " ) &

Case ( num1 ; num1 & "/" & denom )

)

Link to comment
Share on other sites

Here's my scenario...

The user would type a fraction in the first field called "Fraction"(The user is instructed to enter fractions only, but it would be nice if the calculation could handle whole numbers and fraction just in case it was entered incorrectly), then the user would enter the multiplier in the "FractionMultiplier" field and the "FractionResult" field would disply the multiplied fraction. The multiplied fraction would be reduced to lowest terms in whole numbers and fractions. Does a script/button need to be created to trigger the calculation?

Thank you,

Mitch

Link to comment
Share on other sites

It really depends on what KIND of fractions you expect as input.

Basically, there are two ways to approach this: either you multiply the numerator only, or you convert the fraction to a decimal representation first.

In the first case, the problem is to reduce the result: e.g. 1/4 * 2 = 2/4, and this needs be reduced to 1/2. This requires finding the greatest common divisor, and IIRC this is a recursive calc, so you need either a script or a custom function.

In the second case, 1/3 * 2 becomes 0.3333333 * 2 = .66666666, and it is difficult to get a computer to realize that this is close enough to 2/3.

There is a nice shortcut for fractions with multiples of 2 in the denominator (such as used for measurements in inches)- see http://www.onegasoft.com/tools/commonfractions/index.shtml

Link to comment
Share on other sites

You can increase the precision as necessary, but as you stated earlier, if you have a decimal result of .666666 should the result be the inaccurate fraction 2/3 or the exact fraction 666666/1000000?

I assume the user would want 2/3 so I made the calculation find the most accurate representation with a denominator of 128 or smaller. But, it's a simple change to the precision value to get larger denominators.

Link to comment
Share on other sites

if you have a decimal result of .666666 should the result be the inaccurate fraction 2/3 or the exact fraction 666666/1000000?

I don't know. That's why I pointed out the difficulties. If it's acceptable to have some limits placed upon a solution, that's fine. But I believe the limits should be stated.

Link to comment
Share on other sites

In my first post where I gave the continued fraction formula, I did explain about setting the value of the precision variable to the desired maximum allowed denominator.

I should also explain that it is possible for a decimal number to expand into more than the 7 terms given in the formula, in which case the result would not be exact. However that situation would result in a very large denominator, and the user would have to decide if the resulting approximation to a smaller denominator fraction is acceptable.

Link to comment
Share on other sites

Ah well, I've certainly been guilty of that more times than I care to remember. grin.gif

I have discovered that while the continued fraction algorithm, that this calc is based on, will find the simplest fraction in theory, in the real world (i.e., limited precision of computer arithmetic), the roundoff errors accumulate surprisingly fast in this particular formula. So, accumulated roundoff error in the formula is at least as significant as the accuracy of the original input number. But, I think this formula should be okay for fractions with up to 4 digit denominators.

Link to comment
Share on other sites

  • 2 years later...
  • Newbies

:

Hi, I took the time to join after finding these posts through a search. I joined just so I could thank you for these posts. I have been trying to get FM to work for recipes since version 2.1. I am not advanced enough to have figured this cacl out for myself anyway but I have tried to play with every version from 2.1 to 5.5 and could not get

1 1/3 mulitplied by double (2) to look like 2 2/3.

You guys are the GREATEST. thanks. However, because of you I now have no excuse not to enter 3,000 recipes that are building bigger every day.

Link to comment
Share on other sites

This topic is 6165 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.