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

Pages: 1-

JAVA "to the power of"

Name: Anonymous 2011-12-10 17:01

Sup /prog/?

I know this is a retarded question, but I need some help with a method I need to write. The method parameters are a double and an int (a and b, respectively)

I need to write a for loop in the method that raises a (the double) to the power of b (the int)

How the fuck do I do this?? Normally this wouldn't be a problem, but I'm burnt out from finals

Name: Anonymous 2011-12-10 17:10




public static Double anus(Double a, int b){
  return Math.pow(a, b);
}

Name: Anonymous 2011-12-10 17:12

>>2

I can't call on another method, I have to write my own method using a loop to solve this. Thanks though

Name: Anonymous 2011-12-10 17:15

Go read a programming book for fucks sake. ANY programming book.

Name: Anonymous 2011-12-10 17:16

>>4

This isn't really programming, I know how to do loops and shit, this is just math that I am having trouble with

Name: Anonymous 2011-12-10 17:21

>>5
a0 = 1.
ab = a · a · ... · a (b times.)
a-b = 1 / ab.
00 is undefined.

Name: Anonymous 2011-12-10 17:22


public static Double anus(Double SGERFWGERHEEETHHHHHHHWETHG, int SDGIKgrnsjsnglrSGHERI){
  Double jieurlhigosrlijhoegterlehogti = 1.0;
  while(SDGIKgrnsjsnglrSGHERI-- > 0) {
        jieurlhigosrlijhoegterlehogti *= SGERFWGERHEEETHHHHHHHWETHG;
  }
  return jieurlhigosrlijhoegterlehogti ;
}

Name: Anonymous 2011-12-10 17:32

>>7
Wrongamundo, buddy.

Name: Anonymous 2011-12-10 17:52

>>8

UPDATED TO HANDLE NEGATIVE NUMBERS IN IDIOMATIC JAVA STYLE



public static Double anus(Double SGERFWGERHEEETHHHHHHHWETHG, int SDGIKgrnsjsnglrSGHERI){
  if (SDGIKgrnsjsnglrSGHERI == 0) return 1.0;
 
  Double jieurlhigosrlijhoegterlehogti = 1.0;
  int GSJRGerhglshergsehrgSGE = (SDGIKgrnsjsnglrSGHERI > 0) ? -1 : 1;
 
  do { jieurlhigosrlijhoegterlehogti *= SGERFWGERHEEETHHHHHHHWETHG;
  } while((SDGIKgrnsjsnglrSGHERI += GSJRGerhglshergsehrgSGE) != 0);
 
  return (GSJRGerhglshergsehrgSGE == -1) ? jieurlhigosrlijhoegterlehogti : 1.0 / jieurlhigosrlijhoegterlehogti;
}

Name: Anonymous 2011-12-10 18:42

>>6
I take 1 for 00 unless otherwise stated.

In computation it's a good choice because it will usually result in a correct ultimate result rather than an aborted one. Even though its undefined, its limit is 1 and that is usually all your program needs.

http://www.wolframalpha.com/input/?i=lim+x%E2%86%920+x^x&asynchronous=false&equal=Submit

Name: Anonymous 2011-12-10 19:19

pubic static doubles powpowpow(double a, int b) {
 if(b<0)
  return 1/powpowpow(a,-b);
 else if(!b)
  return 1;
 else {
   // this is left as an exercise for the reader
 }
}

Name: Anonymous 2011-12-10 19:36

>>11
else if (!b)
that type of shit doesn't work in java, you need to implicitly compare to 0

Name: Anonymous 2011-12-10 19:47

ffffffffffffffffffffffffffff44444444444444444

Name: Anonymous 2011-12-10 21:20

>>11
You don't need a special case:
let powpow a b =
    let rec powpow a b c = match b < 1 with
        if b < 1 then c (* the only terminal call *)
        else powpow a (b - 1) (c *. a) in
    let negpow a b = 1.0 /. (powpow a (-b) 1.0) in
    if b < 0 then negpow a b
    else powpow a b 1.0;;

Name: Anonymous 2011-12-10 22:38

If you don't know how to do this already, you probably shouldn't be programming... Just think about what power means...


if (b == 0) { return 1; }
else if (b == 1) { return a; }
else {
double c = a;
for (/*you're writing this one faggot*/) {
c = c * a;
}
return c;


By the way, I don't know dick about Java, so I can't tell if the syntax is correct.

Name: Anonymous 2011-12-10 23:00

All the examples in this thread are needlessly obfuscated.


def pow(a,b):
    c=1
    while b:
        if b%2:c*=a
        a*=a
        b//=2
    return c

Name: >>14 2011-12-11 1:26

Oops, mispaste:
let powpow a b =
    let rec powpow a b c =
        if b < 1 then c
        else powpow a (b - 1) (c *. a) in
    let negpow a b = 1.0 /. (powpow a (-b) 1.0) in
    if b < 0 then negpow a b
    else powpow a b 1.0;;


>>16
Nice negatives powers.
let powpow a b =
    let rec powpow a b c =
        if b < 1 then c
        else powpow a (b - 1) (c *. a) in
    powpow a b 1.0;;

Name: Anonymous 2011-12-11 4:50

>>10
But that's not the only limit.

limx→+00x = 0
limx→+0x0 = 1

Looks like most programming languages define 00 = 1, though, so yeah, >>1 is better off doing that.

Name: Anonymous 2011-12-11 5:02

// Easy
double candidate = Double.MIN_VALUE;
do {candidate += 0.00000000001;} while (Math.abs(candidate - Math.pow(a,b)) > 0.00000000001); return candidate;

Name: Anonymous 2011-12-11 5:06

>>16
pow = __import__("operator").pow

Name: Anonymous 2011-12-11 5:19

>>18
Right. There are other expressly indeterminate limits, which is a big part of why it's left undefined in general. (Which is certainly "more correct" but usually less useful.)

Name: Anonymous 2011-12-11 5:43

Horse tits dubs

Name: Anonymous 2011-12-11 8:05

x→+0
Why?

Name: Anonymous 2011-12-11 8:14

>>23
For the first example, there's no limit coming from negative infinity (0n is always undefined if n < 0). It didn't really matter in the second one, but it looked good.

Name: Anonymous 2011-12-11 8:50

if you dont know what "to the power of" means mathematically, you have no right to live on this planet

Name: Anonymous 2011-12-11 9:17

>>25
Mathematics is a pseudoscience, IRL there are no 2^(1/2).

Name: Anonymous 2011-12-11 9:29

>>17
*. /.
lol static typing

Name: Anonymous 2011-12-11 12:53

>>27
Seperating exact and inexact arithmetic is a great idea. Static typing has nothing to do with it.

http://www-pu.informatik.uni-tuebingen.de/users/sperber/papers/numerical-tower.pdf

Name: Anonymous 2011-12-11 13:11

>>27
It's funny, I don't see you complaining about it in the C threads.

Name: Anonymous 2011-12-11 13:22

>>28
Seperating people from their wealth and freedom is a great idea. Communism has nothing to do with it.

Name: Anonymous 2011-12-11 13:41

WHY CAN'T I MAKE A NEW THREAD FAGGOTS

Name: Anonymous 2011-12-11 13:42

>>30
Hey genius, Scheme is dynamically typed.

Name: Anonymous 2011-12-11 14:34

>>32
Scheme still uses same ops for all numeric types.

Name: Anonymous 2011-12-11 14:57

>>33
So your problem has to do with the lack of operator overloading. Can't say I miss it.

Name: Anonymous 2011-12-12 4:02

None of this code is ENTERPRISE QUALITY, /prog/!


import java.util.HashMap;

public class PowerFactory
{
  private static PowerFactory powerFactoryFactory = null;

  public static PowerFactory getPowerFactory()
  {
    if (powerFactoryFactory = null)
      powerFactoryFactory = new PowerFactory();
    return powerFactoryFactory;
  }

  private HashMap<Double, HashMap<Integer, Double>> pows;

  private PowerFactory()
  {
    pows = new HashMap<Double, HashMap<Integer, Double>>();
  }

  public double power(int a, double b)
  {
    double ans = 0;
    HashMap<Integer, Double> ins
    if (pows.containsKey(b))
    {
      ins = pows.get(b);
      if (ins.containsKey(a))
        ans = ins.get(a);
      else
      {
        ans = raise(a, b);
        ins.put(a, ans);
      }
    }
    else
    {
      ins = new HashMap<Integer, Double>();
      ans = raise(a, b);
      ins.put(a, ans);
      pows.put(b, ins);
    }
    return ans;
  }

  private static raise(int a, double b)
  {
    double res = b;
    for (int i = 1; i < a; i++)
      res *= b;
    return res;
  }
}

Name: Anonymous 2011-12-12 6:33

>>35
Weak.

Name: Anonymous 2011-12-12 9:56

>>34
When I need it, I use a macro that unhygienically shadows the bindings of +, -, whatever.

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