infinity in C

Hi,

I know that in C++ we can include <limits.h> and then define +inf as numeric_limits<int>::max() and -inf numeric_limits<int>::min().

How can we define +inf, -inf in C ?

Thanks.
std::numeric_limits<int>::max() is not infinity, it's just a very large integer, usually 2^31-1 or 2^63-1. Integers don't have an infinity value. Furthermore, std::numeric_limits<T>::max() is not infinity for any T. std::numeric_limits<T>::infinity().

You can do double inf=1.0/0.0;.
I like to use superscripts instead of the caret to avoid ambiguity: 231-1 or 263-1.

@newdomingo
You are mixing up your #include files.

The C++ header is <limits>.
http://www.cplusplus.com/reference/std/limits/

The C header is <limits.h> (or <climits> when using C++).
http://www.cplusplus.com/reference/clibrary/climits/

The C header defines macros for INT_MIN, INT_MAX, etc.
The C++ header is more dynamic and provides more information.

For dealing with floating point numbers, you might want to check out
http://www.appinf.com/download/FPIssues.pdf

Hope this helps.
closed account (236Rko23)
afaik u/int8/16/32/64_t don't have an "infinite" value, they go from min to max and then warp around.
in C float and double have a +inf and -inf value, but it's not a value, it's a flag, u can read it with isinf() and sigbit() i think.
The best is to open math.h and read it, it's all inside along with the functions' description
i don't know how to set it to inf, never had to, but there should be a macro or something like that
You can get an "infinity" value from the macros in <float.h> (or <cfloat> in C++).

The isinf() and like functions are found in <math.h> (or <cmath> in C++), but they are non-standard in C90 and C++, so you won't find them available in every C environment.

They are standard for C99, though.
Topic archived. No new replies allowed.