Re: passing Indeterminate and Infinity to C via MathLink

*To*: mathgroup at smc.vnet.net*Subject*: [mg121446] Re: passing Indeterminate and Infinity to C via MathLink*From*: Roman <rschmied at gmail.com>*Date*: Thu, 15 Sep 2011 04:40:56 -0400 (EDT)*Delivered-to*: l-mathgroup@mail-archive0.wolfram.com*References*: <j4kfii$kes$1@smc.vnet.net>

Thanks John. The entire reason why I am communicating with external C procedures is in order to speed up computation; if much time is spent in the communication interface then this defeats the point. In particular, when passing large arrays of real numbers (containing NaN and/or inf) then receiving the array element by element via MLGetNext() seems a very inefficient thing to do. I appreciate how faithful Mathematica is in the transmission process, but when I pass "NaN" from C to Mathematica via MLPutReal64() then Mathematica does in fact receive the "Indeterminate" symbol, not an IEEE "NaN". So the conversion capability is built into Mathematica in one direction but not in the other. What I was hoping for is a trick which allows me to use such an automatism in the Mathematica->C direction via MLGetReal64Array(). For instance, is there a way to convert a matrix in Mathematica into a pure numerical representation (where every element must be an IEEE number) which could then be forwarded immediately (no conversions) to MLGetReal64Array()? Cheers! Roman On Sep 12, 10:23 am, John Fultz <jfu... at wolfram.com> wrote: > On Sat, 10 Sep 2011 07:29:23 -0400 (EDT), Roman wrote: > > Hello all, > > I am setting up a C function which accepts real numbers from MathLink. > > The behavior I would like to achieve is that whenever the number is > > "Infinity" then the C function receives "inf" (which is a valid double- > > precision-format number); and whenever the number is "Indeterminate" > > then the C function receives "nan" (which is also a valid double- > > precision-format number). > > Unfortunately MathLink (Mathematica 7.0 for Mac OS X x86 (64-bit)) > > crashes whenever I am trying to pass either Infinity or Indeterminate > > to a MathLink function expecting a double-precision number. > > Would you know how to solve this without going into If[] statements on > > the Mathematica side of MathLink? > > Thanks! > > Roman > > Mathematica represents Indeterminate and Infinity as symbols in its expre= ssion > tree, and MathLink is always very faithful about transmitting the express= ion > tree precisely. Note that it's not very difficult to deal with this in= your C > code, though. You can just declare the function as having a Manual Mat= hLink > type and then, in the C function determine using MLGetNext() whether the = next > thing is a symbol or a real. If it's a symbol, then you can just synth= esizes > the IEEE version of the indeterminate value in your C program. > > About two thirds of the way down this help page: > > tutorial/HandlingListsArraysAndOtherExpressions > > there's an example that illustrates how to use Manual as an argument type=