March 19, 2018 at 5:13 am
I am looking for a way to add or subtract fractions stored as
2/3 , 7/5 or 2/7 , -5/6 etc
in sql server 2005 .
The output should be in fractions only as 1/4 + 2/4 = 3/4
March 19, 2018 at 5:28 am
shiwani.plrs - Monday, March 19, 2018 5:13 AMI am looking for a way to add or subtract fractions stored as
2/3 , 7/5 or 2/7 , -5/6 etc
in sql server 2005 .
The output should be in fractions only as 1/4 + 2/4 = 3/4
Does this look like what you need?
I'm assuming that you are able to code the arithmetic part & just need help converting that to a fraction afterwards.
March 19, 2018 at 8:29 am
Using fractions like 1/3 or even 1/10 is always risky when using float calculations. Both 1/3 and 1/10 are not exactly presented in most binairy systems.
A1, A2, B1, B2 all being integers. Calculations can be done with integers (or big ints) only.
So a sum like : A1/B1 + A2/B2
Calculate :
Numerator: (A1*B2 + A2*B1)
Divisor: (B1 * B2)
Both Numerator and Diviser should be devided by their GCD. (Greatest Common Divider).
For a function for the GCD see :
GCD function 1
GCD function 2
You'll probably be able to find more GCD functions on line, which better conforms to your problem.
For very large numbers
Numerator: (A1*B2 / (GCD(B1,B2) + A2*B1/ (GCD(B1,B2))
Divisor: (B1 * B2)/ (GCD(B1,B2)
And then divided by the GCD of both results.
Ben
Viewing 3 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy