What is the squareRoot of 5?

Discussion in 'School Work Help' started by spider-man, Jan 25, 2010.

  1. xtirpation

    xtirpation Active Member

    32
    234
    0
    You're definitely on the right track, I'm guessing you haven't been taught Java (or coding in general) officially in a class, that was a really good shot. However, in programming, instead of thinking about how your code will behave when it works, always try to think about when it will fail. Once you've run out of possible ways for it to fail, then you know you've got a foolproof working product. I think, in your code, the two flaws were using strict equivalencies (which you wouldn't have known not to use, not being told about accuracy in Java) and adding/subtracting 0.1 (but this one would've been hard to catch).

    Really, I think I'm coming off as a real jerk here, criticizing your code at every possibility, but I mean it for your benefit (now I sound like a pretentious snob) because after all, that's how we all learn to do better, right? Through others pointing out our mistakes?
     
  2. xtirpation

    xtirpation Active Member

    32
    234
    0
    Sorry about the repeated posting. Here, I've noticed that the min value is greater than the max value, that'll definitely need to be fixed, maybe with a Math.max statement or something to figure out which of the two values is greater
     
  3. spider-man

    spider-man Well-Known Member

    466
    55
    1
    hmmmmm, I think it did this:

    min = mid + 0.1;

    and then it went out of the while.
     
  4. xtirpation

    xtirpation Active Member

    32
    234
    0
    you could fix it by printing the below instead.

    output.println("min: " + Math.min(min,max) + " max: " + Math.max(min,max));

    But this is sort of a bad way to fix it. What I would do (without rewriting the whole thing) is to just break and quit the loop when min < max. Of course, then you would have to lower the value added, instead of 0.1, you'd have to use 0.01 or something, and the end result might have less accuracy, though the computer does more calculations.

    The issue here is that the 0.1 is arbitrary; it's not a value chosen based on the value given. To be honest, I wouldn't even add anything, and just do min = mid. I know that you probably want to increment the min by a little bit, since we know that mid isn't the root, but, depending on the input given, the increment will vary. Calculating what increment to use would be silly, because it would require more resources from the CPU, which goes against what the ultimate goal of finding an optimal increment, to minimize CPU usage.
     
  5. spider-man

    spider-man Well-Known Member

    466
    55
    1
    It's getting to hard now, I think I'll stop here.:xd:Thanks-^_^
     
  6. spider-man

    spider-man Well-Known Member

    466
    55
    1
    Just by using my code, I figured out how to find the square root of any number just by using a standard calculator without using the square root button. (Just for fun).