Floating point precision is limited in C++
and for novice programmers that often causes trouble when rounding values.
在教授学生时,展示浮动点精确数问题往往是有益的。 您知道的以何种可能的方式在所有<条码>上不断展示这种四舍五入的问题。 C++汇编者?
Floating point precision is limited in C++
and for novice programmers that often causes trouble when rounding values.
在教授学生时,展示浮动点精确数问题往往是有益的。 您知道的以何种可能的方式在所有<条码>上不断展示这种四舍五入的问题。 C++汇编者?
首先,我们应指出,在1.5
、0.5
和2.0
中,所有各条都得到了确切的表述。 因此,在这一具体例子中,1.5
将永远不会有1.4999999999999999999
。
尽管如此,我认为必须做的是,让学生了解人数是1.1
。
这里是一个样本方案:
#include <iostream>
#include <iomanip>
int main() {
std::cout << std::setprecision(30);
double d1(1.1);
std::cout << d1 << "
";
double d2(11);
double eps = d2/10 - d1;
std::cout << d2 << "
";
std::cout << eps << "
";
bool equal = (d1 == d2);
std::cout << equal << "
";
}
也许你可以通过这一方案走动,谨慎地说,d1
和d2
。 页: 1
对于高中学生来说,你可以穿过教派的双轨算法,看为什么<1>2<>>>> /代码>是可代表的,但<代码>1/10不是。
EDIT:我想到,回家的方法是比较重复复数分数和重复本金分数。 学习你的学生<代码>1/7。 董事会中的长期分歧。 指出你不能在确切使用有限资源的情况下书写<代码>1/7<>/code>。 然后,要么显示它们如何将<代码>1/10<>>>>>作为双向部分,要么简单地告诉它们,你要么利用有限的资源来书写。
指出浮标为定点(32比特),两倍为定点(64比特)。 也许会引入pi本,并且说,你能够代表一个无限的字长(如所有理性)。
无论你试图做什么,请在这里汇报,让我们知道它是如何运作的。
你们可以利用这一榜样:
#include <iostream>
using namespace std;
int main() {
for (double d = 0.0; d != 1.0; d += 0.1)
cout << d << "
";
}
该方案永远不会终止,因为从来就等于1。
我喜欢以下例子:
double sum = 0;
for (int i = 0; i < 10; i++, sum += 0.1);
cout << "sum = " << sum << endl;
cout << "(sum == 1.) is " << boolalpha << (sum == 1.) << endl;
产出如下:
sum = 1
(sum == 1.) is false
矛盾的原因是点计算。
printf("%.20f
",0.1f);
或
cout << fixed << setprecision(20) << 0.1f << endl;
:
举这个例子:
#include <cstdio>
int main() {
printf("%.20lf rounded to two decimal places is %.2lf
", 0.075, 0.075);
return 0;
}
印刷
0.07499999999999999722 rounded to two decimal places is 0.07
请注意,0.075四舍五入至两个小数点应为0.08,而不是我们在该产出中看到的0.07。 这一例子清楚地表明了双管齐下的问题。
I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...
I have been searching for sample code creating iterator for my own container, but I haven t really found a good example. I know this been asked before (Creating my own Iterators) but didn t see any ...
Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...
I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?
Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...
Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...
I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...
Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?