Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #2595

Re: MLGetReal(Double) Problem in MathLink

From Leonid Shifrin <lshifr@gmail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: MLGetReal(Double) Problem in MathLink
Date 2011-05-22 10:56 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <iraq4t$lpg$1@smc.vnet.net> (permalink)

Show all headers | View raw


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@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;
> }
>
>

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: MLGetReal(Double) Problem in MathLink Leonid Shifrin <lshifr@gmail.com> - 2011-05-22 10:56 +0000

csiph-web