对绝对值函数进行错误测试的java代码
时间: 2024-01-29 10:03:16 浏览: 28
以下是对绝对值函数进行错误测试的Java代码示例:
```java
public class AbsoluteValueTest {
public static void main(String[] args) {
// Test case 1: Positive value
double result1 = Math.abs(5);
if(result1 != 5) {
System.out.println("Error: Absolute value of positive number is incorrect");
}
// Test case 2: Negative value
double result2 = Math.abs(-5);
if(result2 != 5) {
System.out.println("Error: Absolute value of negative number is incorrect");
}
// Test case 3: Zero
double result3 = Math.abs(0);
if(result3 != 0) {
System.out.println("Error: Absolute value of zero is incorrect");
}
// Test case 4: NaN
double result4 = Math.abs(Double.NaN);
if(!Double.isNaN(result4)) {
System.out.println("Error: Absolute value of NaN should be NaN");
}
// Test case 5: Positive infinity
double result5 = Math.abs(Double.POSITIVE_INFINITY);
if(result5 != Double.POSITIVE_INFINITY) {
System.out.println("Error: Absolute value of positive infinity is incorrect");
}
// Test case 6: Negative infinity
double result6 = Math.abs(Double.NEGATIVE_INFINITY);
if(result6 != Double.POSITIVE_INFINITY) {
System.out.println("Error: Absolute value of negative infinity is incorrect");
}
}
}
```
这段代码测试了绝对值函数在不同情况下的输出结果,并且对于错误情况输出相应的错误信息。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)