You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@kafka.apache.org by "Joel Koshy (JIRA)" <ji...@apache.org> on 2014/09/23 00:42:34 UTC

[jira] [Updated] (KAFKA-1469) Util.abs function does not return correct absolute values for negative values

     [ https://issues.apache.org/jira/browse/KAFKA-1469?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Joel Koshy updated KAFKA-1469:
------------------------------
    Description: 
Reported by Russell Melick. [edit1: I don't think this affects correctness of
the places that use the abs utility since we just need it to return a
consistent positive value, but we should fix this nonetheless]
[edit 2: actually it affects correctness in places that depend on consistent
values across the fix. e.g., the offset manager is determined based on
abs(hash(consumer group)). So after an upgrade that can change]

{code}
     /**
      * Get the absolute value of the given number. If the number is
   Int.MinValue return 0.
      * This is different from java.lang.Math.abs or scala.math.abs in that
   they return Int.MinValue (!).
      */
     def abs(n: Int) = n & 0x7fffffff
{code}

For negative integers, it does not return the absolute value.  It does
appear to do what the comment says for Int.MinValue though.  For example,

{code}
   scala> -1 & 0x7fffffff
   res8: Int = 2147483647

   scala> -2 & 0x7fffffff
   res9: Int = 2147483646

   scala> -2147483647 & 0x7fffffff
   res11: Int = 1

   scala> -2147483648 & 0x7fffffff
   res12: Int = 0
{code}


  was:
Reported by Russell Melick. [edit: I don't think this affects correctness of
the places that use the abs utility since we just need it to return a
consistent positive value, but we should fix this nonetheless]

{code}
     /**
      * Get the absolute value of the given number. If the number is
   Int.MinValue return 0.
      * This is different from java.lang.Math.abs or scala.math.abs in that
   they return Int.MinValue (!).
      */
     def abs(n: Int) = n & 0x7fffffff
{code}

For negative integers, it does not return the absolute value.  It does
appear to do what the comment says for Int.MinValue though.  For example,

{code}
   scala> -1 & 0x7fffffff
   res8: Int = 2147483647

   scala> -2 & 0x7fffffff
   res9: Int = 2147483646

   scala> -2147483647 & 0x7fffffff
   res11: Int = 1

   scala> -2147483648 & 0x7fffffff
   res12: Int = 0
{code}



> Util.abs function does not return correct absolute values for negative values
> -----------------------------------------------------------------------------
>
>                 Key: KAFKA-1469
>                 URL: https://issues.apache.org/jira/browse/KAFKA-1469
>             Project: Kafka
>          Issue Type: Bug
>            Reporter: Joel Koshy
>              Labels: newbie, patch
>             Fix For: 0.8.2
>
>         Attachments: KAFKA-1469.patch
>
>
> Reported by Russell Melick. [edit1: I don't think this affects correctness of
> the places that use the abs utility since we just need it to return a
> consistent positive value, but we should fix this nonetheless]
> [edit 2: actually it affects correctness in places that depend on consistent
> values across the fix. e.g., the offset manager is determined based on
> abs(hash(consumer group)). So after an upgrade that can change]
> {code}
>      /**
>       * Get the absolute value of the given number. If the number is
>    Int.MinValue return 0.
>       * This is different from java.lang.Math.abs or scala.math.abs in that
>    they return Int.MinValue (!).
>       */
>      def abs(n: Int) = n & 0x7fffffff
> {code}
> For negative integers, it does not return the absolute value.  It does
> appear to do what the comment says for Int.MinValue though.  For example,
> {code}
>    scala> -1 & 0x7fffffff
>    res8: Int = 2147483647
>    scala> -2 & 0x7fffffff
>    res9: Int = 2147483646
>    scala> -2147483647 & 0x7fffffff
>    res11: Int = 1
>    scala> -2147483648 & 0x7fffffff
>    res12: Int = 0
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)