Possible Overflow?
-
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
-
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
Have you looked at the datatype of Mininum, Maximum, and Value properties of the ProgressBar control? Notice they're
int
's and notdouble
's? What's the range of values anint
ger 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 anint
.A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
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
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]
-
Have you looked at the datatype of Mininum, Maximum, and Value properties of the ProgressBar control? Notice they're
int
's and notdouble
's? What's the range of values anint
ger 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 anint
.A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007Thanks 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%)