What is the best way to work with multiplying and dividing fractions in Javascript?
An example of one of the errors I received when multiplying 1/5 times 1/10 can be seen with the following code in the console:
console.log( (1/5) * (1/10));
which prints 0.020000000000000004 instead of 0.02.
I'm trying to perform some chemistry equilibrium calculations (stoichiometry) on the client side, in Javascript. The math requires some matrix calculations including determining the nullspace, or kernel, of two matrices. I was able to find a library called sylvester.js for some of the matrix math but still was unable to find any javascript implementation for finding the kernel. As part of my implementation I need to be able to multiply fractions reliably.
I considered rounding to some precision but felt uneasy about that and started rewriting the code to only use integers and just multiply everything by a set number so I don't have to use fractions. This still doesn't seem like the best way though. What should I do?
EDIT I should mention that one of my requirements is that I have clear, readable code and that the domain is well understood in terms of fractions. I'd like to write the operations that are being performed in such a way that I'm not just working around floating point math being broken. My question is different than "Is floating point math broken?" in that I'm not asking whether or not there is a problem, I know there is a problem, but how do I get around the issue and still write clean code in terms of fractions. Also when searching for fractions in javascript the other answer doesn't even show up.