absolute value?

4 replies [Last post]
AdamF
Title: MiniBot+
Joined: 03/17/2010
Posts:
BotPoints: 62
User offline. Last seen 9 years 19 weeks ago.

Can anyone confirm that the absolute value function is broken on the CBC?

I was re-purposing some old code and wondered why someone had written their own function to calculate an absolute value, so I replaced it with abs(num) which unfortunately caused our program to malfunction. In the process of fixing it I found a way to not have to use an absolute value but I am still curious as to why it doesn't work correctly. Does this mean that the whole math library is broken? (i.e. trig functions/etc)

Adam Farabaugh
Hampton High School

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

I don't have a CBC on me right now to test with, but I'm fairly sure I've used abs() with no issues. What firmware are you using? NHS Patchset or official? Modded in any other way e.g. CBCJVM or CBCLua? Any test code you can provide?

-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

AdamF
Title: MiniBot+
Joined: 03/17/2010
Posts:
BotPoints: 62
User offline. Last seen 9 years 19 weeks ago.

Sorry for the delay in replying.
I believe I was using the NHS patchset at the time I had this specific issue (the compiled binary). But I do now recall having the same issue last year with the standard KISS firmware, which would explain why someone wrote a new function that returns the magnitude of a floating point variable. The syntax I tried was like

float c=-1.1;
float a=abs(c);
printf("%f",a);

which returned something other than 1.1.

Like I said it's not a critical issue because I discovered a more efficient way to calculate wheel distances for turning without any function calls, but it is in interesting bug nonetheless. Maybe the math.h abs() function is not typed for float? I know C doesn't allow overloading but you would think they would have typed it for floating-point vars.

Wait nevermind b/c that would result in a compiler error...

Adam Farabaugh
Hampton High School

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

Hey Adam, sorry for the delayed reply (giving a talk at a conference seems to cause me to have no free time for the week or so beforehand).

In C++, abs() is defined for both integers and floating-points. In C, I think abs() is only defined for ints. In C, if you want the absolute value of a float, use the fabs() function (which is also available in C++). I'm not entirely sure why you didn't get a compile error though.

Anyway, try fabs() and see if that helps. It's in math.h. Let me know if you have any luck.

-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

wmyers
wmyers's picture
Title: Advisor
Joined: 02/24/2011
Posts:
BotPoints: 54
User offline. Last seen 8 years 28 weeks ago.

A compiler error should've occurred if you tried to use the abs() with a floating point value. Use fabs().

If all else fails, just write your own. :)

Wesley Myers
--
YAC Advisor
Botball International Champion 2007 and 2008

Carnegie Mellon University 2012
Electrical and Computer Engineering
Computer Science and Robotics