Re: Limit or Bug?

  • From: "Charles Schultz" <sacrophyte@xxxxxxxxx>
  • To: jkstill@xxxxxxxxx
  • Date: Thu, 3 Jul 2008 16:19:57 -0500

Interesting indeed:

SQL > select mod(3.4028E+38,2) from dual;

MOD(3.4028E+38,2)
-----------------
                0

SQL > select mod(3.4028E+38 + 1,2) from dual;

MOD(3.4028E+38+1,2)
-------------------
                 -1


On Thu, Jul 3, 2008 at 3:44 PM, Jared Still <jkstill@xxxxxxxxx> wrote:

>
> The following bit of SQL uses the mod() function to determine modulus 2 of
> two large numbers
>
> 13:37:20 SQL>set echo on
> 13:37:24 SQL>@mod_test
> 13:37:25 SQL>
> 13:37:25 SQL>
> 13:37:25 SQL>
> 13:37:25 SQL>select mod(power(2,127)-1,2) from dual
> 13:37:25   2  /
>
> MOD(POWER(2,127)-1,2)
> ---------------------
>                     1
>
> 1 row selected.
>
> 13:37:25 SQL>
> 13:37:25 SQL>select mod(power(2,128)-1,2) from dual
> 13:37:25   2  /
>
> MOD(POWER(2,128)-1,2)
> ---------------------
>                    -1
>
> 1 row selected.
>
> 13:37:25 SQL>
>
> --------------------------------------------------
>
> Notice that mod(power(2,128)-1,2) returns an incorrect answer, while the
> answer for mod(power(2,127)-1,2) is correct.
>
> Does someone here know why?
>
> Is it a limit?
>
> Or is it a bug?
>
> Please supply an explanation and/or URL for your answer.  :)
>
> My guess (yes Alex, it's a guess, or rather, a hunch) is that this is
> related to two's complement binary numbers, but I haven't been able
> to (yet) find any explanation for this.
>
> --
> Jared Still
> Certifiable Oracle DBA and Part Time Perl Evangelist
>



-- 
Charles Schultz

Other related posts: