如何在java中获取指数函数的值以及如何编码指数函数

时间:2016-01-13 17:11:47

标签: java math exponential

我希望为我的代码获得指数函数的值,但我不知道如何在Java中编写指数函数。在Java中编写指数函数的方法是什么?

public class Demo {

    // inputs are declared
    private int x[] = {1, 0, 1, 0};
    //weights are declared
    private double w[] = {0.4, 0.6, 0.7, 0.8};
    private double temp = 0;

    private double z = 0;
    private int desiredOutput[] = {0, 1, 1, 1};

    private double sigmoidOutput[] = {1, 1, 1, 1};

    public void calculate() {
        for(int i =0; i < 4; i++) {
            temp = x[i] * w[i];
            z += temp;
        }
        /*
             I want to get the value of 1/(1+e to the power (-z) )
         */
    }

}

2 个答案:

答案 0 :(得分:1)

所以你的代码应该在你在计算方法中写的任何内容后都有这个。

从calculate方法返回结果或全局设置结果或以你想要的方式使用它。

  double d = 1+ Math.exp(-z);
  double result = 1/d;

答案 1 :(得分:0)

按照这里提到的方式Math Exponential

import java.lang.Math;

public class Demo {
        // inputs are declared
        private int x[]={1,0,1,0};
        //weights are declared
        private double w[]={0.4,0.6,0.7,0.8};
        private double temp=0;

        private double z=0;
        private int desiredOutput[]={0,1,1,1};

        private double sigmoidOutput[]={1,1,1,1};

        public void calculate(){
            for(int i =0; i<4; i++){
                temp=x[i]*w[i];
                z+=temp;
            }
            /*
             * I want to get the value of 1/(1+e to the power (-z) )
             */
            double value=(1/(1+Math.exp(-z));
        }
}