双重偏差 [英] Deviation of double

查看:104
本文介绍了双重偏差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hallo NG


我的问题是浮点数据类型的偏差。 90.625不是

正好是90.625。在我的系统上是90.624999999 ....现在我试图为我的问题找到一个

的解决方案,但我想不出一个。所以我试着写一个

work-arround来解决大多数问题。

你怎么看待这个:(我知道它不是一个完美的解决方案....)


double dValue = ...;


if(dValue> = 10000000000000LL){

nInValue + = 0.01;

}否则if(dValue> = 1000000000000LL){

nInValue + = 0.001;

}否则if(dValue> = 100000000000LL){

nInValue + = 0.0001;

}否则if(dValue> = 10000000000LL){

nInValue + = 0.00001;

}否则if(dValue> = 1000000000){

nInValue + = 0.000001;

}否则if(dValue > = 100000000){

nInValue + = 0.0000001;

}否则if(dValue> = 10000000){

nInValue + = 0.00000001 ;

}否则if(dValue> = 1000000){

nInValue + = 0.000000001;

}否则if(dValue> = 100000) ){

nInValue + = 0.0000000001;

}否则if(dValue> = 1) 0000){

nInValue + = 0.00000000001;

}否则if(dValue> = 1000){

nInValue + = 0.000000000001;
nInValue + = 0.0000000000001;

}否br />
nInValue + = 0.00000000000001;

}否则if(dValue< 0){

nInValue - = 0.000000000000001;

} //如果


Greets Chris

Hallo NG

My problem is the deviation of the floating point datatypes. 90.625 is not
exactly 90.625. It is 90.624999999.... on my system. Now I tried to find a
solution for my problem but I couldn''t think of one. So I tried to write a
work-arround which should solve the problem for the most numbers.
What do you think of this: (I know it is not a perfect solution....)

double dValue = ...;

if (dValue >= 10000000000000LL) {
nInValue += 0.01;
} else if (dValue >= 1000000000000LL) {
nInValue += 0.001;
} else if (dValue >= 100000000000LL) {
nInValue += 0.0001;
} else if (dValue >= 10000000000LL) {
nInValue += 0.00001;
} else if (dValue >= 1000000000) {
nInValue += 0.000001;
} else if (dValue >= 100000000) {
nInValue += 0.0000001;
} else if (dValue >= 10000000) {
nInValue += 0.00000001;
} else if (dValue >= 1000000) {
nInValue += 0.000000001;
} else if (dValue >= 100000) {
nInValue += 0.0000000001;
} else if (dValue >= 10000) {
nInValue += 0.00000000001;
} else if (dValue >= 1000) {
nInValue += 0.000000000001;
} else if (dValue >= 100) {
nInValue += 0.0000000000001;
} else if (dValue >= 10) {
nInValue += 0.00000000000001;
} else if (dValue < 0) {
nInValue -= 0.000000000000001;
} // if

Greets Chris

推荐答案



" Christian Meier" < CH *** @ gmx.ch>在消息中写道

news:c8 ********** @ newshispeed.ch ...

"Christian Meier" <ch***@gmx.ch> wrote in message
news:c8**********@newshispeed.ch...
Hallo NG

我的问题是浮点数据类型的偏差。 90.625是
不完全是90.625。在我的系统上是90.624999999 ....现在我试图找到
解决我的问题,但我想不出一个。所以我试着编写
a work-arround来解决大多数问题。
你怎么看待这个:(我知道这不是一个完美的解决方案....)

double dValue = ...;

if(dValue> = 10000000000000LL){
nInValue + = 0.01;
}否则if(dValue> = 1000000000000LL){
nInValue + = 0.001;
} if if(dValue> = 100000000000LL){
nInValue + = 0.0001;
}否则if(dValue> = 10000000000LL) {
nInValue + = 0.00001;
} if if(dValue> = 1000000000){
nInValue + = 0.000001;
}否则if(dValue> = 100000000){
nInValue + = 0.0000001;
}否则if(dValue> = 10000000){
nInValue + = 0.00000001;
}否则if(dValue> = 1000000){
nInValue + = 0.000000001;
} if if(dValue> = 100000){
nInValue + = 0.0000000001;
}否则if(dValue> = 10000){
nInValue + = 0.00000000001;
} el se if(dValue> = 1000){
nInValue + = 0.000000000001;
}否则if(dValue> = 100){
nInValue + = 0.0000000000001;
}否则if (dValue> = 10){
nInValue + = 0.00000000000001;
}否则if(dValue< 0){
nInValue - = 0.000000000000001;
} //如果
Hallo NG

