Can one be found programmatically? The function I'm compiling, indicesOfMin, gives the indices of the minimum value(s) of a real-valued list, the parameter, in a single traversal (which is why I'm using this instead of Ordering or Sort).

indicesOfMin = Compile[{{list, _Real, 1}},
  Module[{bag = Internal`Bag[Most[{0}]], min = 1.7976931348623157*^308, sub = 0.},
    Do[If[(sub = list[[i]] - min) < 0, min = list[[i]]; bag = Internal`Bag[Most[{0}]]; Internal`StuffBag[bag, i];,
      If[sub == 0, Internal`StuffBag[bag, i]]],
      {i, 1, Length[list], 1}];
    Internal`BagPart[bag, All]

As you can see, I'm using C++'s positive infinity value for my first setting of min, but I don't even know if that's the language this will be compiled to.

Potential issues:

  1. I understand I can use the first value in list for min, but that takes extra code and I would like to avoid doing so because it also creates a precondition that the list is at least one element long.
  2. I don't know what language this will be compiled to in advance (the function is part of a package, and the person running it may not have a C compiler, so the maximum field value might be wrong.
  • 1
    $\begingroup$ Compiled functions work with machine numbers, so the same limitations apply that you have in C when working with the double type. I think the minimum/maximum depends on how your CPU works, not the language. Compile can compile to a special byte code used by Mma, or it can compile to C. Both use the same type of machine number. $\endgroup$
    – Szabolcs
    Commented Feb 6, 2013 at 18:25
  • $\begingroup$ @Szbolcs don't various languages allocate different amounts of space for doubles regardless of your CPU? $\endgroup$
    – VF1
    Commented Feb 6, 2013 at 18:32
  • 2
    $\begingroup$ I'd use $MaxMachineNumber but I don't know enough about the internals to vouch for it. You need to figure out a way to throw it in to the Compile to avoid MainEvaluate perhaps with Block $\endgroup$
    – ssch
    Commented Feb 6, 2013 at 18:35
  • $\begingroup$ Typically they don't. Only a handful have custom float implementations, like Mathematica or Maple, and this is usually to support arbitrary precision. The rest use the hardware support in your CPU. What makes things more complicated is that Intel CPUs nowadays support a number of representations ... That said, I think what you are trying to do is a hack that's not wort the risk. I'd say just use a proper implementation (take the first element). I came to the same conclusion here: I won't take the shortcut. $\endgroup$
    – Szabolcs
    Commented Feb 6, 2013 at 18:37
  • $\begingroup$ @ssch I guess that's worth posting as answer. $\endgroup$
    – Szabolcs
    Commented Feb 6, 2013 at 18:41

2 Answers 2


To my surprise, I found that it's actually not difficult to produce a true floating point infinity inside compiled code. Here's how:

cf = With[{
   $MaxMachineNumber = $MaxMachineNumber,
   $MachineEpsilon = $MachineEpsilon
  Compile[{{x, _Real, 0}},
   Block[{inf = (1. + $MachineEpsilon) $MaxMachineNumber},
    inf > x
   ], RuntimeOptions -> "CompareWithTolerance" -> False

We can verify that this value is larger than the largest finite number:

(* -> True *)

However, while one can use it inside compiled code, it is not possible to return it as a result: this causes a numerical error that results in the code being reevalated outside of the VM.

It would also appear that the Mathematica VM is not particularly IEEE754-compliant: attempts to produce a NaN (e.g. NaN = 0. / 0.) result in a value that is both greater and less than any other, when in fact it should be neither greater nor less than anything else. This could perhaps be a valid choice but will make it very difficult to use NaNs productively in compiled code. Furthermore, both inf / inf and inf - inf should give NaN, but while the former works as it should, the latter gives an error, possibly as a result of producing a signaling NaN value (even though exclusively quiet NaNs seem to be produced otherwise).

A final important point: to have any hope of obtaining meaningful (I won't go as far as to say consistent) results with respect to NaNs, it is important to set the option RuntimeOptions -> "CompareWithTolerance" -> False as I have done in the example. Absent this, NaN compares less (but not greater) than both itself and any strictly positive value, which is obviously semantically incorrect.


$MaxMachineNumber has the largest machine number the current computer supports, you could force it into the Compile like this:

f = With[{$MaxMachineNumber = $MaxMachineNumber},
      Compile[{{i, _Real}},
        Module[{min = $MaxMachineNumber},
          Min[i, min]

I don't know if this is a reliable way though.

  • 2
    $\begingroup$ The same trick is used in this answer mathematica.stackexchange.com/a/4877/745. Works without any problems. $\endgroup$
    – Ajasja
    Commented Feb 6, 2013 at 18:46
  • $\begingroup$ Actually machine infinity is also supported in compiled code... the problem is that there's no way to pass it in or out, so that's why I chose this value instead. $\endgroup$ Commented Feb 6, 2013 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.