致死剂量计算器程序中的舍入误差
Rounding errors in lethal dose calculator program
我接到了一项任务,要求我根据用户输入的重量找出苏打水的致死剂量(以克为单位)。我在舍入错误方面遇到很多问题......这应该很简单,但不确定为什么我会遇到这么多问题。例如,这里有几个导致问题的输入和输出。提前致谢!
1 1000 275 的输入:应为 [124850000, 356714286] 但发现为 [1.2485e+08, 3.56714e+08]
60 390 130 的输入:应为 [383630, 1096086] 但发现为 [383630, 1.09609e+06]
/**
* @author
* @date 2-2-22
* @file h02.cpp
*/
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
/**
* Finds lethal dose (in grams) of artificial sweetener for a given dieter weight.
* Also finds the number of cans of soda that would be lethal for a given weight.
* @return 0 for success.
*/
int run()
{
// Add your implementation comments here
// Write your code here
const double dose_per_can {.001}; // percentage of lethal sweetener in one can
const int soda_weight {350}; //in grams
const int pound_to_grams {454}; //in grams
double dieter_weight {};
double mouse_weight {};
double mouse_lethal {};
double dieter_lethal {}; // lethal dose in grams to human/dieter
double lethal_ratio {};
float lethal_per_can {}; //how many grams of art sweetener in a can
double dieter_lethal_cans {};
cout << "Enter mouse weight, lethal dose for mouse (grams), and desired wight of dieter (pounds): " << endl;
cin >> mouse_weight >> mouse_lethal >> dieter_weight;
lethal_ratio = mouse_lethal / mouse_weight; //finding the ratio between the dose given and the weight of the mouse
//lets us use this weight ratio on humans
dieter_lethal = lethal_ratio * (dieter_weight * pound_to_grams); //grams of sweetener that is lethal to dieter with given weight
lethal_per_can = soda_weight * dose_per_can; //find how many grams of sweetener in 1 can.. ends up being constant of .35
dieter_lethal_cans = dieter_lethal / lethal_per_can; // finds how many cans would be lethal to dieter
cout << "Lethal dose in grams, cans is [" << dieter_lethal << ", " <<round(dieter_lethal_cans) << "]" << endl;
return 0;
}
round
你使用returns一个浮点值。您需要 lround
- 请参阅此处:https://en.cppreference.com/w/cpp/numeric/math/round
cout << "Lethal dose in grams, cans is [" << lround(dieter_lethal) << ", "
<< lround(dieter_lethal_cans) << "]" << endl;
Lethal dose in grams, cans is [124850000, 356714292]
我接到了一项任务,要求我根据用户输入的重量找出苏打水的致死剂量(以克为单位)。我在舍入错误方面遇到很多问题......这应该很简单,但不确定为什么我会遇到这么多问题。例如,这里有几个导致问题的输入和输出。提前致谢!
1 1000 275 的输入:应为 [124850000, 356714286] 但发现为 [1.2485e+08, 3.56714e+08]
60 390 130 的输入:应为 [383630, 1096086] 但发现为 [383630, 1.09609e+06]
/**
* @author
* @date 2-2-22
* @file h02.cpp
*/
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
/**
* Finds lethal dose (in grams) of artificial sweetener for a given dieter weight.
* Also finds the number of cans of soda that would be lethal for a given weight.
* @return 0 for success.
*/
int run()
{
// Add your implementation comments here
// Write your code here
const double dose_per_can {.001}; // percentage of lethal sweetener in one can
const int soda_weight {350}; //in grams
const int pound_to_grams {454}; //in grams
double dieter_weight {};
double mouse_weight {};
double mouse_lethal {};
double dieter_lethal {}; // lethal dose in grams to human/dieter
double lethal_ratio {};
float lethal_per_can {}; //how many grams of art sweetener in a can
double dieter_lethal_cans {};
cout << "Enter mouse weight, lethal dose for mouse (grams), and desired wight of dieter (pounds): " << endl;
cin >> mouse_weight >> mouse_lethal >> dieter_weight;
lethal_ratio = mouse_lethal / mouse_weight; //finding the ratio between the dose given and the weight of the mouse
//lets us use this weight ratio on humans
dieter_lethal = lethal_ratio * (dieter_weight * pound_to_grams); //grams of sweetener that is lethal to dieter with given weight
lethal_per_can = soda_weight * dose_per_can; //find how many grams of sweetener in 1 can.. ends up being constant of .35
dieter_lethal_cans = dieter_lethal / lethal_per_can; // finds how many cans would be lethal to dieter
cout << "Lethal dose in grams, cans is [" << dieter_lethal << ", " <<round(dieter_lethal_cans) << "]" << endl;
return 0;
}
round
你使用returns一个浮点值。您需要 lround
- 请参阅此处:https://en.cppreference.com/w/cpp/numeric/math/round
cout << "Lethal dose in grams, cans is [" << lround(dieter_lethal) << ", "
<< lround(dieter_lethal_cans) << "]" << endl;
Lethal dose in grams, cans is [124850000, 356714292]