Python不能分割??!?! [英] Python can't divide??!?!

查看:46
本文介绍了Python不能分割??!?!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯......


这很奇怪。对不起,如果它已经知道(怎么可能不知道,我想知道?)但

我在任何地方都没有看到它的任何参考。


我在Windows XP Pro,i386架构上运行最新的Python(2.3.3)。


点火蟒蛇等等。


请执行以下操作:结果:


3/5 0足够公平,int / int = int

3 / 5.0 0.59999999999999998嗯?

3.0 / 3 0.59999999999999998 ummmm ...

3.0 / 5.0 0.59999999999999998我怎么猜...


这只是一个例子。 Python无法分裂。期。如果你试试1.0 / 3你

得到0.33333333333333331,从这个,上面的例子,以及一些测试,

我推测*东西*从中减去2最后一个数字*在*

之后完成了分工。


古怪。


现在,这只是我,或其他人可以复制它吗?我在这两台计算机上试过它,但它们都是Win XP。我准备在Linux上测试它,但是我想要我先写下这个。


Dan

Ummmm...

This is weird. Sorry if it''s known about (how can it NOT be, I wonder?) but
I haven''t seen any reference to it anywhere.

I''m running the latest Python (2.3.3) on Windows XP Pro, i386 architecture.

Fire up python or whatever.

Do this: Result:

3 / 5 0 Fair enough, int / int = int
3 / 5.0 0.59999999999999998 eh?
3.0 / 3 0.59999999999999998 ummmm...
3.0 / 5.0 0.59999999999999998 how did I guess...

That''s just an example. Python cannot divide. Period. If you try 1.0 / 3 you
get 0.33333333333333331, and from this, the above example, and a few tests,
I speculate that *something* is subtracting 2 from the last digit *after*
the division is done.

Wacky.

Now, is this just me, or can someone else duplicate it? I''ve tried it on two
computers here, but they''re both Win XP. I''m about to test it on Linux but I
figured I''d write this first.

Dan

推荐答案

在这里工作正常:
Works fine here:
a = 3/5
b = 3 / 5.0
c = 3.0 / 3
d = 3.0 / 5.0

打印%f%f%f%f ; %(a,b,c,d)
0.000000 0.600000 1.000000 0.600000




打印"%。17f%。17f%。 17f%.17f %(a,b,c,d)
a = 3/5
b = 3/5.0
c = 3.0/3
d = 3.0/5.0

print "%f %f %f %f" % (a,b,c,d) 0.000000 0.600000 1.000000 0.600000

but
print "%.17f %.17f %.17f %.17f" % (a,b,c,d)


0.00000000000000000 0.59999999999999998 1.00000000000000000

0.59999999999999998

;)


Stephen

Dan Williams写道:

嗯...

这很奇怪。对不起如果它已经知道(怎么可能不知道,我想知道?)但是
我在任何地方都没有看到任何引用它。

我正在运行最新的Python(2.3.3)在Windows XP Pro,i386架构上。

启动python或其他任何东西。

这样做:结果:

3/5 0足够公平,int / int = int
3 / 5.0 0.59999999999999998嗯?
3.0 / 3 0.59999999999999998嗯......
3.0 / 5.0 0.59999999999999998我怎么猜...

这只是一个例子。 Python无法分裂。期。如果你试试1.0 / 3,你就得到0.33333333333333331,从这里,上面的例子,以及一些测试,
我推测* *之后*从*
分工完成了。

古怪。

现在,这只是我,还是其他人可以复制它?我在这两台计算机上试过它,但它们都是Win XP。我准备在Linux上测试它,但我想我会先写这个。

Dan

0.00000000000000000 0.59999999999999998 1.00000000000000000
0.59999999999999998

;)

Stephen
Dan Williams wrote:
Ummmm...

This is weird. Sorry if it''s known about (how can it NOT be, I wonder?) but
I haven''t seen any reference to it anywhere.

I''m running the latest Python (2.3.3) on Windows XP Pro, i386 architecture.

Fire up python or whatever.

Do this: Result:

3 / 5 0 Fair enough, int / int = int
3 / 5.0 0.59999999999999998 eh?
3.0 / 3 0.59999999999999998 ummmm...
3.0 / 5.0 0.59999999999999998 how did I guess...

That''s just an example. Python cannot divide. Period. If you try 1.0 / 3 you
get 0.33333333333333331, and from this, the above example, and a few tests,
I speculate that *something* is subtracting 2 from the last digit *after*
the division is done.

Wacky.

Now, is this just me, or can someone else duplicate it? I''ve tried it on two
computers here, but they''re both Win XP. I''m about to test it on Linux but I
figured I''d write this first.

Dan



Dan Williams < da*@ithium.net>在消息新闻中写道:< ma *************************************** @ py thon .org> ...
"Dan Williams" <da*@ithium.net> wrote in message news:<ma***************************************@py thon.org>...
嗯...

这很奇怪。对不起,如果它已经知道(怎么可能不知道,我想知道?)但是
我还没有看到任何参考。


然后你看上去还不够。

我在Windows XP Pro,i386上运行最新的Python(2.3.3)建立。

启动python等等。

执行此操作:结果:

3/5 0足够公平,int / int = int


这是为了与旧版本的Python兼容。强烈建议所有新的Python脚本都使用来自__future__

导入部门。

3 / 5.0 0.59999999999999998嗯?
3.0 / 3 0.59999999999999998 ummmm ...
3.0 / 5.0 0.59999999999999998我怎么猜...


当你记得计算机在数学中做数学时,这是完全合理的br />
二进制。在二进制中,3/5等于0.1 0011 0011 0011 0011 0011

0011 ...,其舍入为(C99表示法)0x1.33333333333333333p-1。

这个确切的十进制等值是

0.599999999999999977795539507496869191527366638183 59375.在

默认的17位有效数字格式中,这一轮是

0.59999999999999998。

这只是一个例子。 Python无法分裂。期。


每个quintillion只有37个零件。

如果你尝试1.0 / 3你会得到0.33333333333333331,


更确切地说,你得到0x1.5555555555555p-2,或者

0.333333333333333314829616256247390992939472198486 328125.

从这里,上面的例子,以及一些测试,

*没有*从最后一位数减去因为你的浮动

部分硬件没有十进制数字的概念,只有位数。

现在,这只是我,还是别人可以复制它?
Ummmm...

This is weird. Sorry if it''s known about (how can it NOT be, I wonder?) but
I haven''t seen any reference to it anywhere.
Then you haven''t been looking hard enough.
I''m running the latest Python (2.3.3) on Windows XP Pro, i386 architecture.

Fire up python or whatever.

Do this: Result:

3 / 5 0 Fair enough, int / int = int
This is for bug-compatibility with old versions of Python. It is
strongly recommended that all new Python scripts use "from __future__
import division".
3 / 5.0 0.59999999999999998 eh?
3.0 / 3 0.59999999999999998 ummmm...
3.0 / 5.0 0.59999999999999998 how did I guess...
It makes perfect sense when you remember that computers do math in
binary. In binary, 3/5 is equal to 0.1 0011 0011 0011 0011 0011
0011..., which is rounded to (C99 notation) 0x1.33333333333333333p-1.
The exact decimal equivalent of this is
0.599999999999999977795539507496869191527366638183 59375. In the
default 17 significant digits format, this rounds to
0.59999999999999998.
That''s just an example. Python cannot divide. Period.
It''s off by only 37 parts per quintillion.
If you try 1.0 / 3 you get 0.33333333333333331,
More precisely, you get 0x1.5555555555555p-2, or
0.333333333333333314829616256247390992939472198486 328125.
and from this, the above example, and a few tests,
I speculate that *something* is subtracting 2 from the last digit *after*
the division is done.
*Nothing* is subtracting from the last digit because your floating
part hardware has no concept of decimal digits, only bits.
Now, is this just me, or can someone else duplicate it?




世界上每个拥有二进制浮动部件硬件的人都可以

复制它,无论他们是否使用Python。



Everyone in the world who has binary floating part hardware can
duplicate it, whether they''re using Python or not.


Dan Williams写道:
Dan Williams wrote:
启动python或者其他什么。

这样做:结果:

3/5 0足够公平,int / int = int
3 / 5.0 0.59999999999999998嗯?
3.0 / 3 0.59999999999999998嗯......
3.0 / 5.0 0.59999999999999998我怎么猜...

这只是一个例子。 Python无法分裂。期间。
Fire up python or whatever.

Do this: Result:

3 / 5 0 Fair enough, int / int = int
3 / 5.0 0.59999999999999998 eh?
3.0 / 3 0.59999999999999998 ummmm...
3.0 / 5.0 0.59999999999999998 how did I guess...

That''s just an example. Python cannot divide. Period.




C,C ++,Java,Perl或其他任何使用浮点的无数b / b
编程语言都不能。


-

__ Erik Max Francis&& ma*@alcyone.com && http://www.alcyone.com/max/

/ \美国加利福尼亚州圣何塞市&& 37 20 N 121 53 W&& & tSftDotIotE

\ __ /我以为我可能再也见不到周六晚了。

- Robert S. MacNamara



Neither can C, C++, Java, Perl, or any of the other countless
programming languages which use floating point.

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ I thought I might never see another Saturday night.
-- Robert S. MacNamara

这篇关于Python不能分割??!?!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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