Thread: Casting unsigned long division to a double?

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    Maine
    Posts
    11

    Question Casting unsigned long division to a double?

    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

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    What errors? It shouldn't.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Location
    Maine
    Posts
    11
    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.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Location
    Maine
    Posts
    11
    Works great, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Heap corruption using zlib inflate
    By The Wazaa in forum C++ Programming
    Replies: 0
    Last Post: 03-29-2007, 12:43 PM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Need Help Please
    By YouShallNotPass in forum C++ Programming
    Replies: 3
    Last Post: 08-22-2006, 11:22 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. can someone check this out and let me know ?
    By javaz in forum C Programming
    Replies: 5
    Last Post: 01-21-2002, 02:13 PM