数学问题 - 非常小的十进制数的假减法 [英] Math problem - False subtraction of very small decimal number

查看:82
本文介绍了数学问题 - 非常小的十进制数的假减法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数学错误宽度'十进制'类型的变量还是其他什么?



您好,

我已经制作了简单的程序,用于计算数字的平方根

,小数点后28位,包含此函数:



  decimal  CalculateSquareRoot( decimal  number)
{
// 声明变量
decimal 结果;
decimal square;
decimal c;
string end;

// 计算初始平方根
square = number * 0 .5M;

// 确定加/子系数
c = FindC(广场);

result = square * square;
end = ResultControl(result,number,c);
while (end!= end
{
while (end == 更大&& c< = square)
{
square = square - c; // !!!问题线!!!
result = square * square;
end = ResultControl(result,number,c);
}
c = c / 10 ;
while (end == smaller
{
square = square + c;
result = square * square;
end = ResultControl(result,number,c);
}
c = c / 10 ;
}
返回平方;
}





算法还可以,但我发现问题,

程序无法正确计算,

简单的数学运算,如减法



在程序代码的第21行(有问题的行)中:



square = square - c;



的价值:



square = 11111111,061111080443055417550

c = 0,0000000000000000000001

square = 11111111,061111080443055417550?不正确



这是不正确的,因为可以看出

计算完全没有完成,因为
计算与其中一个加数相同。



这应该是'square'变量的新值减法运算

square = 11111111,0611110804430554175499



因为这个计算在'while'循环内部

程序因为这个错误而没有响应。



所有变量都是'Decimal'的类型

这个数学运算应该没有错误

因为这个类型可以接受最多28位小数。



此同样的函数在程序中完美运行

变量被声明为'BigInteger的类型',

和那个版本的程序可以计算数字的平方根

所需的小数位数仅限于数量记忆。



我找不到这个错误的模式,这个例子不是孤立的情况。



如果有人有类似的问题,请回答如何解决。



程序是用IDE SharpDevelop ver 4.2.2编写的,C#。



一切顺利,
$ b $bPerićŽeljko

解决方案

十进制精度数字是28-29位有效数字。



您计算的结果(11111111.0611110804430554175499)有31位数且无法正确显示为十进制没有截断。



关于谢菲尔德关于 BigInteger 的建议如果您需要处理非常大的问题,也许是一个解决方案精确。 BigInteger 未设置有效位数的限制。但当然注意是免费的: BigInteger 数字上的算术操作非常慢。


它看起来像是一个完全准确的浮动结果 - 点数表示。我希望你能理解有真实的数字,它们本质上是无限的对象,以及它们的计算机近似值,例如浮点数。如果从这里开始,您可以了解更多信息: http://en.wikipedia.org/wiki/Floating-point [ ^ ]。



类似 BigInteger 的类型具有非常不同的性质。很有兴趣看看它们是如何实现的,你可以这样做,例如,如果你下载Mono实现的源代码并研究它。



- SA

Math error width 'Decimal' type of variables or something else ?

Hello ,
I have made simple program for calculating square root of numbers
at 28 decimal places, that contains this function :

decimal CalculateSquareRoot(decimal number)
{
   // declare variables
   decimal result;
   decimal square;
   decimal c;
   string end;		
			
   // calculate initial square root
   square = number * 0.5M;
			
   // determine add/sub coefficient
   c = FindC(square);
			
   result = square * square;
   end = ResultControl(result,number,c);
   while(end != "end")
   {
	while(end == "bigger" && c<=square)
	{
		square = square - c;    // !!! problematic line !!!
		result = square * square;
		end = ResultControl(result,number,c);
	}
	c = c / 10;
	while(end == "smaller")
	{
		square = square + c;
		result = square * square;
		end = ResultControl(result,number,c);
	}
	c = c / 10;
   }
   return square;
}



Algorithm is ok but I have found problem,
program do not calculate correct,
simple math operation such as subtraction

In the line 21 ( problematic line ) of program code :

square = square - c;

for values :

square = 11111111,061111080443055417550
c = 0,0000000000000000000001
square = 11111111,061111080443055417550 ? incorrect

This is incorrect, and as it can be seen it seems that
calculation is not done at all because the result of
calculation is the same as one of the addends.

This should be new value of 'square' variable afther subtraction operation
square = 11111111,0611110804430554175499

Since this calculation is inside the 'while' loop
program becomes unresponsive because of this error.

All variables are type of 'Decimal'
and this math operation should go without errors
since this type can accept numbers up to 28 decimals.

Also this same function works perfectly inside program where
variables are declared as type of 'BigInteger',
and that version of program can calculate square root of numbers
at wanted number of decimal places restricted only by quantity of memory.

I can't find pattern for this error and this example is not isolated case.

If someone had similar problem please answer how to solve it.

Program is written in IDE SharpDevelop ver 4.2.2, C#.

All the best,
Perić Željko

解决方案

Precision of decimal number is 28-29 significant digits.

Number you are expecting as result of your calculation (11111111.0611110804430554175499) has 31 digits an cannot be correctly represented as decimal without truncation.

Following Sergey suggestion about BigInteger perhaps is a solution if you need to deal with very big precision. BigInteger dos not set limit for number of significant digits. But of course noting is free: arithmetic operations on BigInteger numbers are very slow.


It looks like a perfectly accurate result for floating-point presentation of numbers. I hope you understand that there are real numbers, which are infinite objects by they natures, and their computer approximations, such as floating-point. You can learn more about it if you start here: http://en.wikipedia.org/wiki/Floating-point[^].

The types like BigInteger have a very different nature. It's pretty interested to see how they are implemented, which you can do, for example, if you download source code of Mono implementation and study it.

—SA


这篇关于数学问题 - 非常小的十进制数的假减法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