0

I was doing the questions in http://javascript-puzzlers.herokuapp.com/. For the 8th problem,

What is the result of this expression? (or multiple ones)
          
var two   = 0.2
var one   = 0.1
var eight = 0.8
var six   = 0.6
[two - one == one, eight - six == two]

The answer is [true, false]. After reading the explanation that JavaScript does not have precision math even though sometimes it works correctly, I feel the answer should be random. Not a standard answer.

Can anyone help and give more detailed explanation about the answer? Thx in advance!

Update

This explains Why 0.1 + 0.1 == 0.2?

Jonas C
  • 1
  • 3
  • Hardly any language has "precision math" – Bergi Nov 01 '20 at 16:16
  • This can not explain my question. I think the answer should be random not a standard one. – Jonas C Feb 12 '21 at 10:57
  • It should be reopened. The provided link does not help explain why the it is a standard answer. https://stackoverflow.com/questions/588004/is-floating-point-math-broken – Jonas C Feb 12 '21 at 11:00
  • Why do you think it should be random? Computers are deterministic. The linked topic explains how JS follows the IEEE 754 standard, which specifies how bit patterns are representing numbers and how they are manipulated to represent subtraction. – Bergi Feb 12 '21 at 12:00

0 Answers0