Hi,
I have a code to convert Integers to Binary in order to send it with 433MHz later.
My number is 70110001.
When I devide it by 2 it gives me 35055000.00, which is wrong. It should result 35055000.50
If I divide 7011001 by 2 it gives me the right result.
Is the number to long for float? I can´t figure out what I am doing wrong.
I test the devision with this:
Serial.println((float)number / 2.0);
Floating point can only represent a finite number of significant digits. The mantissa of a 'float' is 23 bits plus the implicit high-order 1. To represent 70110001 in binary requires 26 bits which won't all fit in a 'float' and you lose 2 of the low order bits.
Representing 7011001 in binary requires 23 bits and will therefore fit.
The division by two has no effect since this only changes the value of the exponent.
For reasons mentioned earlier, the number 70110001 may or may not be accurate represented as a float. Because only 4 bytes are used for floating point numbers, the precision of a floating point number is about 7 digits. Anything after the first 7 digits is the computer's best guess as to the actual number. This is one area where the C supported by the Arduino IDE differs from a "full" (ANSII) C compiler, in that the IDE does not support a true double data type, which is 8 bytes. If it did, you could probably get 15 digits of precision.
Also, next time when you post your code, please place the code in code tags, as explained in Nick Gammon's post on using this Forum, which are found at the top of this Forum.
My number is 70110001.
When I devide it by 2 it gives me 35055000.00, which is wrong. It should result 35055000.50
O…kay.
Floating point numbers should always be treated as being an approximation of some real number. If you need things to be calculated exactly to the digit, then use an integer.
Integers, of course, don't handle fractional results. The fix for this is to handle your quantities as multiples of the smallest fraction you will need to deal with. When handling currency, for instance, store amounts as an integer quantity of cents. When dealing with time, store amounts as a number of minutes or even a number of seconds.
Other languages provide fixed-point aritmetic, eg the sql NUMBER(10,2) type. C doesn't
Floating point numbers should always be treated as being an approximation of some real number. If you need things to be calculated exactly to the digit, then use an integer.
Other languages provide fixed-point aritmetic, eg the sql NUMBER(10,2) type. C doesn't
Not to mention the Precision of that big number...
shift-expression >> additive-expression*
...
The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a nonnegative value, the value of the result is the integral part of the quotient of E1 / 2E2. If E1 has a signed type and a negative value, the resulting value is implementation-defined.
The results of some bitwise operations on signed integers (C90 6.3, C99 and C11 6.5).
Bitwise operators act on the representation of the value including both the sign and value bits, where the sign bit is considered immediately above the highest-value value bit. Signed ‘>>’ acts on negative numbers by sign extension.
Thank you all for help. Thank you.
It looks like that it is not possible to divide my number (70110001) and have a float data type because the float number would be too big for this data type. A pitty.
Hence this restriction I will have to make the number one digit smaller.
naitsimp:
It looks like that it is not possible to divide my number (70110001)
and have a float data type because the float number would be too big for this data type.
No. Floats can be much larger, but 4 byte floats 'only' have six significant digits.
If you care for exact numbers use an integral type.
If you need fractional parts, you can interpret it as 1/10, 1/100 or 1/1000.
Your number 70 110 001 fits perfectly into a long, which gives you 9 full digits.
I am converting a Number to Binary and send this binary with a 433Mhz Module.
IN order to convert it I need to know if the division has a Remainder or not.
I calculate it like in the first table on this site: Decimal to Binary Converter
If Remainder -> 1
If no Remainder -> 0
naitsimp:
I am converting a Number to Binary and send this binary with a 433Mhz Module.
IN order to convert it I need to know if the division has a Remainder or not.
I calculate it like in the first table on this site: Decimal to Binary Converter
If Remainder -> 1
If no Remainder -> 0