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

问题描述

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



您好,

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

,小数点后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

解决方案



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

解决方案



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

10-13 14:58