-3

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);
}
Bob Gilmore
  • 12,608
  • 13
  • 46
  • 53
skyline
  • 51
  • 9
  • Read [ask] and provide a [mcve]. It is absolutely not clear what you are asking or what your problem is - unless it is the C language. For the latter, there are good books teaching the language. – too honest for this site Feb 21 '17 at 00:10
  • I thought It was clear and I was trying to Use as little code as possable. Hence why i said If you want my formuler I can add it. In my past posts people told me not to post the code and onley post what is nessacery aand that is what I did – skyline Feb 21 '17 at 00:15
  • There are very clear site-rules. Read the links. – too honest for this site Feb 21 '17 at 00:22
  • It is not clear what`-1.#IO` is supposed to be. You seem to think this is a common ideom - it is not. You might want to read about floating point constants in C. – too honest for this site Feb 21 '17 at 00:29
  • 1
    1) I know its not a common idiom I Thought that was obvious when i said thare are not many posts on it Thats why i said what it was. 2) I said -1.#IO is the value given when a floating point reaches infinity How is that not clear? – skyline Feb 21 '17 at 01:49
  • Give - when? By which function? Where? Please provide a reference to the C standard where this character sequence is specified or even used. – too honest for this site Feb 21 '17 at 11:29
  • @skyline: you can accept one of the answers by clicking on the grey checkmark below its score. – chqrlie Feb 21 '17 at 22:49
  • 1
    @Olaf First I never stated that it was part of the C stranded. I only said that I was using C code. second It it is a floating point exception as stated in the IEEE Standard for Floating Point Operations documentation as you can find out more about that on this post here http://stackoverflow.com/questions/35997187/what-leads-to-the-floating-point-exception-1-io It was that post that that gave me the information to let me to say it is given when a floating point reaches infinity. I just wanted to know if there was a way to catch it. And yes there is. I posted how to below. – skyline Feb 22 '17 at 23:54
  • 1) "I only said that I was using C code" - The C language is standardised in ISO/IEC9899, current version 2011. Thus your construct shall be defined in the standard wrt your phrase. Otherwise you have to provide information what that means. The IEEE floating point standard is only part where the standard references it explicitly. This does not imply you can use some arcane syntax not defined in the C standard for floating point constants. 2) Your tags don't specify IEC 60559, nor does your text, which you seem to refer to. There are various floating point formats. – too honest for this site Feb 23 '17 at 01:33
  • 1
    The OP is not using the C language, his question is legitimate for RobotC, which seems to have non standard string conversion of infinities. – chqrlie Feb 24 '17 at 19:42
  • @chqrlie That's why we should insist on using the correct language tag. Good you added robotc, but you should have removed the C tag. – too honest for this site Mar 27 '17 at 15:22

2 Answers2

3

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 */
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
-4

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;}
...
skyline
  • 51
  • 9
  • 2
    What programming language are you using? – chqrlie Feb 21 '17 at 07:19
  • I was using C inside the RobotC for vex IDE. – skyline Feb 22 '17 at 23:35
  • `string` is not a C standard type. Please read [ask], you don't provide all required information in your question, but assume this is universally true. Considering `string` is some string type, this is **not** valid C! – too honest for this site Feb 23 '17 at 01:35
  • @Olaf, just FYI, RobotC is an educational product, best described as *mostly C with a robotics API*. Some simplifications have been added: for instance, `char*` is aliased to `string`, a `bool` type is added, etc. Some standard C items are removed - there is no dynamic memory allocation (dynamic size array is not possible), no struct-returning functions, simplified enum and struct declarations, no function pointers, etc.(although references and pointers are supported for the most part) etc. Most users of RobotC are students, and are not at all aware of the differences between RobotC and C. – CoolBots Mar 27 '17 at 06:41
  • @CoolBots you did notice the timestamps of the edit adding robotc tag and my comments, did you? The question was maltagged. FYI: C has a boolean type! – too honest for this site Mar 27 '17 at 15:17
  • @Olaf well, C99 has a boolean type, via including `stdbool.h`. :) Anyway, it just seemed my comment might be useful in understanding why a RobotC post might be maltagged as C by the asker, in general. My apologies if it's not. – CoolBots Mar 27 '17 at 16:05
  • @CoolBots: `stdbool.h` just provides some macros. C has a built-in boolean type! Read the standard. And you comment is pointless, because the tags are what we use to determine the language. Ignorantia legis no excusat. The [tour] is pretty clear about tagging. – too honest for this site Mar 27 '17 at 16:25