Re: [go-nuts] Mathematical operations - the generics way

2022-03-25 Thread Paul Hankin
I don't know if it's important to you, but your Max[float64] isn't 
compatible with math.Max in the standard library.
For example, Max[float64](NaN, 1) returns 1 rather than NaN.

On Tuesday, 22 March 2022 at 16:58:40 UTC+1 esi...@gmail.com wrote:

> I found a working version meantime.
>
> // Max returns the bigger value between two numbers.
> func Max[T constraints.Ordered](x, y T) T {
> if x > y {
> return x
> }
> return y
> }
>
> // Abs returns the absolut value of x.
> func Abs[T constraints.Signed | constraints.Float](x T) T {
> if x < 0 {
> return -x
> }
> return x
> }
>
> On Saturday, March 19, 2022 at 12:22:39 PM UTC+2 Gergely Födémesi wrote:
>
>> some details: https://github.com/golang/go/issues/48918 
>>
>> On 3/18/22, Endre Simo  wrote: 
>> > Now that generics are officially supported, I was checking in the Go 
>> source 
>> > 
>> > if there are some generic implementation of utility methods like Abs, 
>> Min, 
>> > Max etc, but I couldn't find it other than this 
>> > proposal https://github.com/golang/go/discussions/48287. 
>> > 
>> > Anyone knows if something related has been adopted and implemented in 
>> Go 
>> > 1.18? 
>> > 
>> > -- 
>> > 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. 
>> > To view this discussion on the web visit 
>> > 
>> https://groups.google.com/d/msgid/golang-nuts/e930b89a-0ba6-46f5-b3ec-7aeb26d8acf9n%40googlegroups.com.
>>  
>>
>> > 
>>
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/5a96dccf-2e52-4172-bd2f-e1bf0e8757b4n%40googlegroups.com.


[go-nuts] Re: modules and my v2 package only partly appearing on go.dev

2020-02-29 Thread Paul Hankin
On Friday, 28 February 2020 20:43:16 UTC+1, Paul Hankin wrote:
>
> I am rather confused about modules, and have trouble making them work.
>
> I have a package: https://github.com/paulhankin/poker
>
> The go.mod looks like this: `
> module github.com/paulhankin/poker/v2
>
> go 1.13
> `
>
> The package has a v1, and v2. At v2, I moved the main part of the package 
> into a subdirectory "poker" so the import would be "
> github.com/paulhankin/poker/v2/poker" rather than "
> github.com/paulhankin/poker/v2".
>
> I can successfully import the v2 package in a new modules-aware project, 
> and goproxy seems to pick up new versions:
>
> $ go list -m --versions
> github.com/paulhankin/poker/v2 v2.0.0 v2.0.2 v2.0.3 v2.0.4 v2.0.5 v2.0.6 
> v2.0.7
>
>
> I can't find the documentation on go.dev though. The closest I can find 
> is this:
> https://pkg.go.dev/github.com/paulhankin/poker/v2
> which contains the cmd subdirectory, but not the poker subdirectory.
>
> Am I doing something wrong? Did I make a mistake in moving the package 
> into a subdirectory, or is there an easier way to avoid the import name 
> being "v2"?
>
> I'd file a bug against go.dev if I thought it was broken, but I find 
> modules so complicated that I don't know if it's my understanding that's 
> wrong, or if I made a mistake, or if go.dev isn't working correctly. I'm 
> pretty frustrated with the experience so far.
>

Just to finish up here, go.dev now contains the documentation for the 
package. I guess I had to wait for the v2.0.0 version to disappear from 
some cache.

-- 
Paul Hankin

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/6d2efc59-4f51-4a24-848a-416a3866c5c1%40googlegroups.com.


[go-nuts] Re: modules and my v2 package only partly appearing on go.dev

