I've written a Mathlink program that is called from Mathematica 9. It works very well but currently uses double precision numbers in the C++ function.

I've already replaced them with floats in C++ and corrected the .tm files ArgumentTypes to

:ArgumentTypes: { Real32List, Integer, Real32 }

But when calling the function within Mathematica like


it just returns the input - therefor a data type missmatch occurs.

Can anyone tell me how to pass the numbers as Real32? It's quite important to use 32 bit floating point numbers since I am doing calculations on a GPU that has a significatly better single precision performance.


//template file:

void calc P((float *, int, int, float));

:Function:       calc
:Pattern:        Calc[a_List, b_Integer, c_Real32]
:Arguments:      { a, b, c}
:ArgumentTypes:  { Real32List, Integer, Real32 }
:ReturnType:     Manual
  • 1
    $\begingroup$ If you used something like f[x_Real32] in the :Pattern: part, it should instead be f[x_Real] as Real32 is not a datatype in Mathematica. The things you put in :ArgumentTypes: are not the same as the things you put in :Pattern:. The former refers to data types MathLink can transfer while the latter will be used to validate the function call in Mathematica and need to be a Mathematica pattern. $\endgroup$
    – Szabolcs
    Mar 26, 2014 at 15:13
  • $\begingroup$ If you will please share a complete example (i.e. full source code, template file and C) of a minimal attempt to write a function that takes a single Real32 and returns it, I'll probably be able to spot the problem. $\endgroup$
    – Szabolcs
    Mar 26, 2014 at 15:19
  • $\begingroup$ Thanks! That was the solution. In the pattern I now use F[x_List, y_Real] and in ArgumentTypes { Real32List, Real32 } and the C function is P((float *, int, float)); $\endgroup$ Mar 26, 2014 at 16:14
  • $\begingroup$ @Szabolcs will you please post an answer, or vote to close? Thank you! $\endgroup$ Mar 26, 2014 at 18:00
  • 1
    $\begingroup$ @JacobAkkerboom I don't want to vote to close. MathLink is not that well covered, so it's useful to have this. user2224780, can you include some extra information in your post, in particular the complete template for this function, to show the mistake very clearly? Then I'll post an answer. We're trying to make questions on this site useful not only for the original asker but also for future visitors. $\endgroup$
    – Szabolcs
    Mar 26, 2014 at 18:15

1 Answer 1


What you are trying to do is described in the tutorial Handling Lists, Arrays, and Other Expressions. In the middle of the tutorial it is explained how you can transfer different types of numbers. It works the same if you want to transfer a real with 32 bit.

:Function:       f
:Pattern:        f[i_Integer, x_Real, y_Real]
:Arguments:      {i, x, y}
:ArgumentTypes:  {Integer, Manual}
:ReturnType:     Real

then your function f looks like

float f(int i) {
  float x, y;
  WSGetReal32(stdlink, &x);
  WSGetReal32(stdlink, &y);
  return i+x+y;

Something along this should help you.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.