|
|
View previous topic :: View next topic |
Author |
Message |
Gordon23
Joined: 17 Feb 2024 Posts: 6 Location: UK
|
Floats & Ints |
Posted: Sun Mar 03, 2024 3:59 am |
|
|
Hi,
I am struggling to get to grips with Ints and float when doing calcualtions and then printf's.
I have:- (just an example)
unsigned int8 rData[6] = {0};
unsigned int16 tcomb;
tcomb = rdata[0] * 256 + rdata[1];
tcalc1=175*(tcomb/65535);
printf("%Lu\r",tcomb);
printf("%Lu\r",tcalc1);
This does not work, just 0.00. If i change tcomb to:-
float tcomb;
printf("%.2f\r",tcalc1);
Then i get what appears to be good data. My question is, does this look the best way to get the result from tcalc1? I find if difficult to understand "Casting".
I need to do more to the calculation, the result will then end up needing to being a signed float, as:-
float finalresult;
finalresult= -45+tcalc1;
printf("%3.2f\r",finalresult);
Am i doing this correctly?
Thanks, |
|
|
Jerson
Joined: 31 Jul 2009 Posts: 125 Location: Bombay, India
|
|
Posted: Sun Mar 03, 2024 7:23 am |
|
|
The value in tcomb will range from 0 till 65535.
Thus, using the native integer math of the processor, tcomb/65535 will result either in a 0 for values less than 65535 or 1 when it is equal. Fractions have no significance in integer math.
So, 175*(tcomb/65535) is going to yield either 0 or 175
if you typecast the operation, you will do it this way
tcalc = 175 * ( (float) tcomb / 65535 )
This forces the value in tcomb to be changed to a floating point number, then divided and later multiplied. This will give a proper mathematical result.
The usual way would be to declare tcomb, tcalc1 as floats and perform all calculations.
If you are optimizing use of code space, other techniques can be used, but, depends on what is acceptable to your application. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Sun Mar 03, 2024 10:25 am |
|
|
Some comments:
First, you display a common error on your format numbers for the
printf. In C, the number in front of the decimal point in the format, is the
'total field width'. This is all the characters to output. Includes thiings
like the decimal point. Now your number looks as if it'll have at least
a couple of digits in front of the decimal point, so %5.2f, which supports
numbers like xx.xx - five characters, two after the decimal.
Now on your original calculation:
tcalc1=175*(tcomb/65535);
Why the brackets????. You are asking it to solve tcomb/65535, and
then multiply the result by 175. The division would be done using
int16 maths, since everything inside the bracket is no larger than an
int16. Result therefore is 0/1, then multipled by 175.
If instead you removed the brackets and did the first multiply with
int32 arithmetic, and only the final division in float, this is quicker than
using float throughout, and will give the same result. So:
tcalc1=175LL*tcomb/65535.0;
Now this makes the first multiply use int32 arithmetic (by declaring
175 as being a 'long long', which is an int32 type), and then performs
a floating point division by 65535 (be declaring the divisor here as
65535.0), which is a floating point value.
Key is to get your head around that the maths type used on each sum,
is the type of the largest/most complex item involved. So:
int8*int8 -> int8 maths
int16*int8 -> int16 maths
int16*int32 -> int32 maths
int32*float -> float maths
etc for other types and operations
Now a 'cast', allows you to simply tell the compiler to treat a value
as being of a different type. So
int8val -> treated as int8
(int16)int8val -> treated as int16
(float)int8val -> treated as a float value.
etc. for other types.
As I show, you can also set a type for a value using'key letters and
formats.Any numeric constand containing a decimal point in a floating
point value. A number over 255, or followed by 'L' is an int16. A number
over 65535, or followed by 'LL' is an int32. |
|
|
Gordon23
Joined: 17 Feb 2024 Posts: 6 Location: UK
|
|
Posted: Sun Mar 03, 2024 12:46 pm |
|
|
Jerson & Ttelman,
Makes sense now - thank you for taking the time to explain so well.
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
|