Questions tagged [infinity]

Infinity is a specific floating point value that is always larger than any other floating point value. It may arise as a result of division by zero, arithmetic overflow and other exceptional operations. Infinity is different from the maximal still valid floating point value that the certain data type can hold. IEEE 754 floating point standard allows both positive and negative infinity values.

Infinity is a specific floating point value that is always larger than any other floating point value. It may arise as a result of division by zero, arithmetic overflow and other exceptional operations. IEEE 754 floating point standard allows both positive and negative infinity values.

Infinity is different from the maximal still valid floating point value that the certain data type can hold. This value is still smaller than the infinity.

Some programming languages allow to specify the infinity in the code explicitly. For instance, the Java code

    double [] values = ...
    double min = Double.POSITIVE_INFINITY;

    for (double x: values) 
      if (x < min) min = x;

can be used to obtain the minimal value in the array that only contains usual, valid floating point values.

475 questions
50
votes
4 answers

How to generate NaN, -Infinity and +Infinity in ANSI C?

I use ANSI C89 (not C++), and I want to generate NaN, -Infinity and +Infinity. Is there any standard way (eg. standard macro)? Or is there any platform and compiler independent way to generate these numbers? float f = 0.0 / 0.0; // Is f ALWAYS in…
Amir Saniyan
  • 13,014
  • 20
  • 92
  • 137
48
votes
3 answers

Testing for positive infinity, or negative infinity, individually in Python

math.isinf() tests for positive or negative infinity lumped together. What's the pythonic way to test for them distinctly? Ways to test for positive infinity: x == float('+inf') math.isinf(x) and x > 0 Ways to test for negative infinity: x ==…
Bob Stein
  • 16,271
  • 10
  • 88
  • 101
44
votes
5 answers

How are NaN and Infinity of a float or double stored in memory?

As I understand it java will store a float in memory as a 32 bit integer with the following properties: The first bit is used to determine the sign The next 8 bits represent the exponent The final 23 bits are used to store the fraction This leaves…
roblovelock
  • 1,971
  • 2
  • 23
  • 41
38
votes
6 answers

How do you check for infinite and indeterminate values in C++?

In my programs infinity usually arises when a value is divided by zero. I get indeterminate when I divide zero by zero. How do you check for infinite and indeterminate values in C++? In C++, infinity is represented by 1.#INF. Indeterminate is…
Samil
  • 958
  • 1
  • 17
  • 20
36
votes
3 answers

What are the INFINITY constants in Java, really?

I just recently ran across the constants in the primitive type wrapper classes like Double.POSITIVE_INFINITY and Double.NEGATIVE_INFINITY. In the API, it defines the first as: A constant holding the positive infinity of type double. It is equal to…
asteri
  • 11,402
  • 13
  • 60
  • 84
34
votes
5 answers

How to represent integer infinity?

I need a way to represent an integer number that can be infinite. I'd prefer not to use a floating point type (double.PositiveInfinity) since the number can never be fractional and this might make the API confusing. What is the best way to do…
ConditionRacer
  • 4,418
  • 6
  • 45
  • 67
32
votes
3 answers

Why is infinity = 0x3f3f3f3f?

In some situations, one generally uses a large enough integer value to represent infinity. I usually use the largest representable positive/negative integer. That usually yields more code, since you need to check if one of the operands is infinity…
Gabriel
  • 1,803
  • 1
  • 13
  • 18
31
votes
9 answers

In Haskell, is there infinity :: Num a => a?

I'm trying to implement a data structure where if I had the use of infinity for numerical comparison purposes, it would simply things greatly. Note this isn't maxBound/minBound, because a value can be <= maxbound, but all values would be <…
me2
  • 3,069
  • 2
  • 26
  • 33
28
votes
24 answers

In what contexts do programming languages make real use of an Infinity value?

So in Ruby there is a trick to specify infinity: 1.0/0 => Infinity I believe in Python you can do something like this float('inf') These are just examples though, I'm sure most languages have infinity in some capacity. When would you actually use…
Chris Lloyd
  • 12,100
  • 7
  • 36
  • 32
28
votes
4 answers

JSON.stringify converting Infinity to null

I have JavaScript Object say: var a = {b: Infinity, c: 10}; When I do var b = JSON.stringify(a); it returns the following b = "{"b":null, "c":10}"; How is the JSON.stringify converts the object to strings? I tried MDN Solution. function…
me_digvijay
  • 5,374
  • 9
  • 46
  • 83
26
votes
4 answers

INFINITY in Swift Lang

According to Apple's documentation, Swift doesn't support preprocessor directives. In C/Objective-c the "INFINITY" definition is very useful for some checks. So, How do I get a number that never is less that another?
Daniel
  • 783
  • 1
  • 5
  • 16
26
votes
6 answers

How do I create or test for NaN or infinity in Perl?

How do I create or test for NaN or infinite values in Perl?
ysth
  • 96,171
  • 6
  • 121
  • 214
23
votes
2 answers

Why is ( Infinity | 0 ) === 0?

I'm fiddling around with bitwise operators in JavaScript and there is one thing I find remarkable. The bitwise or operator returns 1 as output bit if one of the two input bits are 1. So doing x | 0 always returns x, because | 0 has no effect: ( 1 |…
pimvdb
  • 151,816
  • 78
  • 307
  • 352
22
votes
2 answers

Is there an inf constant in Perl?

I'm initialising a list with infinities for an algorithm. Writing $x = 9**9**9 feels unintuitive, and also, I might want to use BigInt in the future. 1/0 throws an error. What's the canonical way to get inf?
Andreas
  • 7,470
  • 10
  • 51
  • 73
21
votes
4 answers

Is the inverse of std::numeric_limits::infinity() zero?

Is there anything in the C++ standard (or the IEEE 754 floating-point standard) that guarantees that 1./std::numeric_limits::infinity() is zero (or at least a small number)?
davidhigh
  • 14,652
  • 2
  • 44
  • 75
1
2
3
31 32