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

Conversion (unsigned int)double incorrect.

I'm calculate sin by table.

Example fragment code bellow

....
#define Tabl_acos 0x80000000
...
...
double tsin (double arg)
{ double argout;
unsigned int *adr;

adr = (unsigned int *)((unsigned int)Tabl_sin + (unsigned int)fabs(arg)*400); argout = *(double *)adr; return (argout);
}

If I'm write
... + (unsigned int)fabs(arg)*400) -> result correct, but (arg) lose fraction right part and argout lose precition.
If I'm write
... + (unsigned int)(fabs(arg)*400)) -> result incorrect! argout = fantastic value!!!

Parents
  • And so you decided to immediately do some debugging to understand what happens. So you quickly modified your program to call the function with a number of different arguments, and print out the argument, and the result of doing the type cast on fabs() directly or after having multiplied the result of the multiplication with 400. And also considered printing the result of multiplying with 400 inside the fabs() call.

    And what was the result of that test? Did you figure out what did happen, and why? Did you see why parentheses and order of execution matters?

    So - exactly where are you stuck? You never tell us what _you_ think should happen when you perform your typecast operation. You don't even tell us what range your arg parameter has, or the size of your tables, or if you think you use radians, degrees or some other unit for your angles. And you don't tell us if your tables are defined for the full circle or a fraction of the circle or if you expect your code to work even if the angle is more than 360 degrees.

    In the end it's quite good if you sit down and figure out exactly what assumptions you have made and that you document them. And that you then look at your code and see if it really fulfills your assumptions. Right now, the code most definitely doesn't and that's why you get the wrong results. Learning means that you must make sure that you understand your own code - so make sure that you are able to visualize step-by-step what the processor is actually expected to do when it process your two expressions.

Reply
  • And so you decided to immediately do some debugging to understand what happens. So you quickly modified your program to call the function with a number of different arguments, and print out the argument, and the result of doing the type cast on fabs() directly or after having multiplied the result of the multiplication with 400. And also considered printing the result of multiplying with 400 inside the fabs() call.

    And what was the result of that test? Did you figure out what did happen, and why? Did you see why parentheses and order of execution matters?

    So - exactly where are you stuck? You never tell us what _you_ think should happen when you perform your typecast operation. You don't even tell us what range your arg parameter has, or the size of your tables, or if you think you use radians, degrees or some other unit for your angles. And you don't tell us if your tables are defined for the full circle or a fraction of the circle or if you expect your code to work even if the angle is more than 360 degrees.

    In the end it's quite good if you sit down and figure out exactly what assumptions you have made and that you document them. And that you then look at your code and see if it really fulfills your assumptions. Right now, the code most definitely doesn't and that's why you get the wrong results. Learning means that you must make sure that you understand your own code - so make sure that you are able to visualize step-by-step what the processor is actually expected to do when it process your two expressions.

Children