If I have two unsigned long integers and I want to divide them and store the result as a double, how might this be accomplished? The following presents errors.
Code:unsigned long x, y; double percent = (double) x/y
If I have two unsigned long integers and I want to divide them and store the result as a double, how might this be accomplished? The following presents errors.
Code:unsigned long x, y; double percent = (double) x/y
It actually doesn't give errors, it gives a warnings. I am doing this operation within a kernel module. One error is something like __divfi or something. Is there some problem with casting an unsigned long int to a double? The kernel module compiles with warnings, but will not install.
You would probably receive more useful advise if you gave some specifics (eg what compiler? any non-default compiler settings? what target system? what is the actual text of the warnings?).
Converting from an integral type can to a floating point type can lose information if the floating point type can't represent every integral value that can be stored in the integral type. I wouldn't normally expect that (I'd be more likely to expect it if you were converting from a unsigned long to a float, as those types are the same size on a lot of machines). You've also done an explicit conversion, which normally has a side effect of telling the compiler not to complain about suspicious conversions.
> One error is something like __divfi or something
Gotta love the lack of precision in reporting problems.
Yeah, "something like" is really gonna narrow down the problem.
I'm gonna guess it's an unresolved symbol because it's invoked a function to do some part of the work.
Perhaps re-express your intentions as say
int percent = (x*100)/y;
Or maybe study the compiler options to cause it to generate more inline code rather than relying on standard library code.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Works great, thanks.