I have a problem with the atan2 function.
I have 2 programs, one in C and one in Matlab. Both have an identical implementation.
My problem is that I am getting occasionally different values from the atan2 function in C and Matlab.
For instance:
y= -0.96764419257510348
Matlab atan2= 2.8438364863237604
C atan2= 2.8438364863237608
So only the last digit is different (and not in all cases), but unfortunately, I would need the values to be identical.
Do you have any ideas it might be like this and how can I solve it?
Thank you.

Recommended Answers

All 6 Replies

Solved the problem (more or less).
It seems that the retun values of the two functions differ a little.
So one solution is to make a mex function (c function that can be executed in matlab) that implements the atan2 funtion.
Now the output is the same.

It seems like I actually haven't solved the problem. The values are still sometimes different and I have no clue why.
I have even set the same compiler for the mex files and the C program, and still the values are a little different.
Does anyone have any idea why?
Thank you

Matlab likely guarantees a higher precision than C, and what you're seeing is the round-off errors creeping into the C algorithm.

Is there any way to set them to return the same values, maybe set the precision in matlab?

You can round to something like six digits in both languages, that would probably be more than sufficient.

Thx for the answer. I will give it a try, see what happens.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.