Jump to content

[JAVA] Division always results in 0?

FakezZ
Go to solution Solved by Guest,

percentage = ((float)i/numOfRuns)*100;

So here's the code:

for (int i = 0; i < numOfRuns; i++)		{			a = (double) i;			x += i;			percentage = (i / numOfRuns) * 100;			System.out.println(percentage + "% completed");		}

How can the percentage always be 0? numOfRuns in this scenario is 900000, if it matters...

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

Congratz u just divided by 0 . Jk i don't know anything about programming

Link to comment
Share on other sites

Link to post
Share on other sites

Congratz u just divided by 0 . Jk i don't know anything about programming

Lel... If I did, it would just throw an exception xD. And I was like: AN ANSWER? SO FAST? WUT????

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

Congratz u just divided by 0 . Jk i don't know anything about programming

Then don't post.

 

Use % instead of / -  / is "divides evenly", % is divides into.

Link to comment
Share on other sites

Link to post
Share on other sites

Then don't post.

 

Use % instead of / -  / is "divides evenly", % is divides into.

Don't be anti-joke chicken , if we don't have fun why do we live ?

Link to comment
Share on other sites

Link to post
Share on other sites

percentage = ((float)i/numOfRuns)*100;

Link to comment
Share on other sites

Link to post
Share on other sites

Then don't post.

 

Use % instead of / -  / is "divides evenly", % is divides into.

Tried it, it just gives random numbers :/

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

Tried it, it just gives random numbers :/

/ is divide, % is remainder/modulus, your problem is i / numOfRuns is truncating so will always be equal to 0 as it will always be a fraction, AleksaNS solution will fix it.

Link to comment
Share on other sites

Link to post
Share on other sites

Try (i * 100)/ numOfRuns

instead of (i / numOfRuns) * 100

 

That prevents the rounding error that you have from occuring.

You see, (i / numOfRuns) is less than 1 and java rounds this to 0 before multiplying by 100.

 

EDIT: Changed around one of the parentheses to better illustrate what I meant but you don't really need them.

I cannot be held responsible for any bad advice given.

I've no idea why the world is afraid of 3D-printed guns when clearly 3D-printed crossbows would be more practical for now.

My rig: The StealthRay. Plans for a newer, better version of its mufflers are already being made.

Link to comment
Share on other sites

Link to post
Share on other sites

percentage = ((float)i/numOfRuns)*100;

and it will still not give you percentage.

I solved it by writing ((float) i / (float) numOfRuns), cause it would give crazy big numbers otherwise. Anyway, it works now, thanks! :D

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

Use % instead of / -  / is "divides evenly", % is divides into.

Modulus (%) is remainder. http://www.cafeaulait.org/course/week2/15.html

 

If i and numOfRuns in this code are integers, then you're doing integer division which gets rid of the decimal part.

percentage = (i / numOfRuns) * 100;

One of them needs to be a float/double. You could cast with (float) or (double) like @AleksaNS suggested, or just use the a variable you're setting for some reason.

percentage = (a / numOfRuns) * 100;// orpercentage = ((float) i / numOfRuns) * 100;percentage = ((double) i / numOfRuns) * 100;
Link to comment
Share on other sites

Link to post
Share on other sites

I solved it by writing ((float) i / (float) numOfRuns), cause it would give crazy big numbers otherwise. Anyway, it works now, thanks! :D

That works too but I advise you to try my way too, if you can. It's good programming practice to multiply before you divide when you do both, to prevent rounding errors.

I cannot be held responsible for any bad advice given.

I've no idea why the world is afraid of 3D-printed guns when clearly 3D-printed crossbows would be more practical for now.

My rig: The StealthRay. Plans for a newer, better version of its mufflers are already being made.

Link to comment
Share on other sites

Link to post
Share on other sites

 

Modulus (%) is remainder. http://www.cafeaulait.org/course/week2/15.html

 

If i and numOfRuns in this code are integers, then you're doing integer division which gets rid of the decimal part.

percentage = (i / numOfRuns) * 100;

One of them needs to be a float/double. You could cast with (float) or (double) like @AleksaNS suggested, or just use the a variable you're setting for some reason.

percentage = (a / numOfRuns) * 100;// orpercentage = ((float) i / numOfRuns) * 100;percentage = ((double) i / numOfRuns) * 100;

Yeah I tried quite a few things, but they didn't seem to work (possibly some mistake on my part). Now it works perfectly :D

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

That works too but I advise you to try my way too, if you can. It's good programming practice to multiply before you divide when you do both, to prevent rounding errors.

That still doesn't work if it's integer division. Still need to work with float/double

Link to comment
Share on other sites

Link to post
Share on other sites

That still doesn't work if it's integer division. Still need to work with float/double

It's only to 1% accurate but it does work (and for things that don't take too long that's really all the user really wants to know). Just multiply before you divide. Trust me, I've programmed before.

I cannot be held responsible for any bad advice given.

I've no idea why the world is afraid of 3D-printed guns when clearly 3D-printed crossbows would be more practical for now.

My rig: The StealthRay. Plans for a newer, better version of its mufflers are already being made.

Link to comment
Share on other sites

Link to post
Share on other sites

It's only to 1% accurate but it does work (and for things that don't take too long that's really all the user really wants to know). Just multiply before you divide. Trust me, I've programmed before.

Sure it's valid if you want to truncate the decimal place and not round up when required. Depends on the use and accuracy needed.

Link to comment
Share on other sites

Link to post
Share on other sites

Sure it's valid if you want to truncate the decimal place and not round up when required.

It's not entirely accurate I know, but does it really need to be? For some applications it probably does, but doing it without casting is computationally less expensive. Your way is entirely correct, I know. More correct than mine. I almost proposed the same solution as you did but decided not to because I wanted to show OP (@FakezZ) that when performing integer calculations it's better to multiply first and then divide instead of doing it the other way round. For floats this does not matter this much, but for ints it just does.

I cannot be held responsible for any bad advice given.

I've no idea why the world is afraid of 3D-printed guns when clearly 3D-printed crossbows would be more practical for now.

My rig: The StealthRay. Plans for a newer, better version of its mufflers are already being made.

Link to comment
Share on other sites

Link to post
Share on other sites

It's not entirely accurate I know, but does it really need to be? For some applications it probably does, but doing it without casting is computationally less expensive. Your way is entirely correct, I know. More correct than mine. I almost proposed the same solution as you did but decided not to because I wanted to show OP (@FakezZ) that when performing integer calculations it's better to multiply first and then divide instead of doing it the other way round. For floats this does not matter this much, but for ints it just does.

I agree

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×