On 4 February 2010 08:04, Wardrop <[email protected]> wrote:
> (defn <insert-name-here> [n base]
> (loop [n n count 0]
> (if (< n base)
> {:val n :count count}
> (recur (float (/ n base)) (inc count)))))
>
> [ ... ]
>
> I mean, what the hell would you name this function, or would you not
> create such an obscure and generalised function to start with?
How about significand-and-exponent-in-radix? :-)
(Or for a shorter name maybe sci-notation-map or something...)
In fact, I might generalise it further to accept an optional argument
-- defaulting to 0 -- to determine how the result is to be normalised:
user> (defn significand-and-exponent-in-radix
([n radix] (significand-and-exponent-in-radix n radix 1))
([n radix o]
(let [threshold (Math/pow radix o)]
(loop [n n count 0]
(if (< n threshold)
{:significand n :exponent count}
(recur (double (/ n radix)) (inc count)))))))
#'user/significand-and-exponent-in-radix
user> (Math/pow 10 1)
10.0
user> (significand-and-exponent-in-radix 1000 10)
{:significand 1.0, :exponent 3}
user> (significand-and-exponent-in-radix 1000 10 0)
{:significand 0.1, :exponent 4}
Also, add docstrings. With the REPL at hand, the explanation of
anything unclear from your function's name is one (doc ...) call away
if you do.
All best,
Michał
--
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