-1

I don't understand why the values in the following code are not equal to 1:

var a:uint = (4.1-1.7)/2.4;
trace(a);//traces 0

var b:int = (4.1-1.7)/2.4;
trace(b);//traces 0

var c:Number = (4.1-1.7)/2.4;
trace(c);//traces 0.9999999999999998
  • [What every programmer should know about floating point](http://floating-point-gui.de/) – Barmar Jul 20 '13 at 22:14
  • Sorry for the duplication. I did try to search before I posted the question. – Kasper Knudsen Jul 20 '13 at 22:26
  • Adding to Chip's answer, because of the susceptible minor inaccuracies, each of your variables are equal to `0.9999999999999998`, but because the first two are typed as `uint` and `int`, those numbers are truncated (not rounded) down to `0`. – Chunky Chunk Jul 21 '13 at 07:34

1 Answers1

0

This is because in most languages real, non-integer numbers are stored using floating point representation (http://en.wikipedia.org/wiki/Floating_point), which is inherently susceptible to minor inaccuracies.

Chip Camden
  • 210
  • 1
  • 7