数学函数以获得所需输出

huangapple go评论64阅读模式
英文:

Math function to get output desired

问题

我目前正在学习基本的数学函数,但在我的作业中无法得到预期的输出。我认为措辞只是让我变得越来越困惑。非常感谢您提前的帮助!以下是问题的布局:

Z = 7

使用数学方法和System.out.println语句来显示:

  • Z的平方根加1
  • 期望输出:7.0710678118654755
  • 我的输出:8

我的代码:

public class tester {

    public static void main(String[] args) {
         int z = 7;
         System.out.println(Math.sqrt(Math.pow(z, 2) + 1));
    }
}
英文:

I am currently learning the basic math functions and I am struggling to get this output for my homework. I think the wording is just making me more and more confused. Thanks in advance for your help! Here is the problem layout:

Z = 7

Use the Math methods and the System.out.println statement to display:

  • the square root of z squared plus 1
  • Output desired: 7.0710678118654755
  • My output: 8

My code:
public class tester {

public static void main(String[] args) {
	 int z = 7;
	 System.out.println(Math.pow(Math.sqrt(z), 2) + 1);
	
}

}

答案1

得分: 2

"the square root of z squared plus 1" 意思是 √(z²+1)。

您当前正在计算 (√z)²+1 — 顺便提一下,这与 z+1 是相同的。这就是为什么您得到 8。

英文:

“the square root of z squared plus 1” means √(z<sup>2</sup>+1).

You are currently calculating (√z)<sup>2</sup>+1 — which incidentally is the same as z+1. Hence why you’re getting 8.

答案2

得分: 1

在这种情况下,对于一个小的整数幂,我会避免使用 Math.pow 方法,而是像这样做:

int z = 7;
System.out.println(Math.sqrt((z*z) + 1));

输出

7.0710678118654755

还要注意,很多时候,int 类型的数学计算不能给出正确的浮点数答案,因为转换为双精度(double)类型是在太晚进行的。在这里,Math.sqrt 方法为你将值转换为了 double 类型,所以没有问题。但在某些情况下,你可能希望将 1 指定为 1.0,以强制进行转换。

另外要注意,由于运算符优先级,(z*z) 可以简写为 z*z。但如果有疑问或为了澄清意思,可以使用括号。

英文:

In this case for a small integral power I would avoid using the Math.pow method and do it like this.

int z = 7;
System.out.println(Math.sqrt((z*z) + 1));

Prints

7.0710678118654755

Also note that quite often, int math doesn't give the correct floating point answer because the conversion to double is done too late. Here the Math.sqrt converts the value to a double for you so there is no problem. But in some cases you would have wanted to specify 1 as 1.0 to force the conversion.

Also note that due to operator precedence, (z*z) could have just been z*z. But when in doubt or to clarify the meaning, use parens.

答案3

得分: 0

"the square root of z squared plus 1"意味着当z为7时,其结果为50,因为7*7 = 49,而49+1 = 50。尝试使用 Math.sqrt(Math.pow(z,2)+1);

英文:

"the square root of z squared plus 1" means the square root of 50 if z is 7, because 7*7 = 49 and 49+1 = 50. Try Math.sqrt(Math.pow(z,2)+1);

huangapple
  • 本文由 发表于 2020年9月8日 06:03:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63784784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定