On Wed, Jul 27, 2011 at 4:33 PM, Alan Malloy <a...@malloys.org> wrote:
> On Jul 27, 11:45 am, Andrea Tortorella <elian...@gmail.com> wrote:
>> Hi everyone,
>> I don't know where to post about bugs (if this is a bug).
>> Anyway in clojure 1.3 with the new numerics:
>>
>> (format "%d" 2N)
>>
>> throws IllegalFormatConversionException, is it a bug? are there any
>> workarounds?
>
> Unlikely to change anytime soon: format just calls
> java.util.Formatter.format; if you look at the source of that, it
> tests for each built-in numeric type specially, instead of using
> Number. But you can probably use the %s specifier, since converting it
> to a string is unlikely to do much harm.

Of course none of the nice number-specific formatting options are
available then. If you don't mind loss of precision in
less-significant digits, you might use %f and (double the-number) to
format its coercion to double.

But according to
http://download.oracle.com/javase/6/docs/api/java/util/Formatter.html
Formatter *is* supposed to work with BigInteger with %d (and other
integer-formatting options, and with BigDecimal with %f and other
float-formatting options). So I don't know what's going wrong here,
other than that it also says IllegalFormatConversionException gets
thrown if the format symbol and the parameter are mismatched.

The JavaSE 6 Formatter works properly when invoked explicitly via interop:

=> (.format (java.util.Formatter.) "%d" (into-array Object [(bigint 2)]))
#<Formatter 2>

(2N isn't recognized as a BigInteger literal by Clojure 1.2, it seems.)

In my copy of Clojure 1.2, format also seems to work:

=> (format "%d" (bigint 2))
"2"

So this could be a bug in 1.3 the OP is running into, or else 2N is
turning into something other than what (bigint 2) evaluates to.

=> (format "%d" #=(bigint 2))
"2"

also works for me, and is more closely equivalent to what the OP is
trying to do (as the bigint is embedded directly in the code via the
reader rather than constructed on the fly at eval time).

It looks like either 2N is doing something unexpected or the format
function somehow broke in 1.3.

-- 
Protege: What is this seething mass of parentheses?!
Master: Your father's Lisp REPL. This is the language of a true
hacker. Not as clumsy or random as C++; a language for a more
civilized age.

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