Why is Math.floor() used instead of integer division in BER Codec
Asked Answered
S

2

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?

Symbolism answered 22/1, 2013 at 8:51 Comment(9)
@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.Logogram
They are not strictly equivalent: #10457708 However since temp is > 0 in your example it is equivalent.Beaverette
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 noSymbolism
@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.Jodijodie
@EJP I think the question really is: are they equivalent amd if they are which is "better".Beaverette
@Beaverette 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.Jodijodie
@EJP: Good point. I edited the question to try and make it clearer, because I think it's a valid question. Hope that helps.Logogram
@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.Vida
@Vida You're right - I may have thought that temp was a double - but being an int floor is indeed superfluous.Beaverette
L
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.

Logogram answered 22/1, 2013 at 15:11 Comment(0)
C
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

Cons answered 26/1, 2013 at 16:9 Comment(1)
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.Varix

© 2022 - 2024 — McMap. All rights reserved.