18

I am looking at the SNMPBEECodec which can be seen at this location
In particular I am looking at the function encodeLength()
A snippet I am interested in

        int numBytes = 0;
        int temp = length;
        while (temp > 0)
        {
            ++numBytes;
            temp = (int)Math.floor(temp / 256);
        }   

(from the Drexel SNMP library).

I would like to know why Math.floor() is used instead of just a simple integer division like temp/256. It seems the simple integer division would give the same result. Or is there a technical difference?

sleske
  • 81,358
  • 34
  • 189
  • 227
bobby
  • 2,629
  • 5
  • 30
  • 56
  • 4
    @EJP: No, in general there may be a good technical reason for such code. Understanding this reason can be very interesting. However, this is not such a case. However, you only find out by asking if you don't know yourself. – sleske Jan 22 '13 at 08:57
  • 3
    They are not strictly equivalent: http://stackoverflow.com/questions/10457208/java-how-do-i-perform-integer-division-that-rounds-towards-infinity-rather-tha However since temp is > 0 in your example it is equivalent. – assylias Jan 22 '13 at 09:00
  • The reason I asked this was because the Drexel library is a pretty popular one for SNMP. I wanted to know if there was something obvious I was missing out. Seems to be no – bobby Jan 22 '13 at 09:00
  • @slekse I don't know what you're disagreeing with. I didn't say there wasn't a reason. I said he's asking in the wrong place. There may indeed be a good technical reason, but only the author actually knows why he wrote it. Anything else is just guesswork. I personally don't know why he even wrote a division when it looks like a shift would have done. Maybe there's a reason, maybe not. Not constructive. – user207421 Jan 22 '13 at 09:01
  • 2
    @EJP I think the question really is: are they equivalent amd if they are which is "better". – assylias Jan 22 '13 at 09:03
  • @assylias If that's really the question, he should have asked it that way. I am dealing the text of what he actually wrote. Rather than just guessing about it. – user207421 Jan 22 '13 at 09:05
  • 3
    @EJP: Good point. I edited the question to try and make it clearer, because I think it's a valid question. Hope that helps. – sleske Jan 22 '13 at 09:08
  • @assylias: Are you sure they're not strictly equivalent? The question you linked to concerns the difference between division that rounds towards zero and division that rounds towards -infinity. But this question is about the difference between `Math.floor(temp / 256)` and just `temp / 256`. As sleske pointed out, `temp / 256` already yields an integer, so passing it to `Math.floor()` does nothing. – LarsH Jul 17 '18 at 16:31
  • @LarsH You're right - I may have thought that temp was a double - but being an int `floor` is indeed superfluous. – assylias Jul 20 '18 at 11:01

2 Answers2

23

To answer the technical part of your question:

Using math.floor() is superfluous: temp / 256 is an integer (by Java's rules for integer arithmetic), and using Math.floor() on an integer is pointless. You could simply use temp / 256.

Why the author did this is impossible to answer without reading their mind. The author might simply have been confused about the behaviour of division in Java, and decided to "play it safe" - but that is just speculation.

mavrosxristoforos
  • 3,573
  • 2
  • 25
  • 40
sleske
  • 81,358
  • 34
  • 189
  • 227
22

Well, unfortunately the author can no longer read his mind either - it's been about 12 years since I wrote this, so I forget the reason I didn't just use integer division. Some thoughts: I use integer division elsewhere assuming the usual behavior, so it wouldn't likely have been basic confusion on the rules for integer division in Java; it's possible (though unlikely) that I was at some point using a non-integral data type for the argument, and didn't get rid of the superfluous floor() when I changed; or perhaps (more likely) I was at some point attempting to round up rather than down while developing the algorithm, using ceil() as a cheap (= fewer characters) way to do this, and just reflexively switched to floor() when I changed.

So unfortunately the real reason is lost in the mists of time... but I agree, the floor() is superfluous. I should really post the code on Github or the like so folks can improve and evolve it. \ Jon

jsevy
  • 321
  • 1
  • 4
  • Is it possible you were trying to implement floored division? It's different for negative numbers: `-1` floor division `8` is `-1` instead of `0`. If you had written `256.0` instead of `256` then you would've had a working floor division. – Aaron Franke Apr 11 '18 at 01:11