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
21
votes
7 answers

How do you represent infinity in a JSON API?

What is the best way to represent infinity in a JSON API? e.g. free to read articles for this month (this will be a finite number on some subscriptions and infinite on the premium subscription). Is it a better idea to return null for this use case…
Oin
  • 6,951
  • 2
  • 31
  • 55
20
votes
2 answers

how to remove positive infinity from numpy array...if it is already converted to a number?

How does one remove positive infinity numbers from a numpy array once these are already converted into a number format? I am using a package which uses numpy internally, however when returning certain arrays, certain values are returned as the…
songololo
  • 4,724
  • 5
  • 35
  • 49
20
votes
4 answers

Why does node not evaluate Math.tan(Math.PI/2) to Infinity but Chrome V8 does?

When running this in a node command-line interface: > Math.tan(Math.PI/2) 16331778728383844 But in Chrome: > Math.tan(Math.PI/2) Infinity Aren't both using the same V8 engine? Node's result is not even equal to the maximum "integer" value in…
Ludovic C
  • 2,855
  • 20
  • 40
20
votes
5 answers

PHP: How to encode infinity or NaN numbers to JSON?

Apparently, infinity and NaN are not a part of JSON specification, so this PHP code: $numbers = array(); $numbers ['positive_infinity'] = +INF; $numbers ['negative_infinity'] = -INF; $numbers ['not_a_number'] = NAN; $array_print = print_r ($numbers,…
Septagram
  • 9,425
  • 13
  • 50
  • 81
19
votes
3 answers

If I enter [1/0..1/0] on GHCI I am getting Infinite Infinity. Why?

I can't understand the following behavior of ranges in Haskell. Enumerating 1 to 1 gives me a list contain only 1, and 2 to 2 gives me a list contain only 2 as given below. Prelude> [1..1] [1] Prelude> [2..2] [2] But enumerating infinity to…
Kavin Eswaramoorthy
  • 1,595
  • 11
  • 19
19
votes
3 answers

glsl infinity constant

Does GLSL have any pre-defined constants for +/-infinity or NaN? I'm doing this as a workaround but I wonder if there is a cleaner way: // GLSL FRAGMENT SHADER #version 410 const float infinity = 1. / 0.; void main () { } I am…
atb
  • 1,412
  • 1
  • 14
  • 30
18
votes
2 answers

Python integer infinity for slicing

I have defined a slicing parameter in a config file: max_items = 10 My class slices a list according to this parameter: items=l[:config.max_itmes] When max_items = 0, I want all items to be taken from l. The quick and dirty way…
Adam Matan
  • 128,757
  • 147
  • 397
  • 562
18
votes
1 answer

Why do dates at infinity look like NAs but act like dates?

I was trying to figure out the best way to deal with Postgresql's ability to represent Infinity and -Infinity in their timestamps when using RPostgreSQL to bring data over into R. Along the way I found some strange behavior when trying to represent…
jed
  • 615
  • 3
  • 11
18
votes
1 answer

Why does flooring infinity not throw some error?

I found myself having a case where the equivalent of floor $ 1/0 was being executed. λ> 1/0 Infinity This is normal behavior as far as I understand but, when Infinity is floor'd or ceiling'd λ> floor $ 1/0 …
gxtaillon
  • 1,016
  • 1
  • 19
  • 33
17
votes
3 answers

Do something infinitely many times with an index

In more ruby way of doing project euler #2 , part of the code is while((v = fib(i)) < 4_000_000) s+=v if v%2==0 i+=1 end Is there a way to change i += 1 into a more functional programming style construct? The best I can think of…
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
17
votes
9 answers

Modeling infinity for the largest double value

The question is about modeling infinity in C++ for the double data type. I need it in a header file, so we cannot use functions like numeric_limits. Is there a defined constant that represents the largest value?
ashim
  • 24,380
  • 29
  • 72
  • 96
17
votes
2 answers

How to store infinity in a BigDecimal (Java)?

I am writing algorithms inside methods that return BigDecimal values but now and again the result calculated will be + or - infinity. Rather than the program crashing I'd like to catch the exception and return infinity as a value like the way you…
user2989759
  • 279
  • 1
  • 4
  • 14
16
votes
2 answers

Adding to Number.MAX_VALUE

The answer to this question may be painfully obvious but I can't find it in the Mozilla docs nor on Google from a cursory search. If you have some code like this Number.MAX_VALUE + 1; // Infinity, right? Number.MIN_VALUE - 1; // -Infinity,…
Mark
  • 1,312
  • 1
  • 16
  • 33
15
votes
3 answers

Creating complex infinity with std::complex in C++

I'm trying to create a complex infinity equal to Inf+Inf*j where j is the complex variable. When I do this : #include #include using std; ... complex attempt1 = complex( numeric_limits::infinity(), …
14
votes
4 answers

Why does this method return double.PositiveInfinity not DivideByZeroException?

I ran the following snippet in the VS2015 C# interactive and got some very weird behavior. > double divide(double a, double b) . { . try . { . return a / b; . } . catch (DivideByZeroException exception) . { . …
Kelson Ball
  • 948
  • 1
  • 13
  • 30
1 2
3
31 32