0

Hi programmers what the condition to sum the fraction number

1/2+1/3=5/6

2nd Dec 2017, 9:54 PM
Yamama Yousif
Yamama Yousif - avatar
2 Réponses
+ 1
Well you if a decimal approximation is good enough you can convert the fractions​to floating​ point numbers first. Then add them. If you need the fraction answer you can convert back by multiplying by different numbers until you get an integer. You allow for a tolerance like 1% or 0.01% etc. This because Floating​ point is an​ approximation​, and recurring decimals don't always multiply up correctly. If you want an easy error resistant to add fractions​, then you can make your​ own data type, probably implemented​ as a class. Or you could see if there is a library available to do this already. I suggest boost rational: http://www.boost.org/doc/libs/1_65_1/libs/rational/index.html
3rd Dec 2017, 4:17 AM
Jared Bird
Jared Bird - avatar
0
thank you jared 🌸🌸
3rd Dec 2017, 5:35 AM
Yamama Yousif
Yamama Yousif - avatar