Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

*NEW* Homework - Exponent

Name: Anonymous 2010-03-06 15:02

YASHT - Yet Another Stupid Homework Thread

Write a function that returns the value of any number raised to an integer power in the language of your choice. Positives, negatives, the whole nine yards.

Here's my Scheme implementation:

(define (power n pow)
  (cond ((> pow 1) (* (power n (- pow 1)) n))
        ((= pow 1) n)
        ((= pow 0) 1)
        (else (/ 1 (power n (* pow -1))))))


EXTRA CREDIT Write one that raises numbers to non-integer powers. I don't know the math behind that, so I couldn't do it.

Name: Anonymous 2010-03-06 15:05

sub power { shift ** shift }

Name: Anonymous 2010-03-06 15:07

You 'ave SHbeen Trolled

Name: Anonymous 2010-03-06 15:25

where is the sussman part?

Name: Anonymous 2010-03-06 15:26

pow :: Double -> Integer -> Double
pow 0 _ = 0
pow a b | b == 0    = 1
        | b < 0     = 1 / pow a (-b)
        | otherwise = a * pow a (b - 1)

Name: Anonymous 2010-03-06 20:50

public double pow(double a, int b){
    return Math.pow(a, b);
}

Name: here we go... 2010-03-06 20:53

#include <complex.h>

...


You get the idea.

Name: Anonymous 2010-03-06 21:10

#include <void.h>

...

You get the idea.

Name: Anonymous 2010-03-06 23:05

>>8
I'm afraid I don't. I get the impression I'm missing out on something extraordinary.

Name: Anonymous 2010-03-07 5:32

>>1
You obviously haven't heard about fast exponentiation algorithm.
Write one that raises numbers to non-integer powers. I don't know the math behind that, so I couldn't do it.
IHBT

Name: Anonymous 2010-03-07 5:52

EXPT function does this in CL.

Name: Anonymous 2010-12-10 15:43

Name: Anonymous 2011-02-02 23:19

Name: Anonymous 2011-02-03 2:55

Name: Anonymous 2011-02-04 19:28

Don't change these.
Name: Email:
Entire Thread Thread List