Hi,
Can anyone send me a c code to divide 2 64bit numbers. My compiler only supports 32/32 division.
Thanx & Regards
Mani
Hi,
Can anyone send me a c code to divide 2 64bit numbers. My compiler only supports 32/32 division.
Thanx & Regards
Mani
The code is available in linux, see div64.c. Can you copy that?
Are you positive your compiler doesn't support 64-bit division? If your C compiler supports C99, this should work:
#include <stdint.h>
#include <stdio.h>
int main(void)
{
int64_t numerator = 123;
int64_t denominator = 10;
int64_t quotient = numerator / denominator
printf("%" PRId64 " / %" PRId64 " = %" PRId64 "\n",
numerator, denominator, quotient);
return 0;
}
A more general idea is to use a multi-precision library, like GMP.
GMP is a free library for arbitrary precision arithmetic, operating on signed integers, rational numbers, and floating point numbers. There is no practical limit to the precision except the ones implied by the available memory in the machine GMP runs on. GMP has a rich set of functions, and the functions have a regular interface.
Floating point division is handled with void mpf_div (mpf_t rop, mpf_t op1, mpf_t op2)
More than likely, the division limitation stems from the fact that you're compiling for 32-bit instead of 64-bit.
I don't recall seeing an integer division instruction that handles 64 bit for x86. It would multiply 2 32-bit integers, and split the results across 2 registers, but not division as far as I recall..