mirror of https://github.com/golang/go.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 <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
5b147122d6
commit
456f2f5cb8
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue