3
\$\begingroup\$

How can I improve the style to avoid so many float64()s?

func IsInTimeRange(tvalue int32,timeout int){
 t := time.Now().Unix()
 return int(math.Abs(float64(t)-float64(tvalue))) < timeout
}
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Nov 23, 2015 at 7:14
\$\endgroup\$
5
  • \$\begingroup\$ start with running gofmt on your code \$\endgroup\$ Commented Nov 23, 2015 at 7:22
  • \$\begingroup\$ thanks. I'm not comfortable with so many "float64" in major. \$\endgroup\$ Commented Nov 23, 2015 at 7:37
  • \$\begingroup\$ What is the origin of the tvalue and timeout values. Without a common basis in time.Time, there is no way to compare them relative to a current time. \$\endgroup\$ Commented Nov 23, 2015 at 8:11
  • 1
    \$\begingroup\$ You have a return in a function with no return type in its signature. Why not get your code to compile before posting it? \$\endgroup\$ Commented Nov 23, 2015 at 9:48
  • \$\begingroup\$ Sorry . It's hard to write code in textarea. I will pay attention nexttime. \$\endgroup\$ Commented Nov 23, 2015 at 12:47

3 Answers 3

2
\$\begingroup\$

For example,

package main
import (
 "fmt"
 "time"
)
func IsInTimeRange(tvalue int32, timeout int) bool {
 delta := int(time.Now().Unix() - int64(tvalue))
 return -timeout < delta && delta < timeout
}
func main() {}

tvalue int32 will overflow in year 2038, change it to tvalue int64.

package main
import "time"
func IsInTimeRange(tvalue int64, timeout int) bool {
 delta := int(time.Now().Unix() - tvalue)
 return -timeout < delta && delta < timeout
}
func main() {}
answered Nov 23, 2015 at 8:28
\$\endgroup\$
2
  • \$\begingroup\$ tvalue maybe negative by bad call. What a pity , there are no function overloading in golang \$\endgroup\$ Commented Nov 23, 2015 at 12:36
  • \$\begingroup\$ I have no idea that what's int64 safe in python with Thrift,so I choose int32. \$\endgroup\$ Commented Nov 23, 2015 at 12:45
1
\$\begingroup\$

You seem to convert to float64 only to use math.Abs. This is complete overkill. Abs for integers is a trivial (if you can neglect overflow as in your example) if statement.

(Background: math.Abs exists to handle NaNs and Infs properly.)

answered Nov 23, 2015 at 8:22
\$\endgroup\$
1
\$\begingroup\$

This is not a full answer, but might still be of interest.

As already stated; math.Abs is overkill for integer absolutes. And writing your own Abs function is trivial.

But if you want one, the fastest way to do it can be found in the png package:

func Abs(x int) int {
 // intSize is either 32 or 64.
 const intSize = 32 << (^uint(0) >> 63)
 // m := -1 if x < 0. m := 0 otherwise.
 m := x >> (intSize - 1)
 // In two's complement representation, the negative number
 // of any number (except the smallest one) can be computed
 // by flipping all the bits and add 1. This is faster than
 // code with a branch.
 // See Hacker's Delight, section 2-4.
 return (x ^ m) - m
}
answered Nov 23, 2015 at 8:41
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.