Generics are difficult. Either you end up defining special types with generic operations on them (a la pkg/sort) or you end up reflecting on yourself and burning a lot of performance and adding complexity.
At the worst case, all of them could be implemented and you have a more verbose library.
I don't think there's any need to wait for extensions to be added to
the standard library. These could be implemented as independent
packages by the community and when they reach a good level of maturity
then the golang maintainers could consider bringing them into the
standard library.
You have some good ideas, there's no need to wait, just start coding
and see if others like the ideas too :)
Ian
However, in many cases all calculations are going to be using the same type: uint64 was the ideal type for my pixel-manipulations of image data, for example. Repeatedly casting to and from float64 for every pixel in the image would provide quite a lot of unnecessary overhead and unnecessary code. (Also, this is semi-related to the discussion here, which complains about the lack of implicit casting. I'm glad Go doesn't do this, but I do agree it's feels wasteful to repeatedly cast to and from float64 all the time)
Most processors have a floating point exp operation, but do not have an integer exp operation.
Or is there source file with assembly specific optimisations that I've forgotten to notice?
You're looking at math.exp (unexported) not math.Exp. The former is there for a fallback for systems where there isn't a faster version in assembly.I don't know what "integer option" you are referring to on your 32bit netbook. I don't claim to be an expert on the ever-changing x86 instruction set, but I'm pretty sure there is no integer exponentiation instruction still. (And as the assembly from John Asmuth points out to me, there isn't a floating point exp instruction either, so my previous argument is likely incorrect.)
Perhaps :)
Given that, I suspect the fastest method is indeed to do three floating point conversions and one floating point exp, rather than hand-roll an integer exponentiation by squaring.
So if the issue is only efficiency, use the math library. If the issue is typing speed:func e(x y, int) int {u64(math.Pow(float64(x), float64(y)))}
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
func power(x,y,p int64) int64 { var res int64 res = 1; // Initialize result x =(x%p)%mod; // Update x if it is more than or // equal to p for y>0{ // If y is odd, multiply x with result if y & 1==1{ res = ((res*x)%p)%mod; } // y must be even now y = y/2; x = ((x*x)%p)%mod; } return res; }