|
|
View previous topic :: View next topic |
Author |
Message |
Prashant Patel
Joined: 19 Jul 2004 Posts: 33
|
Multiplication of two float number |
Posted: Mon Sep 27, 2004 3:46 pm |
|
|
Hi...
Can anybody give answer of my following question
regarding multiplication of two float numbers?
(1)
What happen when we multiply two float number.
As float is of 32bit. What can be the result.
(2)
If we cast the result of two float number in to
singed int, does it give correct result if we
expect the result between -127 to +127.
(3)
Can we declare the float number of 8bit ot 16bit..??
(4)
Can we declare the float number with only positive
values. Thus increasing the range on positive side..??
Thanks...Hope you guys will clear it..??
Regards
Prashant |
|
|
Steve H Guest
|
|
Posted: Mon Sep 27, 2004 9:44 pm |
|
|
Floats are good for really small problems where you need a quick coding job. To do real work with known precision fixed point is a much better idea - but it takes longer to code (for more info search - fixed point).
(1)
What happen when we multiply two float number.
As float is of 32bit. What can be the result.
A large number with the same precision of # of digits.
(2)
If we cast the result of two float number in to
singed int, does it give correct result if we
expect the result between -127 to +127.
Instead of doing this I would work with fixed point - much faster, more accurate and smaller. All you have to do is to keep track of the decimal point yourself. You will probably not be happy with the limited precision of the floats anyway when casting down.
(3)
Can we declare the float number of 8bit ot 16bit..??
No (NIMV - Not In My Version :-)
(4)
Can we declare the float number with only positive
values. Thus increasing the range on positive side..??
No - not here or in any 'standard' C.
Steve H. |
|
|
Ttelmah Guest
|
Re: Multiplication of two float number |
Posted: Tue Sep 28, 2004 2:44 am |
|
|
Prashant Patel wrote: | Hi...
Can anybody give answer of my following question
regarding multiplication of two float numbers?
(1)
What happen when we multiply two float number.
As float is of 32bit. What can be the result.
(2)
If we cast the result of two float number in to
singed int, does it give correct result if we
expect the result between -127 to +127.
(3)
Can we declare the float number of 8bit ot 16bit..??
(4)
Can we declare the float number with only positive
values. Thus increasing the range on positive side..??
Thanks...Hope you guys will clear it..??
Regards
Prashant |
A float', is not 32 bits!.
It uses 32bits of storage, but only 23 of these store the actual number, the rest store the exponent, and signs (for both the number, and the exponent). Hence the basic 'accuracy' of a float, is limited to about 6 and a three quarter digits in decimal (1in 2^23).
Now the problem is that if (for instance), you could declare a 16bit 'float', if you wanted to keep the same possible 'range' of numbers, there would only be 7bits available for the numeric value. Hence larger/smaller floats, require making decisions about the supported numeric range, and completely redesigning the format to suit. This results in a lot of code, and hence is not available on the PIC with the CCS compiler. In most cases, the 4byte float, is considered to be the smallest that is worthwhile, since the accuracy available is so low on smaller types.
Now a float, is a _scaled_ binary value, of just 23bits. When you multiply the values, the accuracy remains the same (just 23bits), but the scale will move.
You can 'scale' a float, by applying an offset to the number, but since the basic arithmetic package only handles the default format during the actual calculations, there is no increase in the accuracy available on one side of the zero point.
For many operation, a float is not the best solution. If (for instance), you are working with numbers representing currency, and use a float, the rounding errors, can result in pence/cents being lost. Instead, use a 32bit integer, and scale it, so that it represents a count in pence/cents. Hence a value of '1000', would represent 10.00. Then up to a value of over 21billion dollars/pounds, the arithmetic would be accurate. Integer arithmetic is also much simpler to code (and as a result faster), resulting in better behaviour for many such applications.
Best Wishes |
|
|
Prashant Patel
Joined: 19 Jul 2004 Posts: 33
|
Thanks... |
Posted: Tue Sep 28, 2004 2:44 pm |
|
|
Excellent ...Thanks.... |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|