简单的平方根
Simple Square Rooter
我在C++类之间,所以为了保持练习,我写了一个程序来寻找根源。
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double input;
bool done = false;
static double check;
while (done == false)
{
cout << "what number to root? ";
cin >> input;
bool foundRoot = false;
check = input;
while (foundRoot == false)
{
if (check * check == input || check*check < input)
{
foundRoot = true;
//done = true;
}
else
{
check = check - .00001;
}
}
cout << "The root of " << input << " is " << setprecision(7) << check << endl;
}
}
这个程序似乎为任何平方测试返回正确的值(即使是非完全的(;但是输入"25"返回4.9999~。为什么?
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double input;
bool done = false;
static double check;
cout << "what number to root? ";
cin >> input;
bool foundRoot = false;
check = input;
while (!foundRoot)
{
if (check * check <= input)
{
foundRoot = true;
}
else
{
check = check - .000001;
}
}
cout << "The root of " << input << " is " << setprecision(7) << check << endl;
}
上面的代码给出了正确的输出。我只是提高了精度。您正在使用蛮力方法来查找根。你使用一些复杂的方法怎么样,比如牛顿方法。
相关文章:
- 在c++中用vector填充一个简单的动态数组
- (C++)分析树以计算返回错误值的简单算术表达式
- 我的简单if-else语句是如何无法访问的代码
- 使用简单类型列表实现的指数编译时间.为什么
- 如何在BST的这个简单递归实现中消除警告
- 一种在C++中读取TXT配置文件的简单方法
- 关于简单C++函数(is_palindrome)的逻辑的问题
- 显示错误输出的简单数组排序程序
- 当无法使用模板和宏时,生成类型变体C++代码的最简单方法是什么?
- 退出简单while循环时出现问题
- 为什么简单的算术减法在"if"条件下不起作用?
- C++-字符串是否包含一个带有简单循环的单词
- 关于 c++ 函数中指针赋值的简单问题
- 从函数返回任意简单类型的数据
- 如何在没有函数的情况下编写此代码并使C++更简单?
- 有没有办法简单地从 GPU 调用多个 cpp 输出文件?
- 在简单示例中,Python3 + ctypes 回调会导致内存泄漏
- 当简单捕获中的标识符显示为参数的声明符 ID 时,没有编译器诊断
- 如何使用 samtools C API 构建一个简单的主.cpp文件
- 简单的平方根