3

I know why 0.1 + 0.2 !== 0.3, because 0.1 cannot be represented exactly in a binary floating point representation, but why 0.1 + 0.3 === 0.4 in JavaScript? I think 0.1, 0.3 both cannot be represented exactly, why 0.1 + 0.3 will exactly equal 0.4? and I test this in C,I got 0.40000000000000002, this result is what I want, here is my C code

#include "stdio.h"

int main(){

double a = 0.1 + 0.3;
printf( "%.17lf", a );

}

thank you every much!

PerterPon
  • 105
  • 1
  • 9
  • What does your C code print for `double a = 0.4` ? – Thilo Nov 24 '16 at 09:40
  • Possible duplicate of [While 3 \* 0.1 cannot be represented exactly, why can Javascript properly calculate 0.15 \* 2?](http://stackoverflow.com/questions/14251052/while-3-0-1-cannot-be-represented-exactly-why-can-javascript-properly-calcula) – JJJ Nov 24 '16 at 09:43
  • Possible duplicate of [Why does adding 0.1 multiple times remain lossless?](http://stackoverflow.com/questions/26120311/why-does-adding-0-1-multiple-times-remain-lossless) – Thilo Nov 24 '16 at 09:47
  • 1
    `double a = 0.4` in C print `0.40000000000000002 ` too, e... – PerterPon Nov 24 '16 at 10:02

0 Answers0