Hi Brian,
On Oct 16, 5:18 am, Brian Hurt <[email protected]> wrote:
> with double precision infinities and NaNs. pr just calls .toString on
> doubles, which for infinities and nans returns the string "Infinity" or
> "NaN":
> The problem is, when you try to read these values back in, they're
> interpreted as symbols, not as numbers:
Reading as a symbol would not be so bad in most cases, so long as the
symbol resolved to what you wanted:
user=> (eval (with-in-str (with-out-str (print "Double/
POSITIVE_INFINITY")) (read)))
Infinity
user=> (eval (with-in-str (with-out-str (pr Double/POSITIVE_INFINITY))
(read)))
java.lang.Exception: Unable to resolve symbol: Infinity in this
context (NO_SOURCE_FILE:16)
I think it would be preferable for NaN Infinity -Infinity to be
symbols that evaluate to a double, like so:
user=> (def Infinity Double/POSITIVE_INFINITY)
user=> (def -Infinity Double/NEGATIVE_INFINITY)
user=> (def NaN Double/NaN)
user=> (eval (with-in-str (with-out-str (pr Double/POSITIVE_INFINITY))
(read)))
Infinity
However that requires those definitions before the string can be read.
If you want something that can be read independently then either those
would need to be part of core or you could get around this by hooking
in a print method :
(defmethod print-method Double
[#^Double x, #^java.io.Writer w]
(.write w
(condp = x
Double/POSITIVE_INFINITY "Double/POSITIVE_INFINITY"
Double/NEGATIVE_INFINITY "Double/NEGATIVE_INFINITY"
Double/NaN "Double/NaN"
(.toString x))))
user=> (eval (with-in-str (with-out-str (pr Double/POSITIVE_INFINITY))
(read)))
Double/POSITIVE_INFINITY
Hope that helps, I guess it doesn't address the original question of
reading them as actual numbers but for some reason doing so sounds
suspicious to me - though I can't think of a reason why it would be a
bad thing.
Regards,
Tim.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---