here is source code
Code: Select all
float TNUM ;
char TXT[13];
void main()
{
Lcd_Init(&PORTB);
Lcd_Cmd(LCD_CLEAR);
TNUM = 11.0;
FloatToStr(TNUM,TXT);
LCD_Out_CP(TXT);
}
Now after compile the source code
On menu View Assembly
Code: Select all
TNUM = 11.0;
$0383 $3000 MOVLW 0
$0384 $00A3 MOVWF _TNUM
$0385 $3000 MOVLW 0
$0386 $00A4 MOVWF _TNUM+1
$0387 $3030 MOVLW 48
$0388 $00A5 MOVWF _TNUM+2
$0389 $3082 MOVLW 130
$038A $00A6 MOVWF _TNUM+3
Code: Select all
Bit 31 Sign Bit 0:+ ! 1:- = 0 = _TNUM+2.f7
Bits 30 - 23 Exponent Field = _TNUM+3 = 130
Bits 22 – 0 fraction Significand = 1 .01100000000000000000000
Decimal value of the significand = 1.3750000
Let see Example: Converting to IEEE 754 Form:
1. Put 11.0 in single-precision format
2. The first step is to look at the sign of the number.
Because 11.0 is positive, the sign bit =0.
POW(-1, signbit) = 1
3. Write 11.0 in base-2 scientific notation.
This means that we must factor it into a number in the range [1 <= n < 2] and a power of 2.
Code: Select all
11.0 = POW(-1, 0) * (1+fraction) * POW(2,power)
or:
Code: Select all
11.0 / POW(2,power) = (1+fraction).
power = exponent – bias = exponent – 127
So we can divide 11.0 by some power of 2 to get the (1 + fraction).
Code: Select all
11.0 / pow(2,1) = 5.5
11.0 / pow(2,2) = 2.75
11.0 / pow(2,3) = 1.375 range 1<= n <2 ok
11.0 / pow(2,4) = 0.6875
Therefore, 11.0 = 1.375 * 23
4.
Find the exponent.
The power of 2 is 3, and the bias for the single-precision format is 127.
This means that the
In binary, it's 1000 0010 bin
5. Write the fraction in binary form
We know that the number we calculated in step 2 is in the range [1 <= n < 2]. Therefore, we don't have to store the leading 1.
The fraction = 0.375 . Unfortunately, this is not a "pretty" number, like those shown in the book. The best we can do is to approximate this value. Single-precision format allows 23 bits for the fraction.
Binary fractions look like this:
Code: Select all
0.1 = (1/2) =pow(2,-1)
0.01 = (1/4) = pow(2,-2)
0.001 = (1/8) = pow(2,-3)
To 0.375, we can say:
0.375 = pow(2,-2) + pow(2,-3)
0.375 = 0.25 + 0.125
0.375ten = 0.01100000000000000000000bin
The binary string we need is:
floating-point numbers in IEEE 754 format.
6. Now put the binary strings in the correct order -
1 bit for the sign, followed by 8 for the exponent, and 23 for the fraction. The answer is:
Code: Select all
Sign Exponent Fraction
Decimal 0 130 0.375
Binary 0 1000 0010 011 0000 00000000 00000000
011 0000 00000000 00000000
_TNUM+2.F7 _TNUM+3 _TNUM+2 _TNUM+1 _TNUM
130 48 0 0
Now we can say mikroC's IEEE-754 Floating-Point Conversion Single precision (32 bits) working fine
Example: Converting to Float
Convert the following single-precision IEEE 754 number into a floating-point decimal value
Code: Select all
_TNUM+3 _TNUM+2 _TNUM+1 _TNUM
0 130 48 0 0
0 10000010 011 0000 00000000 00000000
1. First, put the bits in three groups.
Code: Select all
Bit 31 (the leftmost bit) shows the sign of the number.
Bits 23-30 (the next 8 bits) are the exponent.
Bits 0-22 (on the right) give the fraction
2. Now, look at the sign bit.
If this bit is a 1, the number is negative.
If it is 0, the number is positive.
This bit is 1, so the number is negative.
3. Get the exponent and the correct bias.
The exponent is simply a positive binary number.
Remember that we will have to subtract a bias from this exponent to find the power of 2. Since this is a single-precision number, the bias is 127.
4. Convert the fraction string into base ten.
This is the trickiest step. The binary string represents a fraction, so conversion is a little different.
Binary fractions look like this:
Code: Select all
0.1 = (1/2) =pow(2,-1)
0.01 = (1/4) = pow(2,-2)
0.001 = (1/8) = pow(2,-3)
So, for this example, we multiply each digit by the corresponding power of 2:
=
Code: Select all
0*pow(2,-1)+ 1*pow(2,-2) + 1*pow(2,-3) + 0*pow(2,-4) = 1/4 + 1/8
=
5. This is all the information we need. We can put these numbers in the expression:
Code: Select all
pow(-1, sign bit ) * (1+fraction) * pow(2 ,exponent – bias)
=
Code: Select all
pow(-1,0) * (1.375) * pow(2 ,130-127)
The answer is 11.0 not approximately 10.99999
What we can say why floatToStr don’t obtain 11 instead of 10.99999
Help me please