My problem is the deviation of the floating point datatypes. 90.625 is not exactly 90.625. It is 90.624999999.... on my system. Now I tried to find a solution for my problem but I couldn''t think of one. So I tried to write a work-arround which should solve the problem for the most numbers.
What do you think of this: (I know it is not a perfect solution....)

double dValue = ...;

if (dValue >= 10000000000000LL) {
nInValue += 0.01;
} else if (dValue >= 1000000000000LL) {
nInValue += 0.001;
} else if (dValue >= 100000000000LL) {
nInValue += 0.0001;
} else if (dValue >= 10000000000LL) {
nInValue += 0.00001;
} else if (dValue >= 1000000000) {
nInValue += 0.000001;
} else if (dValue >= 100000000) {
nInValue += 0.0000001;
} else if (dValue >= 10000000) {
nInValue += 0.00000001;
} else if (dValue >= 1000000) {
nInValue += 0.000000001;
} else if (dValue >= 100000) {
nInValue += 0.0000000001;
} else if (dValue >= 10000) {
nInValue += 0.00000000001;
} else if (dValue >= 1000) {
nInValue += 0.000000000001;
} else if (dValue >= 100) {
nInValue += 0.0000000000001;
} else if (dValue >= 10) {
nInValue += 0.00000000000001;
} else if (dValue < 0) {
nInValue -= 0.000000000000001;
} // if




我不确定你想通过做什么来实现这个,那么

的负数呢?问题是许多数字不能完全以浮点格式表示,就像1/3不能完全用单数十进制代表
。摆弄浮点值

只会在非常具体的情况下起作用。如果您的应用程序无法接受浮点数不可避免的四舍五入错误,那么

可能是一个固定点格式更适合您的应用程序。


-

Peter van Merkerk

peter.van.merkerk(at)dse.nl



I''m not sure what you are trying to achieve by doing this, and what about
negative numbers? The problem is that many numbers cannot be exactly
represented in floating point format, just like 1/3 cannot be exactly
represented with just decimal numbers. Fiddling with floating point values
will only help in very specific cases at best. If the inevitable rounding
errors of floating point numbers is not acceptable for your application,
maybe a fixed point format is more appropriate for your application.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl



Christian Meier写道:
Christian Meier wrote:

Hallo NG

我的问题是浮点数据类型的偏差。 90.625不完全是90.625。在我的系统上是90.624999999 ....现在我试图为我的问题找到一个解决方案,但我想不出一个。


原谅我。你的问题是什么?

所以我试着写一个
work-arround来解决大多数问题。
你怎么看待这个:(我知道它不是一个完美的解决方案....)

Hallo NG

My problem is the deviation of the floating point datatypes. 90.625 is not
exactly 90.625. It is 90.624999999.... on my system. Now I tried to find a
solution for my problem but I couldn''t think of one.
Forgive me. What was your problem?
So I tried to write a
work-arround which should solve the problem for the most numbers.
What do you think of this: (I know it is not a perfect solution....)




我看起来不像任何解决方案。


-

Karl Heinz Buchegger
kb ****** @ gascad.at


" Christian Meier" < CH *** @ gmx.ch>写在

news:c8 ********** @ newshispeed.ch:
"Christian Meier" <ch***@gmx.ch> wrote in
news:c8**********@newshispeed.ch:
Hallo NG

我的问题是浮点数据类型的偏差。 90.625不完全是90.625。在我的系统上是90.624999999 ....现在我试着为我的问题找到解决方案,但我想不出一个。所以我试着写一个work-arround来解决大多数数字的问题。你怎么看待这个:(我知道这不是一个完美的解决方案....)
Hallo NG

My problem is the deviation of the floating point datatypes. 90.625 is
not exactly 90.625. It is 90.624999999.... on my system. Now I tried
to find a solution for my problem but I couldn''t think of one. So I
tried to write a work-arround which should solve the problem for the
most numbers. What do you think of this: (I know it is not a perfect
solution....)




(...)


你不能解决工作浮点精度限制。你可以

只使用更多位来获得更好的近似值。一个确切的表示

需要无限量的位,不是吗?

这就是电脑的工作方式,对不起。

但是,您可以使用专门的扩展精度算术库,

就像GNU MP http://www.swox.com/gmp/ 例如。


-

:: bartekd [at] o2 [dot] pl



(...)

You can''t "work around" the floating point precision limitation. You can
only use more bits to have a better approximation. An exact representation
would require an infinite amount of bits, wouldn''t it?
That''s how computers work, sorry.

You can use a specialised extended-precision arithmetics library, though,
like the GNU MP http://www.swox.com/gmp/ for example.

--
:: bartekd [at] o2 [dot] pl


这篇关于双重偏差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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