jim gotti

Ranch Hand

Posts: 36

posted 13 years ago

Im currently starting from scratch to refresh some java having been away for a while.

i was reading over HWS javanotes and came across this blurb, maybe someing can clear it up for me.

i was reading over HWS javanotes and came across this blurb, maybe someing can clear it up for me.

If N is an integer variable, then N/100 is an integer, and 1/N is equal to zero for any N greater than one! This fact is a common source of programming errors. You can force the computer to compute a real number as the answer by making one of the operands real: For example, when the computer evaluates 1.0/N, it first converts N to a real number in order to match the type of 1.0, so you get a real number as the answer.

why is 1/N equal to 0 if N > 1? ;where N is an int?

Mike Gershman

Ranch Hand

Posts: 1272

Oneal Shaha

Ranch Hand

Posts: 98

posted 13 years ago

The simple Logic behind it is that whenever you perform any operation like X/Y where X and Y both are integers, and if the answer is not a perfect interger then simple the digits after the decimal point

so if answer of 5/2 is 2.50 then .50 is removed and 2 is given as a answer in interger format.

In case 1/N if N>1 then the real answer will always be 0.#### In this case all ####s are removed and 0 as a simple interger is returned as answer.

**.**are removed.so if answer of 5/2 is 2.50 then .50 is removed and 2 is given as a answer in interger format.

In case 1/N if N>1 then the real answer will always be 0.#### In this case all ####s are removed and 0 as a simple interger is returned as answer.

-Oneal

posted 13 years ago

Remember in gradeschool, when you first learned how to divide (or at least, this is how I learned to do it)?

you would do something like

so 23 divided by 5 is 4, with a remainder of 3.

integer division does exactly this, then just throws the remainder away.

so when you do 1 / 1, you get 1 remainder 0.

when you do 1 / 2, you get 0, remainder 1...remainder thrown away, therefore you get 0.

1 / 3 is 0 remainder 1, throw away, 0... etc.

you would do something like

so 23 divided by 5 is 4, with a remainder of 3.

integer division does exactly this, then just throws the remainder away.

so when you do 1 / 1, you get 1 remainder 0.

when you do 1 / 2, you get 0, remainder 1...remainder thrown away, therefore you get 0.

1 / 3 is 0 remainder 1, throw away, 0... etc.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors