YW.

Though, you might want to refine it a bit to support BigDecimal and avoid
reflection.

(defn abs [x]
  (cond
    (instance? BigDecimal x)
    (let [^BigDecimal x x]
      (.abs x))
    (instance? Double x)
    (let [^Double x x]
      (Math/abs x))
    (instance? Float x)
    (let [^Float x x]
      (Math/abs x))))

(defn acmp [a1 a2 tolerance]
  (every? #(< % tolerance)
    (map #(abs (- %1 %2)) (seq a1) (seq a2))))

; example test usage: (is (acmp a1 a2 tolerance))

If you also want to avoid boxing you'll need to make a Java class with a
method overloaded for float[], double[], Float[], Double[], and
BigDecimal[] and an acmp function (now limited to homogeneous Java arrays)
that calls the appropriate overload without reflection by using (cond
(instance?)); or else, use such a cond to pick from among five (loop ...)s
with hinted (in two cases, primitive) locals. If you want a pure-clojure
solution that's elegant (no code duplication) you'll want to code a macro
to generate the loop bodies, parametrized by type name symbol, and use
that. :)

On Tue, May 28, 2013 at 1:08 PM, Jim - FooBar(); <jimpil1...@gmail.com>wrote:

>  aww cool! thanks Cedric :)
>
>
>
> On 28/05/13 18:02, Cedric Greevey wrote:
>
>   (defn acmp [a1 a2 tolerance]
>   (every? #(< % tolerance)
>     (map #(Math/abs (- %1 %2)) (seq a1) (seq a2))))
>
> user=> (acmp (double-array [0.01 0.7 2.2])
>              (double-array [0.011 0.695 2.199])
>              0.01)
> true
> user=> (acmp (double-array [0.01 0.7 2.2])
>              (double-array [0.011 0.695 2.199])
>              0.005)
>  false
>  user=>
>
>  Will work on any seqables of float/double.
>
>
>
> On Tue, May 28, 2013 at 12:57 PM, Jim - FooBar(); <jimpil1...@gmail.com>wrote:
>
>>  ooo thanks Chris! I was suspecting the exact same thing because I tried
>> this:
>>
>> (is (= (seq (aget ready 0))
>>          (seq (aget ready 1))))
>>
>> and got this:
>>
>> expected: (= (seq (aget ready 0)) (seq (aget ready 1)))
>> actual: (not (= (-0.5345224838248488 0.2672612419124244 0.801783725737273
>> *1*)
>>                         (-0.5345224838248488 0.2672612419124244
>> 0.801783725737273*2*)))
>>
>> observe the last digit of the 3rd number (the bold ones)! This is why the
>> test fails in Clojure...
>>
>> thanks again :)
>>
>> Jim
>>
>>
>>
>>
>> On 28/05/13 17:42, Jim - FooBar(); wrote:
>>
>> Hi everyone,
>>
>> sometimes I feel really stupid!
>>
>> I am currently looking at a well-known java library's tests and found
>> this:
>>
>> Assert.assertArrayEquals(arrayOutput[0], arrayOutput[1], 0.01);
>> //arrayOutput is a 2d double-array btw
>>
>> since I've basically wrapped this lib, I'd like to port the java tests as
>> well. First of all, how do I compare 2 arrays in Clojure without using
>> .equals() which is broken for arrays. More importantly, where on earth is
>> that method (*assertArrayEquals*)? Looking at the docs for JUnit [1], I
>> can see no overload that takes 2 arrays and a double!!! In fact the only
>> methods that take 3 args expect a String as the first arg....what is
>> happening? can anyone shine some light please?  I am utterly confused...
>>
>> thanks in advance,
>>
>> Jim
>>
>> [1]http://junit.sourceforge.net/javadoc/org/junit/Assert.html
>>
>>
>>    --
>> --
>> 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
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "Clojure" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to clojure+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>>
>
>  --
> --
> 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
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
>
>  --
> --
> 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
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to