2020-02-28 Thread Paul Hankin
On Friday, 28 February 2020 21:04:04 UTC+1, Paul Hankin wrote:
>
> On Friday, 28 February 2020 20:43:16 UTC+1, Paul Hankin wrote:
>>
>> I am rather confused about modules, and have trouble making them work.
>>
>> I have a package: https://github.com/paulhankin/poker
>>
>> The go.mod looks like this: `
>> module github.com/paulhankin/poker/v2
>>
>> go 1.13
>> `
>>
>> The package has a v1, and v2. At v2, I moved the main part of the package 
>> into a subdirectory "poker" so the import would be "
>> github.com/paulhankin/poker/v2/poker" rather than "
>> github.com/paulhankin/poker/v2".
>>
>> I can successfully import the v2 package in a new modules-aware project, 
>> and goproxy seems to pick up new versions:
>>
>> $ go list -m --versions
>> github.com/paulhankin/poker/v2 v2.0.0 v2.0.2 v2.0.3 v2.0.4 v2.0.5 v2.0.6 
>> v2.0.7
>>
>>
>> I can't find the documentation on go.dev though. The closest I can find 
>> is this:
>> https://pkg.go.dev/github.com/paulhankin/poker/v2
>> which contains the cmd subdirectory, but not the poker subdirectory.
>>
>> Am I doing something wrong? Did I make a mistake in moving the package 
>> into a subdirectory, or is there an easier way to avoid the import name 
>> being "v2"?
>>
>> I'd file a bug against go.dev if I thought it was broken, but I find 
>> modules so complicated that I don't know if it's my understanding that's 
>> wrong, or if I made a mistake, or if go.dev isn't working correctly. I'm 
>> pretty frustrated with the experience so far.
>>
>
> Oh, I see the problem. I created the subdirectory after tagging v2.0.0.
>

I deleted the tag, and go.dev has removed the v2.0.0 release, but the 
package /poker is still not showing up on 
https://pkg.go.dev/mod/github.com/paulhankin/poker/v2?tab=packages

Can someone please help?

-- 
Paul Hankin 

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/c1ecf162-dd8e-4262-9a66-3c400dcafd60%40googlegroups.com.


[go-nuts] Re: modules and my v2 package only partly appearing on go.dev

2020-02-28 Thread Paul Hankin
On Friday, 28 February 2020 20:43:16 UTC+1, Paul Hankin wrote:
>
> I am rather confused about modules, and have trouble making them work.
>
> I have a package: https://github.com/paulhankin/poker
>
> The go.mod looks like this: `
> module github.com/paulhankin/poker/v2
>
> go 1.13
> `
>
> The package has a v1, and v2. At v2, I moved the main part of the package 
> into a subdirectory "poker" so the import would be "
> github.com/paulhankin/poker/v2/poker" rather than "
> github.com/paulhankin/poker/v2".
>
> I can successfully import the v2 package in a new modules-aware project, 
> and goproxy seems to pick up new versions:
>
> $ go list -m --versions
> github.com/paulhankin/poker/v2 v2.0.0 v2.0.2 v2.0.3 v2.0.4 v2.0.5 v2.0.6 
> v2.0.7
>
>
> I can't find the documentation on go.dev though. The closest I can find 
> is this:
> https://pkg.go.dev/github.com/paulhankin/poker/v2
> which contains the cmd subdirectory, but not the poker subdirectory.
>
> Am I doing something wrong? Did I make a mistake in moving the package 
> into a subdirectory, or is there an easier way to avoid the import name 
> being "v2"?
>
> I'd file a bug against go.dev if I thought it was broken, but I find 
> modules so complicated that I don't know if it's my understanding that's 
> wrong, or if I made a mistake, or if go.dev isn't working correctly. I'm 
> pretty frustrated with the experience so far.
>

Oh, I see the problem. I created the subdirectory after tagging v2.0.0.

-- 
Paul Hankin

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/21888ea2-b3a6-4340-adaa-2d8f9c07402e%40googlegroups.com.


