Handling fractions

I'm trying to do a matrices program that will allow me to solve a matrix with 3x3 dimensions the answers being in a fourth line of the first dimension (technically making it a 4x3 matrix, but whatever), and my biggest problem is fractions. Computers don't quite accept fractions the way we do, so I can't quite code this without something to use for fraction multiplication/division. I was wondering if anyone knew of a class or library that allows computers to analyze fractions. I can make one myself if there's not, but I don't really want to go that route if there are easier solutions.
Maybe I'm misinterpreting your problem... but if you want to represent a fraction you literally have to type: double fraction = 3 / 4; I don't see what the problem is.
Last edited on
If I have 1/9 / 1/3, then I want to get 1/3 as a result. I don't know any classes that can do that, so if nobody else does, then I may have to make my own, which already seems like it's going to be a headache.
rational arithmetic class is a common student exercise. But if you don't feel like doing it, it has been available from boost for over a decade: http://www.boost.org/doc/libs/release/libs/rational/
Last edited on
I'm actually freelancing this, so it's not quite the same as a homework assignment. This almost seems more complicated than what I want. It's definitely more effective, but it seems like it won't work for converting other numbers into fractions like I want it too. Thanks for the link though.
I don't think defining your own fraction class would be that hard. Converting a decimal to a fraction pretty much involves multiplying the decimal by 100 and putting that value over 100 (i.e. .33333 = 33.333 / 100). From there just simplify, which again shouldn't be that hard with a for loop or recursion.
Last edited on
Yeah. I finally made it. I only have to change a few things to make it work a little better.
Topic archived. No new replies allowed.