JS math.ceil | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

JS math.ceil

Hello, why the code below returns 218, not 217? Math.ceil(195.3/(75/1000)*12))

21st Jun 2023, 6:03 AM
Kamil Stenka
Kamil Stenka - avatar
2 Answers
+ 4
The result of your formula is 31249. You can understand it better if you print the intermediary results. In Javascript a division gives you a floating point number. And calculating with floats, will always be a little imprecise, because they cannot always be correctly represented in a finite number of bits in memory. Math.ceil will round up the result to the next integer, even when the fractional part is just a tiny, tiny number. https://code.sololearn.com/c1usD4kx4rbT/?ref=app
21st Jun 2023, 7:22 AM
Tibor Santa
Tibor Santa - avatar
+ 2
Thanks Tibor for answer. I missed one bracket 195.3/((75/1000)*12). However, from mathematical point of view x1 and x11 in my code supposed to be equal… https://code.sololearn.com/czOh5LQKO5xe/?ref=app I need to take that imprecision into account.
21st Jun 2023, 8:13 AM
Kamil Stenka
Kamil Stenka - avatar