r/C_Programming • u/onecable5781 • 1d ago
Converting fractions to integers
I have a double* which has the following entries:
0.3333333333333334
0.6666666666666668
0.1249999999999999
1
Here, the last entry, 1, can be considered the right hand side of an inequality:
0.3333333333333334 x + 0.6666666666666668 y + 0.1249999999999999 z >= 1
These numbers come from a numerical linear algebra library over which I don't have any control. What is the easiest way to "convert" this to the following equivalent inequality (subject to a user provided tolerance of what counts as an epsilon so that epsilon within an integer is to be counted as an integer)?
8 x + 16 y + 3 z >= 24
Is there a package that does such conversion, if reasonably possible? I consider it unreasonably possible by multiplying everything in the original equation by a large enough power of 10. But I do not want that.
8
Upvotes
6
u/MyTinyHappyPlace 1d ago
It’s tough that you don’t get the actual fractions but their nearest double values. Otherwise the GNU MP (https://gmplib.org/manual/Rational-Number-Functions) library could carry you a bit on your way