|
What is the datatype of the variables used? Try using a double, this may solve your problem...
|
|
|
|
|
both are type of int .
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
Ah, you see.. int cannot contain decimals... all the devisions result (rounded) in -12.. If you change the data types to double (or decimal if you like) the problem should be solved...
|
|
|
|
|
okay, I compared the other code with this one and found I was using long in there and I changed this to long as well and it working nice
System.Diagnostics.Debug.WriteLine(bytesReaded + " * 100 / " + totalBytesToRead + " = " + (((long)bytesReaded * 100) / (long)totalBytesToRead));
but why ?? whats wrong with int
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
You could still overflow the longs someday.
|
|
|
|
|
Probably better to do the division and then the multiplication.
Edit: Yeah, that won't work with ints.
modified on Thursday, November 12, 2009 11:34 PM
|
|
|
|
|
you mean
(bytesReaded / totalBytesToRead) * 100
??
well, I'm not sure it will work a bit accurate...
>> (50 / 100) * 100
>> 0 * 100
>> 0
or
>> (50 * 100) / 100
>> 5000 / 100
>> 50
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
Just try it.
modified on Thursday, November 12, 2009 12:30 PM
|
|
|
|
|
you're a binary kind of guy then:
progress = "not done at all"
progress = "not done at all"
progress = "not done at all"
...
progress = "not done at all"
progress = "done"
In VB the intermediate quotient would be a real number; other languages would stay with integers, hence the quotient is either 0 or 1.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
Oh, right...
So use floats, but I still stand behind doing the division first to avoid overflow errors.
|
|
|
|
|
PIEBALDconsult wrote: doing the division first to avoid overflow errors
overflow is very unlikely when using real numbers (most certainly when the result is to be a progress percentage).
my approach is to divide both numerator and denominator by some number; the obvious choice here is by 100.
try it!
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
Real numbers stored in what datatype?
Why not divide just the denominator by 100? (Unless you have a very small denominator.)
Or, find the Greatest Common Factor and reduce!
|
|
|
|
|
PIEBALDconsult wrote: Why not divide just the denominator by 100?
that's what I recommended.
PIEBALDconsult wrote: reduce!
That is costly and may not help at all, as you well know.
Next you're gonna calculate prime numbers first...
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
Luc Pattyn wrote: that's what I recommended
You said divide both.
Luc Pattyn wrote: calculate prime numbers
Oh, yeah, I meant to include that: first determine that neither is a prime, then determine the GCF...
|
|
|
|
|
when what you need is the value of N/D where N and/or D is a product, it helps to divide both N and D by some rather small value X.
in the current situation N is a product of a variable and a constant (N = bytesRead * 100), hence that constant 100 is the first candidate for X.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
Hmmm... so (N/100)*100/(D/100) or (N/100)/(D/100)/100 ?
Maybe the OP should look into a checked scope...
|
|
|
|
|
"divide both N and D by some rather small value X" is the math recipe.
Always think in the problem domain, then come up with the matching codes.
Applied on bytesReaded*100 with X=100 it means you don't need the multiplication in the numerator, as again you well know.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
As the poster before said use an other type or do the div before the mul, because your program calculates right.
36257792 * 100 = 3.625.779.200 (int can only hold 2.1 billion!)
-> 3.625.779.200 -> cast to int =~ -669188096
-669188096 / 54444964 = -12,29109263...
cast to int = -12
so your program is totally right!
Greetings
Covean
|
|
|
|
|
Yes I think I should use double, because if I div before mul then it may not show any progress at all if file size is small like 100 bytes. Anyway thanks for that info...
thanks others too, Eduard Keilholz and PIEBALDconsult
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
Xmen W.K. wrote: (bytesReaded * 100) / totalBytesToRead
when bytesReaded is of type int or less, the expression bytesReaded * 100 will be evaluated as int, which means the largest value it can have is Int32.MaxValue = some 2 billion. So for whenever bytesReaded exceeds some 20 million, the multiplication will overflow, resulting in a wrong value with a 50% probability of being negative.
Since you only want to know a percentage, the easiest solution is to use bytesReaded / (totalBytesToRead/100) , however this expression will throw whenever totalBytesToRead<100;
a very good approximation would be bytesReaded / (1+totalBytesToRead/100)
Keep in mind my approximations may never reach 100 as the division will round down.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
modified on Thursday, November 12, 2009 11:07 AM
|
|
|
|
|
Oh, I hadn't seen this reply of yours until now.
I would add that, if (1+totalBytesToRead/100) doesn't vary, it can be calculated once rather than repeatedly:
int divisor = totalBytesToRead/100 ;
if ( divisor == 0 ) divisor = 1 ;
But as long as you're making a local variable to hold it, why not use a float (only two decimal places are required) and be done with it?
float divisor = totalBytesToRead/100.0F ;
|
|
|
|
|
PIEBALDconsult wrote: why not use a float
because I am always hesitant to introduce reals when the end result I'm after is an integer.
But it would be all right here.
And now this begs the question: why is BackgroundWorker.ReportProgress() taking an int, as progress really is something that goes from 0.0 to 1.0?
and why is ScrollBar.Value an int? One often regrets it isn't either long or some real.
Maybe they too don't want to force reals on you when your app is all integer.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
No idea. I also want NumericUpDown to be available with different types: NumericUpDown<int> or something.
Luc Pattyn wrote: progress really is something that goes from 0.0 to 1.0?
Well, can't it also go 0 to 54444964 ? And let .net handle the math?
|
|
|
|
|
You are the only one who hit the nail in its head
|
|
|
|
|
I'm a hammer at times.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|