I’m using the Cerbuino Bee board and notice errors in the value of variables that use double precision numbers.

Example:

double[] MY_VAR = new double[2] {1.0, 1.1};

Debug.Print("MY_VAR[0] = " + MY_VAR[0]);

Debug.Print("MY_VAR[1] = " + MY_VAR[1]);

In Debug window:

MY_VAR[0] = 1

MY_VAR[1] = 1.1000000000000001

Why is there an error with the value of MY_VAR[1] and how do I get rid of it? This small error becomes significant after many iterations in a for loop, for example.

Thx

That’s how floating point numbers work on computers. You can’t do anything about it…

I thought you could use the decimal type In full dot net. Not sure if it’s implimented in netMF

You could check if the error is part of the debug.print conversion.

How do I do that?

Do many calculations and then debug.print the result.

I placed MY_VAR[1]=1.1 in a for loop and added 0.1 to it 10000 times and printed the answer:

1001.1000000001591

I must be doing something wrong.

How do I get rid of this error?

Thanks

I think that you did not made any error. The result is, as Simon mentioned, part of implementation in netmf. Have you tried Hagsters suggestion?

@ tdcooper33 - basically not every decimal fraction has an exact equivilent in floating point types. There is eventually a point where you get some quantization error. Also, as the number to the left of the decimal point gets bigger there is less room to represent the fractional component.

There are plenty of ways around this if it causes problems. You could multiply all your numbers by 10 for example.

The .net “decimal” type has more precision, but it is not available in micro.net unfortunately.

As Simon mentioned, this is just how computers work.

If you open windows calculator, and take the square root of 4, you get 2. If you subtract 2 from that result, you don’t get 0, you get the result below.

It’s just a limitation of floating point implementation. the “decimal” type uses 128 bits, so it is accurate out to 28 or 29 decimal places. Floating point uses 32 bits as is only good for 7-8 decimal places. Double (thus the name) uses 64 bits and is good to around 15-16 digits.

Your “double” value started showing errors at 16 digits, which is expected with double.

the MF does not support decimal.

MF supports 32 bit (Float) and 64 bit (Double) floating point. The same as full .net. There is nothing wrong or different with the MF implementation. Google floating point numbers, and you will find explanations of. what you are seeing.

Alright guys thanks. It’s just that this error accumulates over time and causes issues in the main program. At least it doesn’t appear to be random in this case so maybe it can be accounted for. Thanks again.