I tried to add float('-inf') and 10 in python, as per my knowledge -inf is smaller than all other values.
So if I add -inf and 10 it should give 10 as an answer. Rather than giving 10 as an output it is giving -inf.
Is -inf bigger than 10?
-inf means negative infinity. It is "smaller" than all other values in that it is less than them. Adding negative infinity to any finite number still gives negative infinity.
-inf is the smallest number but what that means is that it's the negative number with the largest magnitude. It doesn't mean it's the closest you can get to zero without actually being zero (i.e., the smallest positive number):
<---------------------------|-------------------------->
-inf 0 10 +inf
When you add a massive negative number to 10, you'll still end up with a massive negative number. That's the same idea as with -inf, other than the fact infinity is not a real number.
Related
Is this just the smallest number that can be stored in 32 bits for example?
from math import inf
Floating-point numbers (real numbers as typically implemented on a computer) have special values reserved for positive and negative infinity. Rather than just being “the largest or smallest representable 32-bit numbers,” they act as though they really are infinite. For example, adding anything to positive infinity (other than negative infinity) gives positive infinity, and a similar rule holds for negative infinity.
For more on this, do a search for “IEEE-754 infinity.”
I have the following code
x = -10
for i in range(2,10):
print i, " | ",np.exp(-x**i)
with the following output:
2 | 3.72007597602e-44
3 | inf
4 | 0.0
5 | inf
6 | 0.0
7 | inf
8 | 0.0
9 | inf
Why is the results ~0 for i even and Inf for i odd?
Since x = -10, x**i will alternate between positive and negative high values, and so will -(x**i) which is what is calulated when you write -x**i .np.exp(inf) = inf and np.exp(-inf) = 0 so for high enough numbers, you're alternating between infinity and 0.
You probably wanted to write np.exp((-x)**i), which will make the power index always positive.
The maximum value of a double-precision floating value (which numpy uses for floating-point arithmetic) is a little under 1.8e308 (1.8 * 10^308).
The value for 3 in your table would be e^1000, which WolframAlpha says is a little less than 2e434. The problem is that the numbers you want to use are just too large for numpy to handle, so for odd values, you get infinity.
The inverse is true for the even numbers; you're calculating a number so small that numpy must treat it as effectively zero.
Short answer
Because xi with x < 0 is greater than zero if i is even and smaller than zero if i is odd. Since the exponential function generates very large or very small numbers with very large positive and very large negative input, the values are rounded off to infinity or zero, as is normal for IEEE-754 numbers.
For odd values
By default Python handles floating point numbers as IEEE-754 "double precision".
The limits of a double are between -1.7*10308 and 1.7*10308, everything above and below is considered to be -Inf and +Inf. Now if you calculate this for i=3, you get:
exp(-(-10**3))=exp(-(-1000))=exp(1000)
or approximately 1.9*10434, clearly above the threshold value. For every odd i, the result of -10**i is negative, thus the operand of exp is positive, and you will get values above the threshold.
For even values
For even values, this results in:
exp(-(-10**4))=exp(-10000)
which is again approximately 1.13*10-4343
The smallest positive value (greater than zero) a double can represent is 2-53. The obtained value is clearly lower, all positive values lower than this epsilon; are rounded off to zero, simply because it is the nearest representable value.
How to generate exponential variate without negative numbers in Python?
I tried to use this code but it generates negative number
>>> import random
>>> int(random.expovariate(0.28)
5
I thought about using if statement, but it'll affect my randomness and my final result.
From the documentation of random.expovariate:
Exponential distribution. lambd is 1.0 divided by the desired mean. It should be nonzero. (The parameter would be called “lambda”, but that is a reserved word in Python.) Returned values range from 0 to positive infinity if lambd is positive, and from negative infinity to 0 if lambd is negative.
If you want non-negative results, use non-negative arguments.
I am currently translating a MATLAB program into Python. I successfully ported all the previous vector operations using numpy. However I am stuck in the following bit of code which is a cosine similarity measure.
% W and ind are different sized matrices
dist = full(W * (W(ind2(range),:)' - W(ind1(range),:)' + W(ind3(range),:)'));
for i=1:length(range)
dist(ind1(range(i)),i) = -Inf;
dist(ind2(range(i)),i) = -Inf;
dist(ind3(range(i)),i) = -Inf;
end
disp(dist)
[~, mx(range)] = max(dist);
I did not understand the following part.
dist(indx(range(i)),i) = -Inf;
What actuality is happening when you use
= -Inf;
on the right side?
In Matlab (see: Inf):
Inf returns the IEEE® arithmetic representation for positive infinity.
So Inf produces a value that is greater than all other numeric values. -Inf produces a value that is guaranteed to be less than any other numeric value. It's generally used when you want to iteratively find a maximum and need a first value to compare to that's always going to be less than your first comparison.
According to Wikipedia (see: IEEE 754 Inf):
Positive and negative infinity are represented thus:
sign = 0 for positive infinity, 1 for negative infinity.
biased exponent = all 1 bits.
fraction = all 0 bits.
Python has the same concept using '-inf' (see Note 6 here):
float also accepts the strings “nan” and “inf” with an optional prefix “+” or “-” for Not a Number (NaN) and positive or negative infinity.
>>> a=float('-inf')
>>> a
-inf
>>> b=-27983.444
>>> min(a,b)
-inf
It just assigns a minus infinity value to the left-hand side.
It may appear weird to assign that value, particularly because a distance cannot be negative. But it looks like it's used for effectively removing those entries from the max computation in the last line.
If Python doesn't have "infinity" (I don't know Python) and if dist is really a distance (hence nonnegative) , you could use any negative value instead of -inf to achieve the same effect, namely remove those entries from the max computation.
The -Inf is typically used to initialize a variable such that you later can use it to in a comparison in a loop.
For instance if I want to find the the maximum value in a function (and have forgotten the command max). Then I would have made something like:
function maxF = findMax(f,a,b)
maxF = -Inf;
x = a:0.001:b;
for i = 1:length(x)
if f(x) > maxF
maxF = f(x);
end
end
It is a method in matlab to make sure that any other value is larger than the current. So the comparison in Python would be -sys.maxint +1.
See for instance:
Maximum and Minimum values for ints
What is python's threshold of representable negative numbers? What's the lowest number below which Python will call any other value a - negative inifinity?
There is no most negative integer, as Python integers have arbitrary precision. The smallest float greater than negative infinity (which, depending on your implementation, can be represented as -float('inf')) can be found in sys.float_info.
>>> import sys
>>> sys.float_info.max
1.7976931348623157e+308
The actual values depend on the actual implementation, but typically uses your C library's double type. Since floating-point values typically use a sign bit, the smallest negative value is simply the inverse of the largest positive value. Also, because of how floating point values are stored (separate mantissa and exponent), you can't simply subtract a small value from the "minimum" value and get back negative infinity. Subtracting 1, for example, simply returns the same value due to limited precision.
(In other words, the possible float values are a small subset of the actual real numbers, and operations on two float values is not necessarily equivalent to the same operation on the "equivalent" reals.)