English 中文(简体)
区分按方法归还的长处——错误的价值观
原标题:Dividing longs which are returned by method - wrong values
最佳回答

如果你尝试这一方法:

public static long pow(int x, int n) {
    long p = x;
    System.out.println("Pow: "+x+","+n);
    for (int i = 1; i < n; i++) {
        p *= x;
        System.out.println(p);
    }
    return p;
}

You get this output:

...
Pow: 10,20
100
1000
10000
...
...
1000000000000000
10000000000000000
100000000000000000
1000000000000000000
-8446744073709551616
7766279631452241920

长期价值超支:10^20太大,无法长期适应。

Methods pow and fact must return long and I must use them in exp (college assignment).

Then there is not much you can do to fix it. You could throw an exception if eps is too small.

问题回答

<条码>x通常有多大? 这可能是累进。 更改<代码>int中的所有论点:powfact,改为long

长期的数据类型可能无法精确地处理,因此,你对数值的重新估价是错误的。 为什么你只是要恢复双重价值观?

<><>Edit>: 我在此提出:

  public static long pow(int x, int n) 
  {
    double p = x;
    for (int i = 1; i < n; i++) {
      p *= x;
    }
    return (long)p;
  }

  public static long fact(int n) 
  {
    double s = n;
    for (int i = 1; i < n; i++ ) {
      s *= i;
    }
    return (long)s;
  }


  public static void exp(int x, double eps) 
  {
    double pow = 1.0;
    double fact = 1.0;
    double sum = 0.0;
    double temp;
    for(int ii=1; ii < 100; ii++)
    {
      pow = pow(x, ii);
      fact = fact(ii);
      temp = (double)pow/(double)fact;
      temp = temp == 1 ? 0 : temp;
      sum += temp;
    }

    System.out.println("Check: " + Math.exp(x));
    System.out.println("My: " + sum);
  }

  public static void main(final String[] args)
  {
    int x = 10;
    double eps = 0.0000000000001;

    exp(x, eps);
  }

这关系到你们最接近的人,不使用mal子。

Check: 22026.465794806718
My: 21946.785573087538




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...