gpt4 book ai didi

java - 考虑最接近的十分之一将华氏度计算为摄氏度

转载 作者:行者123 更新时间:2023-12-02 08:56:15 25 4
gpt4 key购买 nike

我正在做以下编程练习:Grasshopper Debug.声明如下:

Debug celsius converter

Your friend is traveling abroad to the United States so he wrote a program to convert fahrenheit to celsius. Unfortunately his code has some bugs.

Find the errors in the code to get the celsius converter working properly.

To convert fahrenheit to celsius:

celsius = (fahrenheit - 32) * (5/9)

Remember that typically temperatures in the current weather conditions are given in whole numbers. It is possible for temperature sensors to report temperatures with a higher accuracy such as to the nearest tenth. Instrument error though makes this sort of accuracy unreliable for many types of temperature measuring sensors.

我尝试过以下代码:

public class GrassHopper {

public static String weatherInfo(int temp) {
double c=convertToCelsius(temp);
if (c < 0)
return (c + " is freezing temperature");
else
return (c + " is above freezing temperature");
}

public static double convertToCelsius(int temperature) {
double celsius = (temperature-32)*(5/9.0);
return celsius;
}
}

我们发现了以下案例:

test5
expected:<31.11111111111111[] is above freezing t...> but was:<31.11111111111111[4] is above freezing t...>

我们认为这可能是因为使用了 double。然后我们使用 BigDecimal‽ 编写了相同的解决方案:

import java.math.BigDecimal;
import java.text.*;
public class GrassHopper {

public static String weatherInfo(int temp) {
BigDecimal c=convertToCelsius(BigDecimal.valueOf(temp));
NumberFormat oneDecimal = new DecimalFormat("#0.0");
c=c.toString().contains(".00") ? BigDecimal.valueOf(Double.parseDouble(oneDecimal.format(c))) : c;
if (c.compareTo(BigDecimal.ZERO) < 0)
return (c + " is freezing temperature");
else
return (c + " is above freezing temperature");
}

public static BigDecimal convertToCelsius(BigDecimal temperature) {
BigDecimal celsius = (temperature.subtract(BigDecimal.valueOf(32))).multiply(BigDecimal.valueOf(5/9.0));
return celsius;
}
}

测试结果如下:

test1
expected:<13.333333333333334[] is above freezing t...> but was:<13.333333333333334[4] is above freezing t...>



test5
expected:<-2.222222222222222[3] is freezing tempera...> but was:<-2.222222222222222[4] is freezing tempera...>

要了解发生了什么并尝试解决它,我们已阅读:

你能帮助我们吗‽

编辑:根据@GovindaSakhare提供的答案,我们可以写:

public class GrassHopper {

public static String weatherInfo(int temp) {
System.out.println("original celsius temp: "+temp);
double c=convertToCelsius(temp);
double roundedValue=roundoff(c,15);
String result = "";
if (roundedValue < 0){
result=roundedValue + " is freezing temperature";
}else{
result=roundedValue + " is above freezing temperature";
}
System.out.println("result: "+result);
return result;
}

public static double convertToCelsius(int temperature) {
double celsius = (temperature-32)*(5/9.0);
return celsius;
}

public static double roundoff(double temperature, int precisionLevel) {
double prec = Math.pow(10, precisionLevel);
return Math.round(temperature * prec) / prec;
}
}

但是,我们如何知道应该使用什么精度级别‽

我问这个问题是因为,通过前面的代码,我们观察到以下行为:

一些测试确实通过了:

 test1
Log
temp: 56
result: 13.333333333333334 is above freezing temperature

test2
Log
temp: 23
result: -5.0 is freezing temperature

test4
Log
temp: 5
result: -15.0 is freezing temperature

还有一些没有通过:

test3
original celsius temp: temp: 33

expected:<0.55555555555555[5]6 is above freezing ...> but was:<0.55555555555555[]6 is above freezing ...>

test5
original celsius temp: 54

expected:<12.22222222222222[1] is above freezing t...> but was:<12.22222222222222[3] is above freezing t...>

我们认为它必须与最后一个练习的陈述部分相关:

"...Remember that typically temperatures in the current weather conditions are given in whole numbers. It is possible for temperature sensors to report temperatures with a higher accuracy such as to the nearest tenth. Instrument error though makes this sort of accuracy unreliable for many types of temperature measuring sensors. "

你会如何思考和行动来解决这个困难‽‽

编辑2:在@GovindaSakhare讨论链接之后,我们发现Java中的测试用例与我们预期的不同。我们尝试了用户 vztot 给出的提示,我们使用了 MathContext.DECIMAL128

我们尝试了以下方法:

import java.math.*;
import java.text.*;
public class GrassHopper {

public static String weatherInfo(int temp) {
BigDecimal c=convertToCelsius(BigDecimal.valueOf(temp));
if (c.compareTo(BigDecimal.ZERO) <= 0)
return (c + " is freezing temperature");
else
return (c + " is above freezing temperature");
}

public static BigDecimal convertToCelsius(BigDecimal temperature) {
BigDecimal division = BigDecimal.valueOf(5).divide(BigDecimal.valueOf(9.0),MathContext.DECIMAL128);
BigDecimal subtraction = temperature.subtract(BigDecimal.valueOf(32),MathContext.DECIMAL128);
BigDecimal celsius = subtraction.multiply(division,MathContext.DECIMAL128);
return celsius;
}
}

但是它在所有测试中都给出了错误:

test1
expected:<13.33333333333333[4] is above freezing t...> but was:<13.33333333333333[333333333333333333] is above freezing t...>

test2
expected:<-5.0[] is freezing tempera...> but was:<-5.0[00000000000000000000000000000000] is freezing tempera...>

test3
expected:<0.555555555555555[]6 is above freezing ...> but was:<0.555555555555555[555555555555555555]6 is above freezing ...>

test4
expected:<-15.0[] is freezing tempera...> but was:<-15.0[0000000000000000000000000000000] is freezing tempera...>

test5
expected:<34.44444444444444[] is above freezing t...> but was:<34.44444444444444[444444444444444445] is above freezing t...>

所以,我们决定更好地思考,我们应该使用 double 作为输入参数而不是 int:

public class GrassHopper {

public static String weatherInfo /*🌡️ℹ️*/ (double temp) {
double c = convertToCelsius(temp);
if (c <= 0)
return (c + " is freezing temperature");
else
return (c + " is above freezing temperature");
}

public static double convertToCelsius(double temperature) {
return (temperature - 32) * 5/9.0;
}
}

因此,最后一个版本确实通过了测试。

最佳答案

您可以使用org.apache.commons.math3.util.Precision.equals()来自 Commons Math3。

关于java - 考虑最接近的十分之一将华氏度计算为摄氏度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60472822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com