unexpected NaN behavior

2012-10-09 Thread Brian Craft
Can someone explain the last result here?

 [1 2 3 Float/NaN]
[1 2 3 NaN]
 (= Float/NaN Float/NaN)
false
(#(= Float/NaN Float/NaN))
false
; all is good so far, but...
(filter #(= % %) [1 2 3 Float/NaN 4])
(1 2 3 NaN 4)

Now I'm lost. What just happened?

-- 
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

Re: unexpected NaN behavior

2012-10-09 Thread Wes Freeman
Possibly a simpler example:

user= (def x Float/NaN)
#'user/x
user= (= x x)
true

I'm not sure whether true or false makes more sense, personally.

Wes

On Tue, Oct 9, 2012 at 4:38 PM, Brian Craft craft.br...@gmail.com wrote:

 Can someone explain the last result here?

  [1 2 3 Float/NaN]
 [1 2 3 NaN]
  (= Float/NaN Float/NaN)
 false
 (#(= Float/NaN Float/NaN))
 false
 ; all is good so far, but...
 (filter #(= % %) [1 2 3 Float/NaN 4])
 (1 2 3 NaN 4)

 Now I'm lost. What just happened?

 --
 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 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

Re: unexpected NaN behavior

2012-10-09 Thread Wes Freeman
Float/NaN must be instantiating something?

user= (def y Float/NaN)
#'user/y
user= (= x y)
false

On Tue, Oct 9, 2012 at 4:48 PM, Wes Freeman freeman@gmail.com wrote:

 Possibly a simpler example:

 user= (def x Float/NaN)
 #'user/x
 user= (= x x)
 true

 I'm not sure whether true or false makes more sense, personally.

 Wes

 On Tue, Oct 9, 2012 at 4:38 PM, Brian Craft craft.br...@gmail.com wrote:

 Can someone explain the last result here?

  [1 2 3 Float/NaN]
 [1 2 3 NaN]
  (= Float/NaN Float/NaN)
 false
 (#(= Float/NaN Float/NaN))
 false
 ; all is good so far, but...
 (filter #(= % %) [1 2 3 Float/NaN 4])
 (1 2 3 NaN 4)

 Now I'm lost. What just happened?

 --
 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 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

Re: unexpected NaN behavior

2012-10-09 Thread Brian Craft
I every language I'm familiar with NaN tests unequal to all values, 
including itself. That's almost the definition of NaN.

This looks like some kind of shortcut where clojure is not bothering to 
test equality of a symbol with itself.

On Tuesday, October 9, 2012 1:48:54 PM UTC-7, Wes Freeman wrote:

 Possibly a simpler example:

 user= (def x Float/NaN)
 #'user/x
 user= (= x x)
 true

 I'm not sure whether true or false makes more sense, personally.

 Wes

 On Tue, Oct 9, 2012 at 4:38 PM, Brian Craft craft...@gmail.comjavascript:
  wrote:

 Can someone explain the last result here?

  [1 2 3 Float/NaN]
 [1 2 3 NaN]
  (= Float/NaN Float/NaN)
 false
 (#(= Float/NaN Float/NaN))
 false
 ; all is good so far, but...
 (filter #(= % %) [1 2 3 Float/NaN 4])
 (1 2 3 NaN 4)

 Now I'm lost. What just happened?

 -- 
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to clo...@googlegroups.comjavascript:
 Note that posts from new members are moderated - please be patient with 
 your first post.
 To unsubscribe from this group, send email to
 clojure+u...@googlegroups.com javascript:
 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 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

Re: unexpected NaN behavior

2012-10-09 Thread Michael Klishin
2012/10/10 Wes Freeman freeman@gmail.com

 Float/NaN must be instantiating something?


See http://stackoverflow.com/a/1573715/1367685 and replies to it. NaN (and
floating-point
numbers in general) are tricky beasts.

You get the same behavior in Scala and Java:

scala Float.NaN == Float.NaN
res1: Boolean = false
-- 
MK

http://github.com/michaelklishin
http://twitter.com/michaelklishin

-- 
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

Re: unexpected NaN behavior

2012-10-09 Thread David Nolen
On Tue, Oct 9, 2012 at 4:38 PM, Brian Craft craft.br...@gmail.com wrote:
 Can someone explain the last result here?

 [1 2 3 Float/NaN]
 [1 2 3 NaN]
 (= Float/NaN Float/NaN)
 false
 (#(= Float/NaN Float/NaN))
 false
 ; all is good so far, but...
 (filter #(= % %) [1 2 3 Float/NaN 4])
 (1 2 3 NaN 4)

 Now I'm lost. What just happened?

The behavior is strange, I've simplified it a bit to the following:

 ((fn [x] (= x x)) Float/NaN)
true
 ((fn [x y] (= x y)) Float/NaN Float/NaN)
false

This is using Clojure 1.4.0

-- 
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


Re: unexpected NaN behavior

2012-10-09 Thread Aaron Cohen
On Tue, Oct 9, 2012 at 5:20 PM, David Nolen dnolen.li...@gmail.com wrote:
 On Tue, Oct 9, 2012 at 4:38 PM, Brian Craft craft.br...@gmail.com wrote:
 Can someone explain the last result here?

 [1 2 3 Float/NaN]
 [1 2 3 NaN]
 (= Float/NaN Float/NaN)
 false

This is a primitive comparison and follows IEEE floating point
semantics. NaN must not equal NaN.

 ; all is good so far, but...
 (filter #(= % %) [1 2 3 Float/NaN 4])
 (1 2 3 NaN 4)

This is an Object comparison, and in Java this follows the contract
for equals and hashCode. Thus, an object must be equal to itself (for
HashMap to work).

See: 
http://stackoverflow.com/questions/1408569/why-does-double-nan-equal-itself-when-wrapped-in-a-double-instance

and in particular:
http://docs.oracle.com/javase/6/docs/api/java/lang/Double.html#equals(java.lang.Object)





 Now I'm lost. What just happened?

 The behavior is strange, I've simplified it a bit to the following:

 ((fn [x] (= x x)) Float/NaN)
 true
 ((fn [x y] (= x y)) Float/NaN Float/NaN)
 false

 This is using Clojure 1.4.0

 --
 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 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


Re: unexpected NaN behavior

2012-10-09 Thread Brian Craft


On Tuesday, October 9, 2012 2:27:43 PM UTC-7, Aaron Cohen wrote:

 On Tue, Oct 9, 2012 at 5:20 PM, David Nolen 
 dnolen...@gmail.comjavascript: 
 wrote: 
  On Tue, Oct 9, 2012 at 4:38 PM, Brian Craft 
  craft...@gmail.comjavascript: 
 wrote: 
  Can someone explain the last result here? 
  
  [1 2 3 Float/NaN] 
  [1 2 3 NaN] 
  (= Float/NaN Float/NaN) 
  false 

 This is a primitive comparison and follows IEEE floating point 
 semantics. NaN must not equal NaN. 

  ; all is good so far, but... 
  (filter #(= % %) [1 2 3 Float/NaN 4]) 
  (1 2 3 NaN 4) 

 This is an Object comparison, and in Java this follows the contract 
 for equals and hashCode. Thus, an object must be equal to itself (for 
 HashMap to work). 

 See: 
 http://stackoverflow.com/questions/1408569/why-does-double-nan-equal-itself-when-wrapped-in-a-double-instance
  

 and in particular: 

 http://docs.oracle.com/javase/6/docs/api/java/lang/Double.html#equals(java.lang.Object)
  



Thanks for the explanation. I notice by experimentation that == behaves as 
I would expect. I haven't actually found == in the docs. Only =. 

-- 
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

Re: unexpected NaN behavior

2012-10-09 Thread Wes Freeman
I guess it really does do the same thing in Scala:

scala val f = Float.NaN
f: Float = NaN

scala f == f
res4: Boolean = false

scala f.equals(f)
res5: Boolean = true

I guess you really should use == in Clojure, when doing numerical equality.
It even does dynamic typed comparisons better than =. Such as:

user= (= (/ 100M 100M) 1)
false
user= (== (/ 100M 100M) 1)
true

Thanks for clearing that up, Aaron.

Wes

On Tue, Oct 9, 2012 at 5:27 PM, Aaron Cohen aa...@assonance.org wrote:

 On Tue, Oct 9, 2012 at 5:20 PM, David Nolen dnolen.li...@gmail.com
 wrote:
  On Tue, Oct 9, 2012 at 4:38 PM, Brian Craft craft.br...@gmail.com
 wrote:
  Can someone explain the last result here?
 
  [1 2 3 Float/NaN]
  [1 2 3 NaN]
  (= Float/NaN Float/NaN)
  false

 This is a primitive comparison and follows IEEE floating point
 semantics. NaN must not equal NaN.

  ; all is good so far, but...
  (filter #(= % %) [1 2 3 Float/NaN 4])
  (1 2 3 NaN 4)

 This is an Object comparison, and in Java this follows the contract
 for equals and hashCode. Thus, an object must be equal to itself (for
 HashMap to work).

 See:
 http://stackoverflow.com/questions/1408569/why-does-double-nan-equal-itself-when-wrapped-in-a-double-instance

 and in particular:

 http://docs.oracle.com/javase/6/docs/api/java/lang/Double.html#equals(java.lang.Object)




 
  Now I'm lost. What just happened?
 
  The behavior is strange, I've simplified it a bit to the following:
 
  ((fn [x] (= x x)) Float/NaN)
  true
  ((fn [x y] (= x y)) Float/NaN Float/NaN)
  false
 
  This is using Clojure 1.4.0
 
  --
  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 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 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