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

long int division

I need a 100% accurate division of any tow 32 bit numbers. So I am using the following code and the (/) operator.
However I notice that results are not accurate when the Den (denominator) is shorter that 32 bit.
For example (98765432/01234567) gives 120 where as the correct answer is 80.
Can anyone tell me why ? Is something in my code incorrect More important, if this is not a bug, how to fix it ?

Incidentally, I cannot use Printf function so I am using a union function to map the long int variables into 4 unsigned char and the Display the char via a serial port.
I don't think that process has introduced any errors in the calculations.

Thanks.

Bhal Tulpule

unsigned long int xdata Num,Den,Res;
Num = 98765432;
Den = 1234567;
Res = Num/Den;
//Display Res;

Parents
  • That's all fantastic, but you'd think you could provide some specific and complete test cases, as part of some experimental method? Providing information about the compiler version, and optimization settings. Enumerate the failing numbers so patterns might be apparent.

    The 8051 compiler likely makes far more compromises, and optimization choices you might want to focus on. Appropriate casting on an 8-bit microprocessor, where default int size is 16-bit, being particularly important.

Reply
  • That's all fantastic, but you'd think you could provide some specific and complete test cases, as part of some experimental method? Providing information about the compiler version, and optimization settings. Enumerate the failing numbers so patterns might be apparent.

    The 8051 compiler likely makes far more compromises, and optimization choices you might want to focus on. Appropriate casting on an 8-bit microprocessor, where default int size is 16-bit, being particularly important.

Children
No data