[go-nuts] modules and my v2 package only partly appearing on go.dev

2020-02-28 Thread Paul Hankin
I am rather confused about modules, and have trouble making them work.

I have a package: https://github.com/paulhankin/poker

The go.mod looks like this: `
module github.com/paulhankin/poker/v2

go 1.13
`

The package has a v1, and v2. At v2, I moved the main part of the package 
into a subdirectory "poker" so the import would be 
"github.com/paulhankin/poker/v2/poker" rather than 
"github.com/paulhankin/poker/v2".

I can successfully import the v2 package in a new modules-aware project, 
and goproxy seems to pick up new versions:

$ go list -m --versions
github.com/paulhankin/poker/v2 v2.0.0 v2.0.2 v2.0.3 v2.0.4 v2.0.5 v2.0.6 
v2.0.7


I can't find the documentation on go.dev though. The closest I can find is 
this:
https://pkg.go.dev/github.com/paulhankin/poker/v2
which contains the cmd subdirectory, but not the poker subdirectory.

Am I doing something wrong? Did I make a mistake in moving the package into 
a subdirectory, or is there an easier way to avoid the import name being 
"v2"?

I'd file a bug against go.dev if I thought it was broken, but I find 
modules so complicated that I don't know if it's my understanding that's 
wrong, or if I made a mistake, or if go.dev isn't working correctly. I'm 
pretty frustrated with the experience so far.

Thanks,
Paul Hankin

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/44b8db77-42fd-42cb-8dcc-8788c3d0e69b%40googlegroups.com.


Re: [go-nuts] float accuracy when calculating Fibonacci numbers

2018-05-05 Thread Paul Hankin
On Friday, 27 April 2018 23:57:42 UTC+2, Michael Jones wrote:
>
> Yuval,
>
> There are fundamental issues here.
>
> 1. That equation (de Moivre, Binet) is from the algebra of ideal numbers. 
> Numbers of infinite precision. Not the realm of computer arithmetic. It 
> works fine with double precision (go: float64, c/c++: double) up to F(75) 
> but must fail for F(76) due to the limited precision of 64-bit floating 
> point and has nothing to do with language.
>
> F(76) = 3416454622906707 but the best we can do in 64 bits is 
> 3416454622906706 even with a Pow() function good to +/-1 least significant 
> bit.
>
> 2. Another difference between algebra and computer arithmetic 
> (well...actually about the floor function) is that one of your two power 
> terms is not needed. Psi < 1 so Psi^N is pretty small, so small, that it 
> never changes the value of the rounded result. So you can just evaluate:
>
> return round(math.Pow(math.Phi, float_n) / sqrt_5)
>

A neat not-very-well-known trick is to use that phi^n = Fib(n)phi + 
Fib(n-1). Numbers of the form a*phi+b where a and b are integers are closed 
under multiplication, so you can compute phi^n exactly using integer 
arithmetic -- in log(n) arithmetic operations.

https://play.golang.org/p/j4mZ93c820R

-- 
Paul

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: constant 256 overflows byte

2017-03-08 Thread Paul Hankin
On Tuesday, 7 March 2017 19:02:59 UTC-5, Eric Brown wrote:
>
> memoryBlock := make([]byte, 4)
>
> binary.LittleEndian.PutUint32(memoryBlock, 12345678)  
>
>   
>
> memoryBlock[2] = 256 - (187 - memoryBlock[2]) ' results in error: 
> constant 256 overflows byte
> memoryBlock[2] = 255 - (187 - memoryBlock[2]) + 1 ' results in no errors  
>
>
> Why is this, and what would be your suggested method of handling this?
> I don't like having to do the later as it just looks sloppy.
>
>
Byte operations work modulo 256, so adding 256 does nothing.

Using that, you can simplify your expression to this:
memoryBlock[2] -= 187

https://play.golang.org/p/5ljs4wIeq9
 -- 
Paul Hankin

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.