- User picks height in feet. E.g. 6 feet 0 inches
- User presses save button. Height is saved into db as cm
- User now sees height fetched as cm from db and converted back to feet and inches
- Due to Javascript rounding users height is not same as his chosen feet and inches
- How to accurately store feet and inches as cm and have the value be same that user picked?
We are using Realm as local database.
const testConversion = (feet: number, inches: number) => {
const cmTotal = feet * 30.48 inches * 2.54;
const feetNew = Math.floor(cmTotal / 30.48);
// How to have inches match up?
const inchesNew = (cmTotal % 30.48) * 0.393701;
expect(feetNew).toEqual(feet);
expect(inchesNew).toEqual(inches);
};
// 12 inch in a feet
// tallest man about is about 9 feet
describe("Height conversion from metric to imperial", () => {
it("Amount of stone and lb should stay the same", () => {
testConversion(6, 0);
for (let feet = 0; feet < 9; feet ) {
for (let inches = 0; inches < 12; inches ) {
testConversion(feet, inches);
}
}
});
});
Result:
● Test suite failed to run
expect(received).toEqual(expected) // deep equality
Expected: 0
Received: 12.000006479999998
8 |
9 | expect(feetNew).toEqual(feet);
> 10 | expect(inchesNew).toEqual(inches);
| ^
11 | };
Rounding inches in different ways doesn't seem to help, the end result is always off in some way. Any idea how to do apart from saving feet and inches into separate fields in db?
CodePudding user response:
Floating-point calculations are never accurate (Is floating point math broken?). And due to that your cmTotal % 30.48
in (cmTotal % 30.48) * 0.393701;
is problematic.
You expect that: 182.88 % 30.48
results in 0
but it, however, results in 30.479999999999993
due to the inaccuracies of floating-point arithmetic.
What you want to do is, that the result of newInch
depends on the result you calculated for feetNew
:
(cmTotal - feetNew * 30.48) * 0.393701;
And in your test you shouldn't do expect(inchesNew).toEqual(inches);
because you can't expect that the numbers of such conversion are exactly equal. You will always have some small error. So you probably want to use something like toBeCloseTo
instead.
let feet = 6
let inches = 0
const testConversion = (feet, inches) => {
const cmTotal = feet * 30.48 inches * 2.54;
const feetNew = Math.floor(cmTotal / 30.48);
// How to have inches match up?
const inchesNew = (cmTotal - feetNew * 30.48) * 0.393701;
console.log(feet, feetNew)
console.log(inches, inchesNew)
};
testConversion(6, 0)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>