This question may be a bit strange, but in this question, one of the answers suggests substituting some negatives values in a matrix by the smallest positive real number allowed. So, I was wondering how I do that for mathematica.
2 Answers
Within machine numbers, $MinMachineNumber
is the smallest positive number that can be used on your system. On a 64-bit system it'll typically be on the magnitude of $10^{-308}$.
If that is not small enough, you can use arbitrary precision arithmetic, such as by: N[10, d]^-10000
, where d
is the number of digits of precision you need. I'd recommend trying 17 to start with, as that's about one more digit of precision than machine-precision typically tends to be.
There's no practical limit to how small of an arbitrary precision number you can have. The smallest number my system (64-bit, Windows, Intel CPU) accepts without underflowing is N[10, 17]^-1355718576299609
, but your results may vary. Note that using that particular number may lead to issues, as it is exceptionally close to underflowing on my system -- dividing it by 1.61 will throw an Underflow[]
error. As noted by Chip Hurst, the smallest positive arbitrary number on your system can be found with $MinNumber
.
However, for the exact same reason that I'd recommend against using the smallest possible arbitrary precision number, I'd also recommend against using $MinMachineNumber
in arithmetic. Henrik Schumacher's suggestion of $MachineEpsilon
is a good one for a small number that is still behaves somewhat as expected in machine precision arithmetic, but depending on the intermediate products it may be too large or too small.
If you truly want the absolutely smallest positive real number that can be represented in Mathematica, use arbitrary precision. If you want the smallest machine precision number, use $MinMachineNumber
because that's what it's there for. If you want the smallest machine precision number which doesn't vanish (or overflow, as a denominator) in all arithmetic operations, it depends strongly on your intermediate values, but $MachineEpsilon
is probably a good starting point.
-
2$\begingroup$ Note that the smallest positive arbitrary precision number is
$MinNumber
. $\endgroup$ Commented Nov 18, 2018 at 2:13 -
$\begingroup$ $Thanks eyorble ;) $\endgroup$ Commented Nov 18, 2018 at 8:45
If you are working with machine precision numbers, you can find the smallest non-zero positive power of 2
NestWhileList[#/2 &, 1.0, # > 0 &][[-2]]
I think this is the smallest representable positive number.
$MachineEpsilon
is the smallest positive machine number (in the sense that it is the smallest positivex
such that1. + x
is different from1.
in machine precision). Maybe you mean that? $\endgroup$