Floats acting silly

1 reply [Last post]
acetech09's picture
Title: NooBot+
Joined: 12/08/2009
BotPoints: 15
User offline. Last seen 10 years 12 weeks ago.

This one's got me stumped:

  1. #define ETtolerance 370
  3. float i;
  4. i = ETtolerance/analog10(7);
  5. printf("%f \n",i);

I loop the printf and math part, and move my hand closer and farther from the ET in port 7. But it only either prints 0.0000 or 1.0000. It seems to be rounding, but that is a float value... should it be doing that? I'm trying to get a decimal ratio of real distance to set distance but I can't do it with just a 1 and a 0.

Is there something stupidly obvious with the code that I'm missing, or is it just a limitation in C that division won't yield decimal floats?

- “Today's scientists have substituted mathematics for experiments, and they wander off through equation after equation, and eventually build a structure which has no relation to reality.” - Nikola Tesla

He means we should have more practice tables.

Jeremy Rand
Jeremy Rand's picture
Title: Botball Youth Advisory Council
Joined: 04/03/2009
BotPoints: 1168
User offline. Last seen 7 years 18 weeks ago.

Dividing two ints will always yield an int. You can either make it 370.0, or typecast the dividend or divisor to a float.

-Jeremy Rand
Senior Programmer, Team SNARC (2012-2013), Norman Advanced (2010-2011), Norman HS (2008-2009), Norman North (2005-2007), Whittier MS (2003-2004)
2012-2013 VP of Tech, 2011 President, Botball YAC (2009-2013)
Mentor, Alcott and Whittier MS