This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How can I convert from unsigned int to double so that I can use the sqrt function?

Hi guys,

this may seem like a newbie type of question but i'm really stumped on this one. I want to use the sqrt() function from math.h on two variables which are unsigned int.

if i do:

unsigned int Id, Iq;
double temp;
Id = ///some function call
temp = (double)Id;

and assuming Id contains say 0x2700, after executing the 3rd line, temp = 0, not 9984 as it should. Does anyone know how I can convert from unsinged int to double besides the way I've tried.

thanks a lot.

Nick

  • Are you using the evaluation version?

  • Andrew, thanks for the reply.

    No, I'm using the full version. The problem I was having was when I was running my code using the Keil/FS2 Simulator. When I connected my actual 8051 board, and ran my code in the Keil/FS2 Debugger, I was actually getting the correct type "double" values after using (double) to convert from unsigned int to double. This raises the question, is the Simulator reliable? My boss asked me the same question and I told him that yes, if the simulator works fine, then the code should also work fine when executed on the actual hardware, but this was not true in my case, as I found out later. I guess I can't rely much on the simulator. Any ideas about this?

    thanks,
    Nick

  • "I guess I can't rely much on the simulator."

    I can't see why that should be the case.

    Does the code actually function correctly on the simulator?

    Could this just be an optimisation issue, where you're trying to watch a variable that's been optimised away?
    Have you tried stepping at the assembler level in the simulator.

  • Nick,

    The statement about code being fine if it runs ok on the simulator is mostly true, but there are some exceptions. For instance, if you do something that has "undefined" results as far as the instruction set is concerned, then the simulator will do whatever it feels like, but that might not necessarily match up with what your particular target chip does. Other than that, the results should be identical.

    Another note as well: Although this isn't causing the problem you're having, I'm not sure why you're using the "double" data type. Maybe I'm mistaken, but as far as I know, Keil C51 only supports single-precision floating point values. I believe if you define a double, you'll find it's still just a float.

  • The statement about code being fine if it runs ok on the simulator is mostly true

    there are many more examples of the "OK on simulator does not mean OK".

    For instance you can run just fine on the simulator withour debounce, try that in real life.

    A simulator can be excellent for some things, but it is no "proofer"

    Erik

  • But this is different: he says it works in his target, but not in the simulator!

    This could, of course, still be undefined behaviour - and he just happens to be lucky on his real target...

    (actually, he just said he couldn't view the variable in the simulator; he didn't actually say that it didn't work - hence my earlier follow-up question)

  • Hi Guys, Thank you all for your replies.

    Andrew: the code did not function correctly in the simulator to begin with, converting to double (or float) from unsigned int would give me zero, not the value it should have given me, and yes, I am stepping at the assembler level in the simulator and watching variable contents as each assemly line is executed. When I run the same code in the debugger with my 8051 board connected (8051 executing code, and not the simulator), the type conversion works correctly (this is all I need because in the real world my code will run on my target board, not the simulator).

    Jay, even if I use float, I have the same problem - code works correctly on the target board, but not in the simulator.

    Erik, I wasn't talking about a harware issue like a switch debounce, but I appreciate your comments - Simulator is just a proofer.

    Ultimately, the code works correctly on the actual target board so I'm satisfied.
    Thank you all for your input.

    -Nick

  • Ooops, I meant "Simulator is not a 100% proofer"

    Nick

  • Nick,

    I've tried to duplicate you problem here and I can't seem to get it to fail at all running under the simulator. Since you didn't provide a complete example, I had to invent one and maybe that's the problem.

    Also, I ran everything under the Keil simulator. I'm not sure what a "Keil/FS2 Simulator" is.

    If you can provide me with a complete example that fails it would help us find any problem in simulation.

    FYI, I use the simulator on a daily basis and rarely find a problem with it.

    Jon

  • Hi Jon and All,

    We are using a third party Debugger called FS2 by First Silicon Solutions. It is designed to work with the Keil environment. It comes with its own simulator. Since we use that debugger to do board level debug, we also used its simulator to do the simulations. So it is not the Keil Simulator, but a third party simulator, which is designed to work within the Keil IDE. That could explain the problem I was having with simulating my code, but its all resolved now. So again, it was a third party simulator I was using, not the Keil simulator, and I indicated that when I said we're using the Keil/FS2 Simulator.

    thanks
    -Nick

  • Perhaps you need to alert these First Silicon Solutions guys that maybe there is a bug in their simulator?

  • did you have a code about sqrt calculator