Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. conversion from 'double' to 'unsigned int',

conversion from 'double' to 'unsigned int',

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ariesaloksingh
    wrote on last edited by
    #1

    Hi i want to know how can i convert a "double value" to "unsigned int" without possible loss of data. unsigned int numr; double num1; numr= fread (buff,sizeof(char),num1,pFile); alok singh chauhan

    H S J R N 6 Replies Last reply
    0
    • A ariesaloksingh

      Hi i want to know how can i convert a "double value" to "unsigned int" without possible loss of data. unsigned int numr; double num1; numr= fread (buff,sizeof(char),num1,pFile); alok singh chauhan

      H Offline
      H Offline
      Hamid Taebi
      wrote on last edited by
      #2

      http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_data_conversion.asp[^] maybe it is some helpful to you

      1 Reply Last reply
      0
      • A ariesaloksingh

        Hi i want to know how can i convert a "double value" to "unsigned int" without possible loss of data. unsigned int numr; double num1; numr= fread (buff,sizeof(char),num1,pFile); alok singh chauhan

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #3

        It's impossible. For example, 1.5 can't be converted an integer without loss of data. 1 or 2 are the two closest but we lose .5 either way. I'm not sure what the code you quoted is trying to do?..... Steve

        1 Reply Last reply
        0
        • A ariesaloksingh

          Hi i want to know how can i convert a "double value" to "unsigned int" without possible loss of data. unsigned int numr; double num1; numr= fread (buff,sizeof(char),num1,pFile); alok singh chauhan

          J Offline
          J Offline
          Johann Gerell
          wrote on last edited by
          #4

          Are you serious? 1. A double might contain more bits than an unsigned integer. Hence conversion loss. 2. A double can have decimal places, which an unsigned integer lacks. Hence conversion loss. -- The Blog: Bits and Pieces

          1 Reply Last reply
          0
          • A ariesaloksingh

            Hi i want to know how can i convert a "double value" to "unsigned int" without possible loss of data. unsigned int numr; double num1; numr= fread (buff,sizeof(char),num1,pFile); alok singh chauhan

            R Offline
            R Offline
            rwilmink
            wrote on last edited by
            #5

            Hi, your double num1 contains the number of chars you want to read from pFile. Since you always want to read a whole number of chars, change double num1 to int num1, and your problem is over. Ronald.

            1 Reply Last reply
            0
            • A ariesaloksingh

              Hi i want to know how can i convert a "double value" to "unsigned int" without possible loss of data. unsigned int numr; double num1; numr= fread (buff,sizeof(char),num1,pFile); alok singh chauhan

              N Offline
              N Offline
              normanS
              wrote on last edited by
              #6

              The answer, quite simply, is that you can not in general convert from double (or float) to unsigned int with no data loss! Because double is a floating point format, it might contain a fractional part, which will be discarded in any conversion. And a double might be very large or very small (because it allows powers of 10.) If the double represents an integer in the range supported by an unsigned int, then no conversion is necessary - it can be stored. Lastly, if the double represents something like degrees, with resolution of 0.01 degree and range of 0 to 359.99 degree, you could scale the value - multiply the double by 100.0, add 0.1, then store it in the unsigned int with no data loss.

              1 Reply Last reply
              0
              • A ariesaloksingh

                Hi i want to know how can i convert a "double value" to "unsigned int" without possible loss of data. unsigned int numr; double num1; numr= fread (buff,sizeof(char),num1,pFile); alok singh chauhan

                stefanmihaimogaS Online
                stefanmihaimogaS Online
                stefanmihaimoga
                wrote on last edited by
                #7

                Well, a 'double' is 8 bytes length and a 'int' is 4 bytes length. So, i think, it's impossible to do that. What's with your example?

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups