I am running an experiment using an Arduino that requires several million calculations per second to be performed by an Arduino. I was wondering the odds (if any) that one of these calculations is wrong, and how I could prevent such an error?
-
3\$\begingroup\$ Same way the space shuttle does it. \$\endgroup\$– Ignacio Vazquez-AbramsSep 13, 2013 at 23:55
-
\$\begingroup\$ Several million? At one million calculations, you have 16 instruction cycles to get the job done. I don't know what your calculations are, but I'm betting that isn't going to be enough. \$\endgroup\$– Matt YoungSep 14, 2013 at 0:09
-
1\$\begingroup\$ I don't know how big chance for a mistake in an AVR is, but if you're worried about results and need fast calculations, take a look at Hercules Launchpads. It's a cheap demoboard for a microcontroller used for safety-critical operations, so it should be much better than just an Arduino. It's much faster than AVRs used in Arduino (>6 times), but the board layout is not compatible, so you won't be able to use Arduino shields directly. You'll either need an adapter PCB or you'll need jumper wires. \$\endgroup\$– AndrejaKoSep 14, 2013 at 0:26
-
14\$\begingroup\$ The error probability is either <0.00001% (e.g. cosmic ray impingement) or 100%, depending on whether you have a error in your code. \$\endgroup\$– Connor WolfSep 14, 2013 at 2:15
-
\$\begingroup\$ The "quality" of the µC's work is probably more dependent on environmental effects than anything else: Transients in power, data, or clock lines can cause erroneous instruction execution, a cell phone nearby may be enough to boost the probability of failure by many orders of magnitude. \$\endgroup\$– JimmyBSep 16, 2013 at 13:43
4 Answers
I don't know the odds for this to occur, and I suspect them to be incredibly low. However, if you're really serious about trying to detect such errors, here's one simple way you could: Perform the exact same calculation twice storing the results independently. If all has gone well, both results will be identical. So if you compare them and they differ, you know something went wrong, and you could repeat the calculations until the results match. While it's theoretically still possible that you miss errors, the odds have now been reduced even further. The downside is of course that you at least double the number of calculations and you have to do additional comparisons, both of which take up further CPU cycles. There are certainly other more elegant solutions.
-
1\$\begingroup\$ I know this is probably obvious but as a compromise between calculation overhead and security it may be an option to just check if the result is plausible. For example if we expect a result between 0.0 and 100.0 (maybe calculating percentage) we could check if the result is within this range before continuing processing. \$\endgroup\$– RevSep 14, 2013 at 10:06
Miscalculation? Practically none. That does depend on three things though.
Accurate Code. If your code is bugged, your results will be useless. Not the Arduino's fault. Also code that doesn't get weirdly optimized by the compiler. A good understanding of the hidden Arduino Libraries you use would also be important.
Efficient Code. At the Arduino's stated max, 16mhz, that leaves only 16 ticks of the clock to do a single calculation. You need fast, accurate code that fits in those 16 calculations. Probably not possible in Arduino code with all its overhead. Maybe not even possible in pure C depending on what kind of calculations you need. Get ready to learn assembly.
Type of calculations. The ATMega328 on an Arduino Uno (the ""Standard"" Arduino) has no Floating Point Unit. Any math that requires fractions or decimal results, will be code heavy, and not precise. You will loose/gain some fraction of a digit on every floating point operation. Stick to fixed point and you will have almost no chance of miscalculations.
-
\$\begingroup\$ If the task is purely to do calculations, that should not normally require use of Arduino libraries, at least for the time critical computation cycle. Thus, Arduino library overhead isn't a given, especially if one turns off the millisecond timer tick or better yet, all interrupts. \$\endgroup\$ Sep 14, 2013 at 15:05
-
\$\begingroup\$ @AnindoGhosh I assume the calculations are based on some outside input, adc, sensor, etc, not just pure untouched standard C math.h operations. \$\endgroup\$– PasserbySep 14, 2013 at 15:09
-
\$\begingroup\$ Fair enough... Your assumption trumps my assumption :-) \$\endgroup\$ Sep 14, 2013 at 15:20
I am running an experiment using an Arduino that requires several million calculations per second to be performed by an Arduino. I was wondering the odds (if any) that one of these calculations is wrong, and how I could prevent such an error?
A very bad question in it's nature...
If your understanding of 'wrong' calculation is the one, in which the arduino, for example, insted of performing '+' perfroms a '-' operation, then there is no chance that it will ever occur. The operations to be perform are 'nested' in the opcode of each instruction, i.e. you have to have a faulty chip (very unlikely).
The calculations themselves can also vary in terms of complexity. For example a simple addition/substraction (+/-) or bitwise functions (and, nor, xor...) are performed in hardware, meaning the chance of error inside these calculations is very small (nearly impossible).
However, as mentioned by other users above, arduino takes the levels of programming a few steps higher, by allowing you to directly use floating point operations, or operations such as multiplication/division/power of/ etc. These functions are not implemented in hardware, but are rather performed by many, less complex functions. For example, multiplication of A * B is the same as A + A + A... B times... These complex functions are generally iterative (performed in numbered iterations) and take much longer to execute.
Another thing is the precision of floating point units. It depends of the orgarnization of bits (width) or whether you are using exponential for of representation and so on.
So to make my point clear: The arduino can't go nuts, unless you tell it to do so. It will not use multiplication instead of division, or perform the correct operation on wrong numbers, unless you program it to do so. And performing millions of calculations per second is a tough business even for much faster and more powerful chips.
Essentially zero. If computers weren't reliable for making calculations, we wouldn't use 'em.
-
\$\begingroup\$ Computers in general are reliable, but there's no information confirming that AVR is highly reliable. There are special types of computers used for high reliability and Arduino is not one of them. \$\endgroup\$ Sep 14, 2013 at 0:55
-
\$\begingroup\$ For most practical purposes, the chance of an Arduino making a miscalculation is zero. If the OP needed a high reliability computer his application, he'd know that and probably wouldn't be asking here. \$\endgroup\$ Sep 14, 2013 at 0:59
-
5\$\begingroup\$ But we don't actually know what OP want or needs or thinks he needs or if OP's purposes are practical, so that assumption is in general dangerous. \$\endgroup\$ Sep 14, 2013 at 1:03