FAQ
I have declaration
const SEG_SIZE = 1024
func main() {

msecs := (int) (SEG_SIZE / 1000000) * 1000

supposed to return 1 but it's 0...
Looks like SEG_SIZE / 1000000 always evals to 0 even if i declare msecs as
float64
I can't make any sense of it, can someone help?


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Dave Cheney at Apr 10, 2015 at 2:56 pm
    1024 / 1000000 is 0. For the same reason that 1 / 2 is 0. If you want
    floating point division, both operands must be floating point types.
    On Saturday, 11 April 2015 00:51:54 UTC+10, vlya...@gmail.com wrote:

    I have declaration
    const SEG_SIZE = 1024
    func main() {

    msecs := (int) (SEG_SIZE / 1000000) * 1000

    supposed to return 1 but it's 0...
    Looks like SEG_SIZE / 1000000 always evals to 0 even if i declare msecs as
    float64
    I can't make any sense of it, can someone help?

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Jan Mercl at Apr 10, 2015 at 2:56 pm

    On Fri, Apr 10, 2015 at 4:51 PM wrote:

    I can't make any sense of it, can someone help?
    Provided '/' is the integer division operation:

             \forall a, b \elem \Z, b != 0, a < b: a/b = 0.

    -j

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Egon at Apr 10, 2015 at 2:58 pm

    On Friday, 10 April 2015 17:51:54 UTC+3, vlya...@gmail.com wrote:
    I have declaration
    const SEG_SIZE = 1024
    func main() {

    msecs := (int) (SEG_SIZE / 1000000) * 1000
    (SEG_SIZE / 1000000.0) * 1000.0

    Or

    SEG_SIZE * 1000 / 1000000

    supposed to return 1 but it's 0...
    Looks like SEG_SIZE / 1000000 always evals to 0 even if i declare msecs as
    float64
    I can't make any sense of it, can someone help?
    Also don't use SEG_SIZE unless you really have to, use SegSize.

    (Not sure what you are doing, but) If you need to use milliseconds use:
    msecs := SegSize * time.Milliseconds

    + Egon

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Victor L at Apr 10, 2015 at 4:52 pm
    Just from long standing habit used (type) expr casting when it should be
    other way around in Go...
    On Fri, Apr 10, 2015 at 10:56 AM, Dave Cheney wrote:

    1024 / 1000000 is 0. For the same reason that 1 / 2 is 0. If you want
    floating point division, both operands must be floating point types.
    On Saturday, 11 April 2015 00:51:54 UTC+10, vlya...@gmail.com wrote:

    I have declaration
    const SEG_SIZE = 1024
    func main() {

    msecs := (int) (SEG_SIZE / 1000000) * 1000

    supposed to return 1 but it's 0...
    Looks like SEG_SIZE / 1000000 always evals to 0 even if i declare msecs
    as float64
    I can't make any sense of it, can someone help?

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • David DENG at Apr 10, 2015 at 7:09 pm
    Check this out: http://play.golang.org/p/2hUOe7xdU3

    The second line is what you need.

    math.Floor((SEG_SIZE / 1000000.) * 1000)


    Two points:

        1. Let compiler know you need a floating division by using 1000000.
        2. Use math.Floor to truncate because Go doesn't allow conversion of
        constants with accuracy loss.


    David
    On Friday, April 10, 2015 at 7:51:54 AM UTC-7, vlya...@gmail.com wrote:

    I have declaration
    const SEG_SIZE = 1024
    func main() {

    msecs := (int) (SEG_SIZE / 1000000) * 1000

    supposed to return 1 but it's 0...
    Looks like SEG_SIZE / 1000000 always evals to 0 even if i declare msecs as
    float64
    I can't make any sense of it, can someone help?

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedApr 10, '15 at 2:51p
activeApr 10, '15 at 7:09p
posts6
users5
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase