]> Cypherpunks repositories - gostls13.git/commitdiff
time: use 1e9 rather than 1e-9 in Duration calculations
authorIan Lance Taylor <iant@golang.org>
Fri, 11 Nov 2016 01:10:45 +0000 (17:10 -0800)
committerIan Lance Taylor <iant@golang.org>
Sat, 12 Nov 2016 01:18:26 +0000 (01:18 +0000)
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 <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/time/time.go
src/time/time_test.go

index 175c9a9ae6fb98035e8cdd8049fb7ab0e4cc7aad..00fafb64ddf91f7953007b339ace5d52df014796 100644 (file)
@@ -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.
index 07afcffc2147161e23c39a74db17a7d27b51e6e9..2922560f0972b0b9c991a96f6eabc467723f4b53 100644 (file)
@@ -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) {