我在二进制级别考虑这个问题.
将值为1的浮点值和值为1的整数不编译为(在此省略大量的零)
0001
如果他们两者都编译到这个,那么这种不精确性会在哪里出现.
我正在使用的资源是http://www.cprogramming.com/tutorial/lesson1.html
谢谢.
据Lua说,我注意到了 2 ~= math.sqrt(2) ^ 2
print(2 == math.sqrt(2) ^ 2) --> false
print(2, math.sqrt(2) ^ 2) --> 2 2
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
在Flash中创建一个新的AS3文档,粘贴以下代码并运行它:
var a:Number=0;
trace(a) // 0
a+=0.3;
trace(a) // 0.3
a+=0.3;
trace(a) // 0.6
a+=0.3;
trace(a) // 0.8999999999999999
a+=0.3;
trace(a) // 1.2
a+=0.3;
trace(a) // 1.5
a+=0.3;
trace(a) // 1.8
a+=0.3;
trace(a) // 2.1
a+=0.3;
// ^ This is the output. Notice the inaccuracy starting from 0.9 / 0.89
Run Code Online (Sandbox Code Playgroud)
为什么错误?我只是做一个普通的高分辨率加法.
任何解决方法?
今天我试着编写一个程序来总结用户输入的整数.例如,如果用户输入683它将返回6 + 8 + 3 = 17.
但我在代码中遇到了一些奇怪的问题
using namespace std;
int computeSum(int num);
int computeInteger(int num, int position);
int main()
{
int num;
int sum;
int total;
cin >> num;
total = computeSum(num);
cout << total;
return 0;
}
int computeSum(int num)
{
int position = 10;
int temp = num;
double total = 0;
bool finish = false;
while(!finish)
{
total = total + computeInteger(num, position);
if(num/ position == 0)
break;
position *= …Run Code Online (Sandbox Code Playgroud)