How to output fraction instead of decimal number?

后端 未结 13 2063
余生分开走
余生分开走 2020-12-01 21:59

In C++, When I calculate 2/3, it will output decimal values, how can I just get the original format (i.e.g 2/3) instead of 0.66666667

Thanks

13条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-01 22:32

    To simplify efforts, I suggest you stick with known denominators if possible.

    I'm working with an application where the fractions are restricted to denominators of powers of 2 or using 3 (for thirds).

    I convert to these fractions using an approximation (rounding to the nearest 1.0/24.0).

    Without some restrictions, finding the denominator can be quite a chore and take up a lot of the execution time.

提交回复
热议问题