The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: mex gateway function
Replies: 5   Last Post: Jan 3, 2013 6:02 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
James Tursa

Posts: 2,326
Registered: 8/5/09
Re: mex gateway function
Posted: Jan 3, 2013 6:02 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"Michal Kvasnicka" wrote in message <kc3uq0$t49$>...
> How to create suitable mex gateway function coresponduing to the following C function (decimal to binary conversion)?
> void deci2bin( int x, int n, double* output){
> double *temp;
> int i;
> temp = (double *)mxMalloc(sizeof(double)*n);
> i=0;
> while(x>=0 && i<n){
> temp[i] = x%2;
> i++;
> }
> for(i=0;i<n;i++)
> output[i] = temp[n-1-i];
> }

In addition to looking for a fast utility, you should probably be looking for a correct one as well. I don't see how the above routine can work at all since x is not altered during the while loop extracting the "bits" via the x%2 operation. Also, what is supposed to happen for negative x inputs? In the above code, you will end up copying unitialized memory into output for this case. Bottom line is the above code has bugs that need fixing.

James Tursa

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.