0

sorry for the nooby question but I can't seem to find the answer to this anywhere, probably because I'm unsure what to search for with this problem!

I'm just wondering what the reason is for including a data type in brackets before a calculation takes place? In the code below, all the variables have already been declared at the top of the program, so I'm unsure what the reason / effect of including the (float) has. acc_y and angle_pitch_acc have been declared as floats and acc_total_vector has been declared as long

angle_pitch_acc = asin((float)acc_y/acc_total_vector)* 57.296;

Thanks!

2

It's called casting. It changes the data type temporarily. In this instance it's being used to change an integer type into a floating point type, which means that the whole calculation is done using floating point mathematics. However acc_y is supposedly already a float, but maybe it wasn't at the time the author wrote that line. Maybe things, as they often do in a program, evolved. Only the author knows.

3
  • The OP wrote, that acc_y is declared as float. So the calculation would already be a floating point one, I think – chrisl Feb 13 at 20:22
  • @chrisl I stopped reading at "I wonder..." – Majenko Feb 13 at 20:28
  • Ok thanks, that makes sense. – Elliot P Feb 13 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.