C ++ Vulgar fraction

Is there a vulgar fraction that can be used as a variable?

For example: I want to separate the integers 1 and 3. The result as int is 0, because the result of double is 0,333333333 . However, I want the result to be exactly 1/3 , and then use it in other program equations.

Is there a variable like integer ( int ), decimal ( double ), etc (which is declared and used in the same way)? Or should I do it myself?

At first I thought about how to do it myself, but I failed.

Thanks!

+4
source share
3 answers

Not in the main language, but there are libraries like boost :: Rational .

+10
source

I'm not sure what you are asking, but look at Boost.Rational .

With it, you can write code, for example:

 #include <boost/rational.hpp> boost::rational<int> one(1); boost::rational<int> three(3); boost::rational<int> one_third(one / three); 
+9
source

There are no rational numbers in stl now, it will be part of stl in c0x-upcomming C ++ standard. You can try boost or write yourself.

+2
source

All Articles