为什么Math.Log(8,2)是2.9999999999999996而不是3 [英] why Math.Log(8,2) is 2.9999999999999996 and not 3

查看:103
本文介绍了为什么Math.Log(8,2)是2.9999999999999996而不是3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Guru,

这是一个问题,我无法推断它是如何发生的?我使用math.log写了

一个dll。在应用程序A中使用时,这个DLL工作

罚款,但不适用于应用程序B.在调试时我咬了指甲,

当我在即时窗口看到结果时。 />

?Math.Log(8,2)

2.9999999999999996


目前我不再使用它,但是喜欢要知道是否有人面对这样的情绪。我知道

中没有任何覆盖用于这两个可执行文件。

谢谢

Wave ...

Hello Gurus,
Here is a problem, where i am unable to reason HOW it happens? I wrote
a dll using math.log. This dll when used within application A works
fine, but not with application B. On debugging I was biting my nails,
when i saw result in immediate window.

?Math.Log(8,2)
2.9999999999999996

Currently i am not using it anymore, but like to know if anybody faced
such a situtation. I am aware there were no overrides being used in
any of those two executables.
Thanks
Wave...

推荐答案



" Whidbey Wave" <在********* @ yahoo.com>在消息新闻中写道:77 ************************** @ posting.google.c om ...

"Whidbey Wave" <in*********@yahoo.com> wrote in message news:77**************************@posting.google.c om...
你好大师,
这是一个问题,我无法推断它发生的原因?我用math.log写了一个dll。在应用程序A中使用时,此dll工作正常,但不适用于应用程序B.在调试时,我正在咬指甲,
当我在即时窗口看到结果时。

?Math .Log(8,2)
2.9999999999999996

目前我不再使用它,但想知道是否有人面对这样的情绪。我知道
任何这两个可执行文件都没有覆盖。

谢谢
Wave ...
Hello Gurus,
Here is a problem, where i am unable to reason HOW it happens? I wrote
a dll using math.log. This dll when used within application A works
fine, but not with application B. On debugging I was biting my nails,
when i saw result in immediate window.

?Math.Log(8,2)
2.9999999999999996

Currently i am not using it anymore, but like to know if anybody faced
such a situtation. I am aware there were no overrides being used in
any of those two executables.
Thanks
Wave...




相当常见的问题,但通常是以下形式:

为什么21.9 + 3.1不完全等于25?


问题是指问题。是计算机(*任何*计算机,它不是

具体.net相关甚至微软相关)处理

浮点数。你不能使用

浮点数得到确切的结果!


例如:尝试在有限数中指定1/3 * * br />
的小数,因此将它乘以3恰好为1.


Hans Kesting



Fairly frequently asked question, though usually in the form of:
"why is 21.9 + 3.1 not exactly equal to 25?"

The "problem" is how computers (*any* computer, it''s not
specifically .net related or even microsoft related) handle
floating point numbers. You CAN''T get exact results using
floating points!

As an example: try to specify 1/3 *exactly* in a finite number
of decimals, so that multiplying it by 3 gives exactly 1.

Hans Kesting


I认为他的意思是当应用程序A显示正确的结果时,为什么它会在应用程序B上发生.3。

我猜你不会在两者中都使用Math.Log(8,2) place,但是将变量作为浮点数或双精度数传递,虽然在两个地方看起来都相同,但在发送到Math.Log之前处理稍有不同。


-

快乐的编码!

Morten Wennevik [C#MVP]
I think he meant why is it happening on Application B when Application A shows the correct result, 3.
I''m guessing that you don''t use Math.Log(8,2) in both places, but pass the variables as floats or doubles that although may seem equal in both places, have been processed slightly different before being sent to Math.Log.

--
Happy coding!
Morten Wennevik [C# MVP]




" Morten Wennevik" <莫************ @ hotmail.com>在消息新闻中写道:opr9xsacxpklbvpo@morten_x.edunord ...

"Morten Wennevik" <Mo************@hotmail.com> wrote in message news:opr9xsacxpklbvpo@morten_x.edunord...
我认为他的意思是当应用程序A显示正确的结果时,为什么会在应用程序B上发生,3。
我是猜测你不会在两个地方都使用Math.Log(8,2),但是将变量作为浮点数或双精度数传递,虽然看起来两个地方的
相等,但在发送之前处理稍有不同Math.Log。
-
快乐的编码!
Morten Wennevik [C#MVP]
I think he meant why is it happening on Application B when Application A shows the correct result, 3.
I''m guessing that you don''t use Math.Log(8,2) in both places, but pass the variables as floats or doubles that although may seem equal in both places, have been processed slightly different before being sent to Math.Log.
--
Happy coding!
Morten Wennevik [C# MVP]




好​​的,你是对的。< br *>
我*应该添加的内容是打印结果还可以取决于完成的方式:aprint结果可能会自动完成(比如说)六位小数,在这种情况下,

真实 2.999999999996的结果可* *显示*

为3。因此,计算可以得到完全相同的值

(如果输入值相同则应该如此)但是显示的

结果可能会有所不同,如果以不同的方式完成(我猜测

应用程序A正在某处打印结果。应用程序B中的

结果在调试器中查看)


Hans Kesting



Okay, you are right.
What I *should* have added is that the printed result can also
depend on the way that is done: a "print" of the result might
automatically be done in (say) six decimals, in which case
the "real" result of 2.999999999996 can be *displayed*
as "3". So the calculation can result in exactly the same value
(as it should, if the inputvalues are the same) but the displayed
results can differ, if done in different ways (I''m guessing that
Application A was printing the result somewhere. The
result in Application B was viewed in the debugger)

Hans Kesting


这篇关于为什么Math.Log(8,2)是2.9999999999999996而不是3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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