Re: passing Indeterminate and Infinity to C via MathLink
- To: mathgroup at smc.vnet.net
- Subject: [mg121352] Re: passing Indeterminate and Infinity to C via MathLink
- From: John Fultz <jfultz at wolfram.com>
- Date: Mon, 12 Sep 2011 04:20:07 -0400 (EDT)
- Delivered-to: firstname.lastname@example.org
- Reply-to: jfultz at wolfram.com
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 expression tree, and MathLink is always very faithful about transmitting the expression 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 MathLink 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 synthesizes 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. Sincerely, John Fultz jfultz at wolfram.com User Interface Group Wolfram Research, Inc.