Why there is no int64Slice on sort when we have a float64Slice

6,234 views
Skip to first unread message

Sankar

unread,
Sep 24, 2012, 11:37:49 AM9/24/12
to golan...@googlegroups.com
I have an array of int64s which are generated by storing time differences in nano seconds.

times := make([]int64, n)
for i:=0; i < n; i++ {
        t0 := time.Now()
        //some operation
        t1 := time.Now()
times[i] = (t1.Sub(t0)).Nanoseconds()
}

Now I want to sort this times array but I cannot find a suitable Int64Slice datatype. The sort.Ints function too work only with int datatype and not int64. Why is this so ? 

si guy

unread,
Sep 24, 2012, 12:08:19 PM9/24/12
to golan...@googlegroups.com
Just a guess, but it may have something to do with the forthcoming int being 64 bits wide on amd64. Or it's missing because no one needed it.

Kyle Lemons

unread,
Sep 24, 2012, 12:24:52 PM9/24/12
to Sankar, golan...@googlegroups.com
Because it's four lines of code:

type int64arr []int64
func (a int64arr) Len() int { return len(a) }
func (a int64arr) Swap(i, j int} { a[i], a[j] = a[j], a[i] }
func (a int64arr) Less(i, j int) bool { return a[i] < a[j] }

Float64 is the more common float, int is the more common it, so both have predefined versions of this.

Sankar P

unread,
Sep 24, 2012, 12:33:18 PM9/24/12
to Kyle Lemons, golan...@googlegroups.com
Hm. Ok. But for my taste, it kind of feels odd to not have an API for default bit but 64-bit alone (in case of float) and default-bit-size alone but not for 64 bit. I would not have been surprised had there been no IntSlice type existed at all.

--
Sankar P
http://psankar.blogspot.com

Russ Cox

unread,
Sep 24, 2012, 12:59:31 PM9/24/12
to Sankar P, Kyle Lemons, golan...@googlegroups.com
> Hm. Ok. But for my taste, it kind of feels odd to not have an API for
> default bit but 64-bit alone (in case of float) and default-bit-size alone
> but not for 64 bit. I would not have been surprised had there been no
> IntSlice type existed at all.

The slices exist for the default types taken by constants: 1 (int), ""
(string), and 1.0 (float64). We stopped there.

Russ

Jan Mercl

unread,
Sep 24, 2012, 1:31:06 PM9/24/12
to Sankar, golan...@googlegroups.com
On Mon, Sep 24, 2012 at 5:37 PM, Sankar <sankar.c...@gmail.com> wrote:
> Now I want to sort this times array but I cannot find a suitable Int64Slice
> datatype.

Here you (may) go: http://go.pkgdoc.org/github.com/cznic/sortutil

-j

Sankar P

unread,
Sep 24, 2012, 1:40:10 PM9/24/12
to Jan Mercl, golan...@googlegroups.com
Thanks.

But I went ahead with my own four lines already similar to what Kyle Lemmons wrote :) I was curios about the reasoning behind the decision.

Sankar P

unread,
Sep 24, 2012, 1:41:47 PM9/24/12
to Russ Cox, Kyle Lemons, golan...@googlegroups.com
ah okay.

But in a 32 bit machine, f := 1.0 will take it as float64 or float ? 

I should have checked it myself but sorry I do not have access to a 32 bit machine at the moment. 

Thanks.

Patrick Mylund Nielsen

unread,
Sep 24, 2012, 1:45:22 PM9/24/12
to Sankar P, Russ Cox, Kyle Lemons, golan...@googlegroups.com
There is no float; just float32 and float64. Float64 is the more common.

--
 
 

Sankar P

unread,
Sep 24, 2012, 1:48:57 PM9/24/12
to Patrick Mylund Nielsen, Russ Cox, Kyle Lemons, golan...@googlegroups.com
I accept that float64 is the most common and I dont debate that float32Slice should have been there by default (thought I didnt understand why it didnt exist until I sent the mail here).

I was just curios to know if on a 32 bit machine, f := 1.0 will create a float64 type or float32 type. 

Thanks

Patrick Mylund Nielsen

unread,
Sep 24, 2012, 1:54:17 PM9/24/12
to Sankar P, Russ Cox, Kyle Lemons, golan...@googlegroups.com
Ah, I thought you meant float64 vs. float (which doesn't exist.)

In any case, the default type for the constant 1.0 is float64: http://golang.org/ref/spec#Constant_expressions
Reply all
Reply to author
Forward
0 new messages