psgarcha92:
@HazardMind, thanks alot for that answer.
Guys, you would not believe what i found.
when you use the calculator in Windows, and divide 22 by 7
22/7=3.1428571428571428
When you use python and do the same,
22.0/7.0=3.142857142857143
When you do 22.0/7.0 on an Arduino mega:
22.0/7.0=3.142857074737548
On a Due,
22.0/7.0=3.142857074737549
On a Stellaris Launchpad,
22.0/7.0=3.142857074737548
Why are these values different than what the calculator and Python3 calculates? why these errors? Why are the Boards giving values so different?
Regards
Windows likely does the calculation in double precision, which has 53 binary bits of precision in the mantissa. On my Linux system, in double precision, it prints out: 3.1428571429. In general, with double precision, you will get 15-16 decimal digits of precision, and the exponent can go up to 1e307.
If you declare the variables float, it will do it in single precision, which has 24 binary bits of precision. On my Linux system, in single precision, it prints out: 3.1428570747. In general with single precision, you will 7-8 decimal digits of precision, and the exponent range can go up to 1e38.
Now, if you had declared the variables 'double', on the Arduino, it would still print out 3.1428570747, because the Arduino maps double into float. If you used double on the Due, it should print out 3.1428571429.
The difference in the last digit, is likely due to the way the library converts floating point to textual reresentations.
If you used a calculator program or standalone calculator, it may have done the calculations in base 10, rather than doing it in base 2, and converting to base 10 for output as computers do.
Some computers have decimal floating point arithmetic as part of the basic instructions (you have to use _Decimal32, _Decimal64, and _Decimal128 to get decimal floating point types instead of binary floating point type.). Some others have decimal floating point as an software only implementation. I would imagine most embedded chips do not have the decimal support in the library. FWIW, both the Due and the AVR chips do not have hardware floating point on the chip, so when you do calculations in floating point, it takes hundreds to thousands of instructions to do the floating point operation.
If you want to read up on the binary formats used in computers, that is defined in IEEE 754 (and 854). For a summary, go to IEEE 754 - Wikipedia.