开发者

C and Infinity Size Variable

开发者 https://www.devze.com 2023-01-10 05:37 出处:网络
How variable beyond \"unsigned long long\" size is represented in C or any programming language. Is 开发者_如何转开发there are variable type to represent Infinity? See: Floating point InfinityBoth flo

How variable beyond "unsigned long long" size is represented in C or any programming language. Is 开发者_如何转开发there are variable type to represent Infinity?


See: Floating point Infinity


Both floats and doubles have representations for infinity. Integral types do not have this capacity built into the language but, if you need it, there are tricks you can use to assign INT_MAX or LONG_MAX from limits.h for that purpose.

Of course, to do that, you have to have complete control of the calculations - you don't want a regular calculation ending up with that value and then being treated as infinity from there on.

For numbers larger than those that can be represented by the native types, you can turn to an arbitrary precision math library such as GMP.


Are you talking about representing the number infinity, or working with numbers too big to fit into a single native data type? For the latter, it's possible to create data structures that represent larger numbers, and write your own logic to implement arithmetic for those structures.

I've done this on a basic level for some problems at Project Euler. My approach was to extend the algorithms used in elementary school, which let you do operations on multi-digit numbers by breaking them down into operations using only single-digit numbers plus some carrying, remainders, borrowing, etc.

You might also be interested in this wikipedia article on arbitrary-precision arithmetic.


unsigned long long is the largest of the C standard integer types, it must be able to represent at least 264 – 1. There is no integer type in Standard C that must represent values larger than this. Some implementations may offer a larger type as an extension, but you can't rely on it.

test.c:3: error: ‘long long long’ is too long for GCC

The standard integer types can't represent infinity unless you dedicate a specific bit-pattern for that purpose (for example, ULLONG_MAX). I think the floating point types do have representations for infinity. You can check for whether a floating point type is infinity with the Standard C isinf macro, and you can set a floating point type to infinity with the INFINITY macro. I think that Standard C does not require floating point types to be able to represent positive and negative infinity however.

0

精彩评论

暂无评论...
验证码 换一张
取 消