Re: final message

new topic     » goto parent     » topic index » view thread      » older message » newer message
DerekParnell said...
bill said...

intdiv(-8,-3):

It is easy to get distracted here, but this function is working with magnitudes, not envelopes or any other units-of-measure. The UOM question is one that the standard library leaves for the calling code to work with.

The difference between intdiv() and div() is that the intdiv() result is always an integer.

So, div(-8, -3) gives 2.666666666666666... thus intdiv() needs to return this result but as an integer. The discussion is whether it should floor(), ceil(), or round() the result. It could be argued that the behaviour to take would depend on the reason the function is being called, but that is clearly in the domain of the calling code and intdiv() should be agnostic about why it is being called.

Sometimes we might want to know the maximum number of sets needed to contain all the items being divided; in which case ceil() is useful. However, sometimes we might be trying to find the number of complete sets needed; in which case floor() is better. I'm not sure why one would need to round() the div() result but there could be a valid use-case for that.

So, maybe we we need is to have intdiv() accept a third argument, that tells it how to handle the result of div() before returning. I would like to see ceil() be the default behaviour if the third argument was omitted, but that's negotiable.

Now it does get interesting when the arguments have different signs. div(-8, 3) and div(8, -3) give -2.666666666... So what should intdiv() do here? I'm thinking that we just keep the metaphor of intdiv() returning a number of sets in which case, it would always return a non-negative result. In other words, it should just ignore the arguments' signs.

When I think of integer division, I'm reminded of my elementary school adventures in long division, and the answer should be the quotient, discarding the remainder. Looking at it, it's not immediately obvious to me why (or what for) I'd want to use math:intdiv(). This may simply be conceptual blindness due to my familiarity with integer division as common in other programming languages.

Maybe we could have a version that returns { quotient, remainder }.

I would assume that it would use floor() to go from floating point to integers, and that there should be no need for special sign manipulation.

My expectation (just given the name) would be an implementation of:

public function intdiv(object a, object b)	 
	return floor( floor(a)/ floor(b) ) 
end function 

...and then maybe...

public function intdivr(object a, object b)	 
        object c = floor( floor(a)/ floor(b) ) 
	return { c, a - (b*c) } 
end function 

Matt

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu