From: Ian Lance Taylor Date: Fri, 11 Nov 2016 01:10:45 +0000 (-0800) Subject: time: use 1e9 rather than 1e-9 in Duration calculations X-Git-Tag: go1.8beta1~184 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=456f2f5cb8c2e06e7faba6ba298ffb65c7a19397;p=gostls13.git time: use 1e9 rather than 1e-9 in Duration calculations 1e-9 has a 1 in the last place, causing some Duration calculations to have unnecessary rounding errors. 1e9 does not, so use that instead. Change-Id: I96334a2c47e7a014b532eb4b8a3ef9550e7ed057 Reviewed-on: https://go-review.googlesource.com/33116 Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- diff --git a/src/time/time.go b/src/time/time.go index 175c9a9ae6..00fafb64dd 100644 --- a/src/time/time.go +++ b/src/time/time.go @@ -603,21 +603,21 @@ func (d Duration) Nanoseconds() int64 { return int64(d) } func (d Duration) Seconds() float64 { sec := d / Second nsec := d % Second - return float64(sec) + float64(nsec)*1e-9 + return float64(sec) + float64(nsec)/1e9 } // Minutes returns the duration as a floating point number of minutes. func (d Duration) Minutes() float64 { min := d / Minute nsec := d % Minute - return float64(min) + float64(nsec)*(1e-9/60) + return float64(min) + float64(nsec)/(60*1e9) } // Hours returns the duration as a floating point number of hours. func (d Duration) Hours() float64 { hour := d / Hour nsec := d % Hour - return float64(hour) + float64(nsec)*(1e-9/60/60) + return float64(hour) + float64(nsec)/(60*60*1e9) } // Add returns the time t+d. diff --git a/src/time/time_test.go b/src/time/time_test.go index 07afcffc21..2922560f09 100644 --- a/src/time/time_test.go +++ b/src/time/time_test.go @@ -1003,6 +1003,21 @@ func TestDurationNanoseconds(t *testing.T) { } } +var secDurationTests = []struct { + d Duration + want float64 +}{ + {Duration(300000000), 0.3}, +} + +func TestDurationSeconds(t *testing.T) { + for _, tt := range secDurationTests { + if got := tt.d.Seconds(); got != tt.want { + t.Errorf("d.Seconds() = %g; want: %g", got, tt.want) + } + } +} + var minDurationTests = []struct { d Duration want float64 @@ -1011,6 +1026,7 @@ var minDurationTests = []struct { {Duration(-1), -1 / 60e9}, {Duration(1), 1 / 60e9}, {Duration(60000000000), 1}, + {Duration(3000), 5e-8}, } func TestDurationMinutes(t *testing.T) { @@ -1029,6 +1045,7 @@ var hourDurationTests = []struct { {Duration(-1), -1 / 3600e9}, {Duration(1), 1 / 3600e9}, {Duration(3600000000000), 1}, + {Duration(36), 1e-11}, } func TestDurationHours(t *testing.T) {