General floating-point maths query
问题 Okay so I get that some numbers can't be represented properly in binary just like 1/3 can't be fully represented in decimal. So how come when I console.log(0.3) it returns 0.3 but when I console.log(0.1 + 0.2) it returns the 0.30000000000000004 How come it is accounting for the error (if it even is) when simply outputting 0.3 but doesn't when the addition occurs? 回答1: Suppose we approximate 1/3 and 2/3 in decimal. 1/3 = 0.333 2/3 = 0.667 and we add 1/3+1/3: 1/3+1/3 = 0.333 + 0.333 = 0.666 We