Commit 456f2f5c authored by Ian Lance Taylor's avatar Ian Lance Taylor

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 <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 5b147122
......@@ -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.
......
......@@ -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) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment