How to catch when a float = -1.#IO

678 views Asked by At

I have a problem in my RobotC code where when a float reaches infinity it returns -1.#IO

This is the value that is returned if a float reaches -Infinity.

So the problem is float can only use numerical values. I cannot catch this value.

If I put

if (value == -1.#IO) { ... }

the compiler says unexpected #

If I put

if (value == "-1.#IO") { ... }

the compiler says char string constant '"-1.#IO"' cannot be compared with value. This is obvious because it is trying to compare a string with a float

Now my formula calculates a range of values in which both negative and positive infinity can sometimes exist.

So I need to find a way to catch this value when it pops up so I can replace it with a numerical float value (which in this case will be 0).

float my_Trig_LawOfSin_2Sides1Angle(float angleA, float sideA, float sideB) //SideA must be opposite AngleA
{
    //Catch the divide by 0 on this first line and then return sideA+sideB;
    if (angleA == 0) {
        return sideA + sideB; //this is to avoid the divide by 0 error 
                              //when the bot is looking straight.
                              //It will return the distance of the
    }
    float angleB = (asin(sideB * sin(angleA * (pi / 180)) / sideA)) * (180 / pi);
    if (angleB == "-1.#IO") { return 0; }

    float angleC = 180 - (angleA + angleB);
    float sideC = sideA * sin(angleC * (pi / 180)) / sin(AngleA * (pi / 180));
    return sideC;
}

task main()
{
    result = my_Trig_LawOfSin_2Sides1Angle(50, 200, 300);
}
2

There are 2 answers

0
chqrlie On

If you were programming in C, you could use the macros defined in <math.h> to test for infinite or NaN values:

  • int isinf(f) returns non zero if f is an infinite value, positive of negative.
  • int isnan(f) returns non zero if f is a NaN value. NaN values (not a number) are produced when an expression does not have a defined value: pow(0.0, 0.0), 0.0 / 0.0...
  • isfinite(f) returns non zero is f is neither an infinite nor a nan value.

Your environment uses a C-like dialect that may or may not support these macros, if it does not, you could test for infinities with this simple work around:

if (1.0 / value == 0) {
    /* value is an infinity */
}
7
skyline On

I found out if I convert the float to a string it converts -1#IO to -1.#IND00

So I decided to catch that insted and it worked.

...
    float angleB = (asin(sideB*sin(angleA*(pi/180))/sideA))*(180/pi);

    string infinityCatch = angleB;
    if (infinityCatch == "-1.#IND00"){return 0;}
...