Re: MLGetReal(Double) Problem in MathLink
- To: mathgroup at smc.vnet.net
- Subject: [mg119117] Re: MLGetReal(Double) Problem in MathLink
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Sun, 22 May 2011 06:55:19 -0400 (EDT)
This seems to me more a C-related question than Mathematica or MathLink. You should have changed the format codes for scanf and printf, once you switched to doubles: scanf( "%lf %lf", &a, &b); and printf( "ans = %f\n", a+b); I did not test your MathLink program, but chances are that it should work fine with these modifications. Generally, I'd avoid using scanf. As an alternative, you can read numbers as strings and parse with atoi or atof. Regards, Leonid On Sat, May 21, 2011 at 3:45 AM, Soroush Heidari Pahlavian < shp_maya at yahoo.com> wrote: > When I change the simple addtwo program in order to work with Real(Double) > numbers instead of Integer ones, the answer goes wrong (4+5 = -143566321 > !). > What is the problem with the following code? > 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 doubles: 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; > } > >