tempco function is getting 3-rd parameter as float Its being used in other functions which variables are floats. only calibration and only in 1 function is unsigned long that's why not to change all functions I want to squeeze it inside tempco.
Delta_G:
If your function wants a float and you have a long you don't need to cast anything. The compiler is smart enough to do that one for you.
I checked both ways and both ways are working I`m getting right values with both ways. I want to know if the way I wrote is considered as right way? There must be kinda recommended and not recommended ways I think
surepic:
tempco function is getting 3-rd parameter as float Its being used in other functions which variables are floats. only calibration and only in 1 function is unsigned long that's why not to change all functions I want to squeeze it inside tempco.
Thats not gonna work in my case. Too many functions are sending floats to tempco and its dealing with them but 1 function is using int instead of float so i am casting int to float. Passing by reference how will work if int has twice smaller byte width than float? In your example tempco is expecting address of uint not float.
Delta_G:
Once again, if the function expects float and you have int then you don't need any cast. The compiler will handle the promotion for you. Just pass the int in there and the cast to float happens automagically.
That would be useful if the two different functions did different things. If the only reason for the int vs. float variant is to not require an implicit or explicit cast, duplicating code is not the right thing to do.
//--------------------------------------------------------------------------------------------
In Case:A whether we cast or not, there are correct results.
In Case:B, we must cast to get the correct result.
So, I follow what my teacher has advised me -- always do the cast (follow the convention), and you are safe! (Casting forces the machine to come out of confusion and to produce the expected data type; declaration of correct data type for the holding variable is not enough.)
Delta_G:
No cast needed. Just need to make sure your first constant is a float.
Only true if the expression evaluation order does not screw up things
float ampT = (float) (10000100)(1.1/1024)*0xE9;
Delta_G:
No reason to start with it as an int and cast it when it could be a float from the get-go.
Partly agree, most of the time this makes sense but it depends on platform and application requirements
Significant digits
The Arduino (UNO) float has only 6-7 significant digits while an int32 has 9-10, and int64 has even 18-19.
The value of these extra digits depends on the application.
Performance
The Arduino (UNO) float math is much slower than int8 or int16 math, int32 I'm not sure, int64 is slower than float. So performance wise you can gain a lot by doing part of the math in int. But that only matters if performance is a must.
Sketch size
If you can do your whole sketch in int math, you will not need the float library which saves a few KB. That can be important when your sketch approaches the limits of FLASH e.g. you have a lot of text in PROGMEM or just plain code.
If sketch size becomes a problem it is time to consider another platform.
RAM
Never investigated if doing math in float takes more heap/stack space, my asumption is it will.
If RAM becomes a problem it is time to consider another platform.
@robtillart agree with most of the points and crossed with problem you showed in example about casting few month ago thats why im always casting if it will not harm just to be sure i gonna get expected behaviour.
surepic: @robtillart agree with most of the points and crossed with problem you showed in example about casting few month ago thats why im always casting if it will not harm just to be sure i gonna get expected behaviour.
but then, you have to know that the function expects you to type cast.
you can handle it using the operator precedence as pointed out or doing the cast inside the function.
something like this:
template <class T> void tempco(float a, float b, T &c){
float d = sqrt(sq(a * b * (float)c )) * 0.034565;
c = d * sq(a/b);
}
void setup()
{
Serial.begin(9600);
uint16_t someInt = 4;
float someFloat = 4.0;
tempco(123.0, 789.0, someInt);
tempco(123.0, 789.0, someFloat);
Serial.println(someInt);
Serial.println(someFloat);
}
void loop() {
// put your main code here, to run repeatedly:
}