Re: Problem with MLGetReal(Double) in Mathlink
- To: mathgroup at smc.vnet.net
- Subject: [mg119184] Re: Problem with MLGetReal(Double) in Mathlink
- From: John Fultz <jfultz at wolfram.com>
- Date: Tue, 24 May 2011 05:57:54 -0400 (EDT)
addtwo isn't the program you should be changing. factor is the example you want to start with because it's the closest to doing exactly what it is you want to do. And if you look at factor.c, you'll notice that you are missing a critical step. You didn't send an EvaluatePacket. The Mathematica kernel has a protocol for how it receives information from other applications, and you need to follow it. That means, in this case, using EvaluatePacket (or one of its compatriots, depending upon your needs) to wrap the evaluation you wish to perform. The reason for the wild answer, incidentally, is because MLGetDouble() failed. You didn't realize this because you didn't check the return value to determine whether there was an error. The documentation for MLGetDouble() does not define any behavior for how MLGetDouble will treat its double pointer argument when it fails. I suspect, in this case, that it merely left untouched whatever random bits happen to be in the uninitialized double. Sincerely, John Fultz jfultz at wolfram.com User Interface Group Wolfram Research, Inc. On Sat, 21 May 2011 06:48:17 -0400 (EDT), Soroush Heidari wrote: > When I change the simple addtwo program in order to get 2 Real > (Double) numbers and get a Real result, the result is wrong (5+4 => 97986213 !) What is the problem. > Thanks... > *********************************************************************** > #include <stdio.h> > #include "mathlink.h" > > int main( int argc, char* argv[]) > > { > double a, b; > double ans; > > MLINK lp; > MLEnvironment env; > printf( "Enter two integers: a and b\n\t" ); > scanf( "%d %d", &a, &b); > env = MLInitialize(NULL); > if(env == NULL) return 1; > lp = MLOpen(argc, argv); > if(lp == NULL) return 1; > > MLPutFunction(lp, "Plus", 2); > MLPutDouble(lp, a); > MLPutDouble(lp, b); > MLEndPacket(lp); > > /* skip any packets before the first ReturnPacket */ > while (MLNextPacket(lp) != RETURNPKT) MLNewPacket(lp); > > MLGetDouble(lp, &ans); > printf( "ans = %d\n", ans); > MLClose(lp); > MLDeinitialize(env); > return 0; > }