[Haskell-beginners] List operations
KC
kc1956 at gmail.com
Thu May 19 19:01:29 CEST 2011
x ** y = 10 ** (y * logBase 10 x)
Much easier to do base 10 exponents.
*Euler025> 25.0 ** 3.5
78125.0
*Euler025> 10 ** (3.5 * logBase 10 25.0)
78125.00000000009
On Thu, May 19, 2011 at 9:15 AM, Ertugrul Soeylemez <es at ertes.de> wrote:
> Daniel Fischer <daniel.is.fischer at googlemail.com> wrote:
>
>> However, since you're asking about cost, which indicates that you care
>> for performance, the above would be better written as
>>
>> [x*x*x | x <- list]
>>
>> unless you depend on the small differences in the outcome [I'm not
>> quite sure how many bits may be affected, not many, typically none or
>> one]. Functions like (**), exp, log, sin, cos, ... are slow, very
>> slow. If the exponent is a small (positive) integer, specifically
>> giving a sequence of multiplication steps is much faster, also using
>> (^) instead of (**) is faster for small exponents (but slower than an
>> explicit multiplication sequence).
>
> Neither does this really match my intuition, nor can I confirm it with
> an experiment. Applying (** 3) a million times to a Double takes a
> second and gives me the expected Infinity. Applying (^3) or (\x ->
> x*x*x) a million times to the same value, well, I didn't want to wait
> for it to finish.
>
> The experiment was ran with the following codes in GHCi:
>
> iterate (** 3) 1.000000001 !! 1000000
> iterate (^3) 1.000000001 !! 1000000
> iterate (\x -> x*x*x) 1.000000001 !! 1000000
>
> Note that exponentiation is a cheap operation on Double.
>
>
> Greets
> Ertugrul
>
>
> --
> nightmare = unsafePerformIO (getWrongWife >>= sex)
> http://ertes.de/
>
--
--
Regards,
KC
More information about the Beginners
mailing list