Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello,
I'm wondering if we can do math fraction operations and also store in columns, by keeping it always in fractions format (dividend/divisor) including if there's an integer before the fraction like 5¼.
I did some readings and I only found workarounds. I'm not doing the workarounds yet because a scientific calculator can do what I want (except for the "storing" part) so I assume there's a built-in function or easy way to achieve this in MySQL also.
In scientific calculators it's something like:
5¬3¬4 + 2¬3¬4 = 8¬1¬2
Can't we do the above with MySQL? While also storing the result in fraction format?
I really need to add/subtract a column with fraction numbers, and it have to be fraction to keep it precise.
Like if I UPDATE table SET col = col+3¬1¬3 three times, the column value have to be 10 exactly.
Thank you

I've decided to use 2 INT columns to store the dividend and divisor, and do the corresponding process' to get as close as I wanted above. Still many flaws, for example, cannot store 5¼ but it needs to be 21 & 4.
I'm still open to suggestions =)
---
Help!

As you've figured out, computers aren't well suited to fractions. I think that you've hit upon the best solution (two columns, for numerator and denominator).
You could add a third column for the whole number part of the value, but I like the two column approach better for the actual storage of the value.
21/4 is the same value as 5 1/4, the 'display format' is just different. I think that storing and doing calculations in the two column format will avoid a lot of programming hassles. You won't have to worry about simplifying (ie changing 3 9/4 to 5 1/4 during calculations etc). You will do that only when it's time to display.

Thanks, you convince me.
As you suggested, I'll keep the 2 column style so I can still "SORT BY col_a/col_b" which sounds faster than "SORT BY ((col_c*col_a)+col_b)/col_c". *ouch*
--
Still anyways, does anyone has an idea on why there's no fraction functions in database operation? Or in any programming language?
If a calculator can do that, why couldn't they put it in SQLs or PHP?
Not worth it / not useful enough?
:D
---
Help!

The only useful hit returned by a google search on "php fractions" is:
http://www.evolt.org/working-with-f...
It concerns mostly using CSS to display fractions but does seem to include so code samples that might help. I just skimmed the article.

Laler,
I think I might be able to shed some light on why a "calculator can do that" but not PHP, C++, etc. It's my understanding (defintely could be wrong though) that calculators don't have "fraction operations" and "integer operations". Instead, they store the fraction as two numbers (like you) and use integer operations on them. Don't work with mixed fractions--they are are abandoned in higher math and you can convert to a mixed fraction later if needed.
I diverge...when the calculator adds two fractions, the denominators are compared first. The rest is described at "http://www.themathpage.com/ARITH/add-fractions-subtract-fractions-1.htm". A cheat to avoid Least Common Multiple (LCM) is to simply "cross multiply" and add the numerators. This can result in a non-simplified fraction, so it's not perfect.
Frac1.Num=1
Frac1.Denom=4Frac2.Num=1
Frac2.Denom=3Frac3.Num=Frac1.Num*Frac2.Denom+Frac2.Num*Frac1.Denom
Frac3.Denom=Frac1.Denom*Frac2.DenomFrac3 will equal 7/12. This _is_ in simplified form since 12 is the LCM of 3 and 4. In the case of 1/4 and 1/6, we are not so lucky and end with 10/24. Simplified would be 5/12.
To _always_ get a simplified fraction (like a calculator), you will need to look into an LCM algorithm. You should be able to Google for one (every calculator out there does it).
Multiplying fractions will use the Greatest Common Factor (GCF)--not the LCM. You can also find information about this on the web.
You can implement all this in pretty much any language. SQL, I'm really not sure about, but I found a GCF (GC Denominator in this article) at "http://allennoakes.dotnetdevelopersjournal.com/convert_decimal_fraction.htm". Looks like a little bit of work, but might be doable. I would prefer to do the math in PHP and then store in SQL, but that's me.
Stephen
"Live long and PROGRAM......or at least do _something_ with all that time...!"

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |