this is on clojure 1.1 (current ubuntu stable rep)
so, the code below shows ABS returning 2 different values for the
'same' input in clojure. input being Integer.MIN_VALUE.
of course one input is an int and the other is a bigint, but we can't
see the difference in clojure, and as the code shows at the end,
clojure says they are the same input.

@CuppoJava:
to take more care in how we code is a good idea, but i feel that rich
hickey doesn't want users of clojure to be thinking about if a certain
function is going to act a certain way for inputs that clojure says
are the same, but java says are different. i think this type of
problem is on the same level as managing the memory of your
application.

user=> (def min-int-1 (+ -1 min-int))
#'user/min-int-1
user=> min-int-1
-2147483649
user=> (- min-int-1 min-int)
-1
user=> (Math/abs (+ 1 min-int-1))
2147483648
user=> (Math/abs min-int)
-2147483648

user=> (= (+ 1 min-int-1) min-int)
true


@Sean Corfield:
I haven't tried this on clojure 1.3. is it part of the clojure road
map to eliminate the surprises built into the java language? I think
that it would be easy to eliminate a majority of them. a good resource
is the book 'java puzzlers' http://www.javapuzzlers.com/ which
provides the code in the book for free on their website. not all of
the surprises will apply to clojure because they'll be avoided by
avoiding the use of java, but some will still apply, and they should
be considered in the definition of the language, and when using
interop code.


so, in summary, ABS in java has a bug/surprise. clojure uses this
directly. so clojure has a bug in abs. it's really unlikely that
someone will pass abs an Integer.MIN_VALUE, but maybe it should be
considered. i think the more likely thing to happen is that abs will
be passed a number that is a result of another number being
manipulated. so one solution would be to change to a bigint earlier
than the language does now. or maybe closure could make a safe abs
wrapper.

I know it's not a big deal, but when you write a program that uses all
integers (quite easy to program in clojure), and also uses abs, and it
breaks when it hits this bug...

Rich hickey's answer to why he made clojure was that he wanted to
write programs without going crazy, fixing some of the stuff that is
broken in java, that clojure uses, seems like it follows that spirit.

On Nov 2, 9:33 pm, Sean Corfield <seancorfi...@gmail.com> wrote:
> On Tue, Nov 2, 2010 at 3:22 PM, box <somethingital...@gmail.com> wrote:
> > Math/abs doesn't always return positive numbers. example:
>
> Have you tried this on 1.3.0-master-SNAPSHOT?
>
> user=> (def min-int -2147483648)
> #'user/min-int
> user=> (Math/abs min-int)
> 2147483648
> user=> (def min-int+1 (+ 1 min-int))
> #'user/min-int+1
> user=> min-int+1
> -2147483647
> user=> (Math/abs (- min-int+1 1))
> 2147483648
> user=> (Math/abs (- Integer/MIN_VALUE 1))
> 2147483649
> --
> Sean A Corfield -- (904) 302-SEAN
> Railo Technologies, Inc. --http://getrailo.com/
> An Architect's View --http://corfield.org/
>
> "If you're not annoying somebody, you're not really alive."
> -- Margaret Atwood

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to