2
\$\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
}
\$\endgroup\$

migrated from stackoverflow.com Nov 23 '15 at 22:10

This question came from our site for professional and enthusiast programmers.

  • \$\begingroup\$ start with running gofmt on your code \$\endgroup\$ – Aruna Herath Nov 23 '15 at 7:22
  • \$\begingroup\$ thanks. I'm not comfortable with so many "float64" in major. \$\endgroup\$ – wan zong Nov 23 '15 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\$ – GRosenberg Nov 23 '15 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\$ – Amit Kumar Gupta Nov 23 '15 at 9:48
  • \$\begingroup\$ Sorry . It's hard to write code in textarea. I will pay attention nexttime. \$\endgroup\$ – wan zong Nov 23 '15 at 12:47
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() {}
\$\endgroup\$
  • \$\begingroup\$ tvalue maybe negative by bad call. What a pity , there are no function overloading in golang \$\endgroup\$ – wan zong Nov 23 '15 at 12:36
  • \$\begingroup\$ I have no idea that what's int64 safe in python with Thrift,so I choose int32. \$\endgroup\$ – wan zong Nov 23 '15 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.)

\$\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
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.