1. intdiv
- Posted by bill May 12, 2012
- 1563 views
intdiv: namespace math
public function intdiv ( object a , object b )
Return an integral division of two objects.
Parameters:
divisor : any Euphoria object.
Returns:
if either dividend or divisor is a sequence.
Comments:
is divided by divisor.
The result's sign is the same as the dividend's sign.
Example 1:
object MaxPerEnvelope = 5
integer Envelopes = intdiv(Tokens,MaxPerEnvelope) > 21
This function is a crock.
This calculates how many non-empty sets when dividend
is divided by divisor
The result's sign is the same as the dividend's sign
contradiction
If I have -8 oranges and I want to give equal numbers
to -3 people how many do I have left?
intdiv is not calculating non-empty sets.
Neither is it doing integer division.
101 intdiv 5 = 21 101 intdiv -5 = -21 -101 intdiv 5 = -21 -101 intdiv -5 = 21
2. Re: intdiv
- Posted by DerekParnell (admin) May 12, 2012
- 1563 views
...
This function is a crock.
...
intdiv is not calculating non-empty sets.
Neither is it doing integer division.
I think what you are trying to say, in your own wonderful manner, is that the the function has a poor name and it should never return a negative count of non-empty sets.
If that is what you are saying, then I agree.
Integer Division would instead return the integer portion of the quotient, truncated towards zero. Thus
idiv( 10, 3) --> 3 idiv(-10, 3) --> -3 idiv( 10,-3) --> -3 idiv(-10,-3) --> 3
3. Re: intdiv
- Posted by bill May 12, 2012
- 1547 views
No:
What I am saying is this function should be
killed off as it makes no sense.
Secondly: I would say that of the maths functions
. gcd is incorrect,
. intdiv is meaningless,
. mod is incoherently described.
This says the whole of the maths module is suspect.
I don't particularly want to go through it.
Many, many functions rely on the maths module.
4. Re: intdiv
- Posted by jimcbrown (admin) May 12, 2012
- 1524 views
Secondly: I would say that of the maths functions
. gcd is incorrect,
This was fixed. Have you discovered a new problem? If not, please retract your inaccurate statement.
5. Re: intdiv
- Posted by bill May 13, 2012
- 1541 views
Jim,
If gcd has been fixed good.
I saw no bug report.
In any case this does not change the fact that gcd
was wrong until I pointed it out a few days ago.
The fact that gcd has been fixed (in what manner?)
does not change the fact that there are basic errors
in the maths package.
These errors need to be fixed.
6. Re: intdiv
- Posted by DerekParnell (admin) May 13, 2012
- 1518 views
Jim,
If gcd has been fixed good.
I saw no bug report.
In any case this does not change the fact that gcd
was wrong until I pointed it out a few days ago.
The fact that gcd has been fixed (in what manner?)
does not change the fact that there are basic errors
in the maths package.
These errors need to be fixed.
And just to make it a bit less tabloid-like, the only aspect that was wrong with GCD was when either of the operands was zero - not a common occurrence I'd guess.
7. Re: intdiv
- Posted by DerekParnell (admin) May 13, 2012
- 1527 views
No:
What I am saying is this function should be
killed off as it makes no sense.
With the greatest of respect, "it makes no sense" to you. The concept of finding the number of subsets involved when trying to determine the maximum number of subsets containing the same magnitude, is not nonsense to myself.
For example, if a customer buys a 5.8 meter length of tube from me and the customer wants it cut into 900mm lengths, how many lengths of tube will the customer get? The answer is 7; 6 will be 900mm long and one will be 400mm long.
Secondly: I would say that of the maths functions
. gcd is incorrect,
. intdiv is meaningless,
. mod is incoherently described.
This says the whole of the maths module is suspect.
I don't particularly want to go through it.
Many, many functions rely on the maths module.
Well,
- gcd() was incorrect, and then only when either operand was zero.
- intdiv() is poorly named and doesn't handle negatives very well, but its not actually meaningless.
- mod() is incorrectly documented with respect to negative operands, but its functionality is still correct. Remembering that we also have a remainder() function.
Without being too melodramatic, I agree that the math module still needs to undergo vigorous testing and review.
8. Re: intdiv
- Posted by bill May 13, 2012
- 1531 views
Re GCD, a clean method is:
public function gcd(atom p, atom q) -- Both arguments must be positive. p = abs(p) q = abs(q) -- Strip off any fractional part. p = floor(p) q = floor(q) -- Ensure that 'p' is not smaller than 'q' if p < q then atom r = p p = q q = r end if -- Repeat until remainder 0 while q do atom r = remainder(p,q) p = q q = r end while return p end function
It is equivalent to your code barring typos.
GCD(0,x)=x is essential.
It's implicit in the function.
What you say about indiv being useful is correct
in so far as you are dealing with positive numbers.
My point is that applying indiv to negative numbers
is a category error.
The function you want for your tubing example is ceil(x/y).
You still need to ensure it is applied to positive x,y.
integer division:
-9 \ 4 = -2 9 \ 4 = 2Cheers
9. Re: intdiv
- Posted by DerekParnell (admin) May 13, 2012
- 1516 views
Re GCD, a clean method is:
Yes, your version is 'cleaner' to look at and to grok, but mine is almost twice as fast. And that is important for a standard library routine.
What you say about indiv being useful is correct
in so far as you are dealing with positive numbers.
My point is that applying indiv to negative numbers
is a category error.
The function you want for your tubing example is ceil(x/y).
You still need to ensure it is applied to positive x,y.
Currently intdiv is implemented as ...
sign(a)*ceil(abs(a)/abs(b))
so all we have to do is rename it and drop the sign() call.
integer division:
-9 \ 4 = -2 9 \ 4 = 2
Yes, I'll do a library implementation that results in this.
10. Re: intdiv
- Posted by bill May 13, 2012
- 1517 views
I am somewhat surprised abs() should inline to be the same as yours.
perhaps the local definitions in the if and while make a difference.
Very miinor differenes can double the speed.
I have noticed that
if x and y then is about half as fast if x then if y then ...
That said, I prefer legibilty to microseconds for the most part.
Another very minor error:
The range of the integerss is mis-stated
as between -2^30 and 2^30+1.
I tend to check boundary conditions.
11. Re: intdiv
- Posted by mattlewis (admin) May 13, 2012
- 1495 views
This says the whole of the maths module is suspect.
I don't particularly want to go through it.
Many, many functions rely on the maths module.
We've got unit tests set up. Take a look at tests/t_math.e in your euphoria directory (are they shipped with the windows installation?...if not you can see them in the repository). It's always possible (as with GCD) that we've missed some edge cases and haven't got it quite right. But that's the place to see how we're verifying the correctness of each function, and where to add additional edge cases.
I just ran a test coverage analysis. We're definitely testing each function, though there are 20 lines (out of 296) that are never executed. There are also, undoubtedly, more edge cases that aren't exercised. NB: I ran this against current 4.1 source, and I don't think the recent GCD fixes have been merged there yet, so YMMV.
Matt
12. Re: intdiv
- Posted by bill May 13, 2012
- 1502 views
public function powof2(object p) return not (and_bits(p, p-1)) end function constant MAXINT = 1073741823\\ constant MININT = -1073741824 ? powof2(MININT) => 0
Simple error: it fails because and_bits works on
integers and MININT - 1 is not an integer.
13. Re: intdiv
- Posted by mattlewis (admin) May 13, 2012
- 1492 views
public function powof2(object p) return not (and_bits(p, p-1)) end function constant MAXINT = 1073741823\\ constant MININT = -1073741824 ? powof2(MININT) => 0
Simple error: it fails because and_bits works on
integers and MININT - 1 is not an integer.
I'm not following what you're saying. First, which power of 2 is -1073741824?
MININT is 0xC0000000. And MININT-1 = 0xBFFFFFFF. When you bitwise-and those two together, you get 0x80000000, which is not 0, and therefore correctly identifies MININT as not a power of 2.
On the other hand, powof2(-MININT) returns the correct result. Note that -MININT is not an integer.
Matt
14. Re: intdiv
- Posted by bill May 13, 2012
- 1478 views
Manual section 15.2.2 integer range -1,073,741,824 to 1,073,741,823
-1,073,741,824 = -2^30
If this is not MININT then sorry but it is a power of 2.
15. Re: intdiv
- Posted by mattlewis (admin) May 13, 2012
- 1476 views
Manual section 15.2.2 integer range -1,073,741,824 to 1,073,741,823
-1,073,741,824 = -2^30
If this is not MININT then sorry but it is a power of 2.
It's a power of two multiplied by -1. It's a power of two the same way that 6 is (i.e., 21 * 3).
Which is to say, it's not a power of 2.
Matt
16. Re: intdiv
- Posted by DerekParnell (admin) May 13, 2012
- 1469 views
Manual section 15.2.2 integer range -1,073,741,824 to 1,073,741,823
-1,073,741,824 = -2^30
If this is not MININT then sorry but it is a power of 2.
The powof2() function tests if a number is the result of 2 to some power. There is no 'x' such that 2^x results in a negative number. Thus MININT, a negative number, cannot pass this test.
17. Re: intdiv
- Posted by bill May 13, 2012
- 1479 views
OK
I should have picked that up.
But I guess if you say it this number a power of two
one may well be thinking magnitude not sign.
Perhaps the documentation could be clearer.
Euphoria is usually not explicit about the numbers
if uses and it does say integer, atom, sequence
and never precisely says positive.
And bitwise? This function is in among the
bit-operations. Almost seriously mis-leading.
The reverse of the reasoning that went into indiv.
18. Re: intdiv
- Posted by DerekParnell (admin) May 13, 2012
- 1481 views
Perhaps the documentation could be clearer.
Euphoria is usually not explicit about the numbers
if uses and it does say integer, atom, sequence
and never precisely says positive.
And bitwise? This function is in among the
bit-operations. Almost seriously mis-leading.
Ok, I can clear up the documentation.
19. Re: intdiv
- Posted by bill May 13, 2012
- 1463 views
Would it be possible to update the
site to reflect changes to code and
documentation as the changes are made.
Then we would have a canonical
reference. (I do not download nightly
builds as I prefer the stability of
released versions (preferably major
versions).)
20. Re: intdiv
- Posted by DerekParnell (admin) May 14, 2012
- 1390 views
Would it be possible to update the
site to reflect changes to code and
documentation as the changes are made.
Then we would have a canonical
reference. (I do not download nightly
builds as I prefer the stability of
released versions (preferably major
versions).)
Yes, but we aren't going to do that. It would be like releasing a minor update for each change made to the code repository, and that is not a good idea.
- There is quite a deal of work preparing an release (minor or major)
- There is a higher probability of mistakes being released. We need some time to review and thoroughly test each change.
- It could introduce some confusion when trying to provide assistance because it is more likely than not that people will not be using the latest release when reporting errors.
You can download the latest source code changes from the respository, this includes changes to Euphoria, the library and documentation source.
We might consider setting up a beta download area in which one could get the bleeding edge version prior to its official release.