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#
  4. Possible Overflow?

Possible Overflow?

Scheduled Pinned Locked Moved C#
helpquestion
4 Posts 3 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.
  • S Offline
    S Offline
    StevenWalsh
    wrote on last edited by
    #1

    I wrote an application that is copying a file. I'm also writing a progress bar function for it. The function works fine until what should be 6%, then it starts displaying a negative value. This is the line where the problem is occurring: double Completed = BufferSize * (Chunks - 1); some background: BufferSize = 524288000 (Chunks - 1) = 6 the result i would expect is: 3145728000 however it is displaying: -1149239296 i would assume the double datatype should be large enough. I tried to explicatly cast it as a double as well however no luck there. Please Help!! Thank you, Steve

    D L 2 Replies Last reply
    0
    • S StevenWalsh

      I wrote an application that is copying a file. I'm also writing a progress bar function for it. The function works fine until what should be 6%, then it starts displaying a negative value. This is the line where the problem is occurring: double Completed = BufferSize * (Chunks - 1); some background: BufferSize = 524288000 (Chunks - 1) = 6 the result i would expect is: 3145728000 however it is displaying: -1149239296 i would assume the double datatype should be large enough. I tried to explicatly cast it as a double as well however no luck there. Please Help!! Thank you, Steve

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Have you looked at the datatype of Mininum, Maximum, and Value properties of the ProgressBar control? Notice they're int's and not double's? What's the range of values an intger can hold? -2,147,483,648 to 2,147,483,647, inclusive. There's your problem. When your value reaches the upper limit of an integer it wraps around to the negative side, hence the negative number. Your going to have to scale your values to fit inside the limits of an int.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      S 1 Reply Last reply
      0
      • S StevenWalsh

        I wrote an application that is copying a file. I'm also writing a progress bar function for it. The function works fine until what should be 6%, then it starts displaying a negative value. This is the line where the problem is occurring: double Completed = BufferSize * (Chunks - 1); some background: BufferSize = 524288000 (Chunks - 1) = 6 the result i would expect is: 3145728000 however it is displaying: -1149239296 i would assume the double datatype should be large enough. I tried to explicatly cast it as a double as well however no luck there. Please Help!! Thank you, Steve

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, when you do an assignment such as destinationVariable = someExpression then the type of the destinationVariable is irrelevant while someExpression is evaluated. So when e.g. someExpression consists of only integers, it will be calculated using only integers (with possible overflow); it is only when the assignment itself is going to happen that possibly a conversion (up-casting) will happen. BTW: same is true for most programming languages (C, C++, Java, ...). If the expression's type is insufficient to evaluate correctly, you must make sure a more capable type is used (e.g. by using wider constants such as 1.0 which causes part of the expression at least to use doubles, by using a long/float/double variable, or - at the right place - inserting a long/float/double cast). :)

        Luc Pattyn [My Articles] [Forum Guidelines]

        1 Reply Last reply
        0
        • D Dave Kreskowiak

          Have you looked at the datatype of Mininum, Maximum, and Value properties of the ProgressBar control? Notice they're int's and not double's? What's the range of values an intger can hold? -2,147,483,648 to 2,147,483,647, inclusive. There's your problem. When your value reaches the upper limit of an integer it wraps around to the negative side, hence the negative number. Your going to have to scale your values to fit inside the limits of an int.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007

          S Offline
          S Offline
          StevenWalsh
          wrote on last edited by
          #4

          Thanks for the reply. I actually figured this one out before you replied, but your reply sounds like a more permanent solution then what i did.. (i used the decimal data type, which worked until 100%)

          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