开发者

Why does multiplying a double by -1 not give the negative of the current answer

开发者 https://www.devze.com 2022-12-23 13:49 出处:网络
I am trying to multiply a double value by -1 to get the negative value. It continues to give me a positive value

I am trying to multiply a double value by -1 to get the negative value. It continues to give me a positive value

EDIT: I am putting all the code up.

public class DecToTime {

public static void main(String[] args) throws IOException {

    DecToTime dtt = new DecToTime();    
    double val = dtt.getNumber("13.930000000000E+02");
    System.out.println(val);

  }

public double getNumber(String number) throws IOException{

    StringReader reader = new StringReader(number);
    int c;
    String mantissa="";
    String sign="";
    String exponent="";
    boolean isMantissa=true;

    while((c = reader.read()) != -1) {
        char character = (char) c;
      if(character=='E'){
          isMantissa=false;  
      }
      if(isMantissa==true){
          mantissa=mantissa+character;
      }

      if(isMantissa==false){
          if((character=='+')|| (character=='-')){
              if(character=='+') {
                  sign = "plus";
              }
              if(character=='-') {
                  sign = "minus";
              }
          }
          if(!(sign.equals(""))){
              exponent=exponent+character;
          }
      }

    }
    System.out.println(mantissa+" - "+sign+" - "+exponent);
    double man = Double.parseDouble(mantissa);
    double exp;
    if(sign.equals("plus")){
        exp = Double.parseDouble(exponent);
    }
  开发者_高级运维  else {
        exp = Double.parseDouble(exponent);
System.out.println("Exp: "+exponent);
    }   
    System.out.println(man+" - "+sign+" - "+exp);
    double value = man*Math.pow(10, exp);
    return value;
}

}

The printed result is

13.93 - minus - 2.0

which is correct except that 2.0 should be -2.0


I suspect that in the else branch, the parsed value of exp is already negative, so negating it results in the positive value you see. Try printing out its value before the negation.

It would certainly help us (and you) if you showed / printed the original value of exponent though.


You are making the multiplication decision on the sign string. so it would help if you would include more code. showing how sign is being set up


I can't understand your problem, both the code and the output look ok to me.

I'd add a debugging statement ("System.out.println(exp)") before the multiplication to better understand if the program behaviour is right.


Wrap in a method and add a unit test with your assertions - does it pass or fail?

Also DRY - you might as well parse the exponent before the if statement since you do it in both branches anyway.


It gives correct result. I guess the value of exponent parses to negative.


Exponent can be negative. It is for numbers smaller than 1.

What you want to multiply is the mantissa. That's what controls the sign of the whole number.

-0.02 = -1 * 2 * 10^(-2) = sign * mantissa * base ^ exp


Your code looks fine, and it executed as expected with mantissa = "2.66", and sign="negate". Try debugging into it if you're still facing problems. Also check to see the .class is not being cached someplace .... I've known that to happen, and cause weird problems.

0

精彩评论

暂无评论...
验证码 换一张
取 消