2

Is there anything like Java's Float.POSITIVE_INFINITY in NXC? How else can one represent the positive infinity in NXC?

1

I don't believe that a constant or macro is available in NXC for positive infinity. C99 defines INFINITY in math.h, but the math module in NXC does not appear to include this.

NXC uses IEEE 754 floating point, so something like this should give you the raw value of infinity cast to a float:

int raw_infinity = 0x7f800000;
const float INFINITY = *(float*)&raw_infinity;

I don't have a way to test this currently, and I'm not sure how the underlying firmware and hardware handle special floating point values, so this may or may not actually meet your needs.

  • Thank you! Seems to be working- at least compiles, except for the "(float)&". Are you sure, that it is possible to cast type like this? I think, it just does it automatically: <br/> int raw_infinity = 0x7f800000; const float INFINITY = raw_infinity; or just const float INFINITY = 0x7f800000 – elle May 25 '16 at 10:14
  • At least in standard C, you have to do the cast in the way I did in my answer. C will treat the hex value as an int and cast that value to a float. You need the actual binary representation to be used as a float directly without casting it through an int first. – jncraton May 25 '16 at 13:21

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.