找出浮子的精度 [英] finding out the precision of floats

查看:46
本文介绍了找出浮子的精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我想以平台无关的方式知道浮动

的精度(有效位数)。我已经浏览了文档,但是

我找不到任何关于它的东西!


目前我使用这个可怕的替代品:


FLOAT_PREC = repr(1.0 / 3).count(''3'')


我如何正确地做到这一点或者哪里有相关的部分? docs


谢谢


-

Arnaud

Hi all,

I want to know the precision (number of significant digits) of a float
in a platform-independent manner. I have scoured through the docs but
I can''t find anything about it!

At the moment I use this terrible substitute:

FLOAT_PREC = repr(1.0/3).count(''3'')

How can I do this properly or where is relevant part of the docs?

Thanks

--
Arnaud

推荐答案

2月25日9:57 pm,Arnaud Delobelle < arno ... @ googlemail.comwrote:
On Feb 25, 9:57 pm, "Arnaud Delobelle" <arno...@googlemail.comwrote:

大家好,


我想知道精度(数字)浮动的有效数字)

以独立于平台的方式。我已经浏览了文档,但是

我找不到任何关于它的东西!


目前我使用这个可怕的替代品:


FLOAT_PREC = repr(1.0 / 3).count(''3'')
Hi all,

I want to know the precision (number of significant digits) of a float
in a platform-independent manner. I have scoured through the docs but
I can''t find anything about it!

At the moment I use this terrible substitute:

FLOAT_PREC = repr(1.0/3).count(''3'')



我有点困惑:


你似乎不想要一个能告诉你特定数字中实际数字

的有效十进制数字的函数,例如


nsig(12300.0)-3

nsig(0.00123400)-4




你似乎试图确定平台的实现提供的最大有效小数位数是多少?
的Python'浮点类型。 Python是否在一个平台上实现?
*不能使用IEEE 754 64位FP作为浮点数的内存格式?


干杯,

John

I''m a little puzzled:

You don''t seem to want a function that will tell you the actual number
of significant decimal digits in a particular number e.g.

nsig(12300.0) -3
nsig(0.00123400) -4
etc

You appear to be trying to determine what is the maximum number of
significant decimal digits afforded by the platform''s implementation
of Python''s float type. Is Python implemented on a platform that
*doesn''t* use IEEE 754 64-bit FP as the in-memory format for floats?

Cheers,
John


2月25日上午11点20分,John Machin < sjmac ... @ lexicon.netwrote:

[...]
On Feb 25, 11:20 am, "John Machin" <sjmac...@lexicon.netwrote:
[...]

我有点不解:


你似乎不想要一个能告诉你实际数字的函数

特定数字中的有效小数位数,例如

nsig(12300.0)-3

nsig(0.00123400)-4




你似乎是试图确定平台的实现所提供的最大有效小数是多少,这是Python的浮点类型的

I''m a little puzzled:

You don''t seem to want a function that will tell you the actual number
of significant decimal digits in a particular number e.g.

nsig(12300.0) -3
nsig(0.00123400) -4
etc

You appear to be trying to determine what is the maximum number of
significant decimal digits afforded by the platform''s implementation
of Python''s float type.



是的,你是对的。

Yes you are correct.


Python在平台上实现

*不能使用IEEE 754 64位FP作为浮点数的内存格式吗?
Is Python implemented on a platform that
*doesn''t* use IEEE 754 64-bit FP as the in-memory format for floats?



我不懂IEEE 754 64位FP。 python doc说

浮点数是使用C''double''数据类型实现的,但是我并没有意识到这个跨平台有一个标准。


感谢您澄清这一点。正如我的问题显示我不熟悉

浮点运算!


看看IEEE 754的定义,尾数是由53

有效二进制数字,这意味着

53 * log10(2)= 15.954589770191003有效十进制数字

(我以前的狡猾计算得到了16)。


这是否意味着可以安全地假设这可以存在于任何

平台上?


-

Arnaud

I had no knowledge of IEEE 754 64-bit FP. The python doc says that
floats are implemented using the C ''double'' data type but I didn''t
realise there was a standard for this accross platforms .

Thanks for clarifying this. As my question shows I am not versed in
floating point arithmetic!

Looking at the definition of IEEE 754, the mantissa is made of 53
significant binary digits, which means
53*log10(2) = 15.954589770191003 significant decimal digits
(I got 16 with my previous dodgy calculation).

Does it mean it is safe to assume that this would hold on any
platform?

--
Arnaud


2月25日晚上11:06,Arnaud Delobelle < arno ... @ googlemail.com>

写道:
On Feb 25, 11:06 pm, "Arnaud Delobelle" <arno...@googlemail.com>
wrote:

2月25日上午11点20分,John Machin ; < sjmac ... @ lexicon.netwrote:

[...]
On Feb 25, 11:20 am, "John Machin" <sjmac...@lexicon.netwrote:
[...]

我有点不解:
I''m a little puzzled:


您似乎不想要一个能告诉您实际数字的函数

特定数字中有效小数位数例如
You don''t seem to want a function that will tell you the actual number
of significant decimal digits in a particular number e.g.


nsig(12300.0)-3

nsig(0.00123400)-4

etc
nsig(12300.0) -3
nsig(0.00123400) -4
etc


您似乎在试图确定平台提供的最大有效小数位数是多少?实现

的Python'浮点类型。
You appear to be trying to determine what is the maximum number of
significant decimal digits afforded by the platform''s implementation
of Python''s float type.



是的,你是对的。


Yes you are correct.


Python在平台上实现

*不能使用IEEE 754 64位FP作为浮点数的内存格式吗?
Is Python implemented on a platform that
*doesn''t* use IEEE 754 64-bit FP as the in-memory format for floats?



我不懂IEEE 754 64位FP。 python doc说

浮点数是使用C''double''数据类型实现的,但是我并没有意识到这个跨平台有一个标准。


感谢您澄清这一点。正如我的问题显示我不熟悉

浮点运算!


看看IEEE 754的定义,尾数是由53

有效二进制数字,这意味着

53 * log10(2)= 15.954589770191003有效十进制数字

(我以前的狡猾计算得到了16)。


是否可以安全地假设这可以在任何

平台上持有?


I had no knowledge of IEEE 754 64-bit FP. The python doc says that
floats are implemented using the C ''double'' data type but I didn''t
realise there was a standard for this accross platforms .

Thanks for clarifying this. As my question shows I am not versed in
floating point arithmetic!

Looking at the definition of IEEE 754, the mantissa is made of 53
significant binary digits, which means
53*log10(2) = 15.954589770191003 significant decimal digits
(I got 16 with my previous dodgy calculation).

Does it mean it is safe to assume that this would hold on any
platform?



显然不是;这里有一些我们都需要(ed)阅读的文档:

http://docs.python.org/tut/node16.html

"""

今天几乎所有机器(2000年11月)使用IEEE-754浮点数
算术,几乎所有平台都将Python浮点数映射到IEEE-754

双精度。

"""

我很想知道2000年11月的例外情况和

如果它们仍然存在的话。还有一个问题是,对你来说多重要。

。假定表示是64位,即使从尾数中取3位

并将它们捐赠给指数,也会给你带来15.05十进制数字 - 或许你可以假设你已经得到了

至少15位十进制数字。


虽然我们正在等待大师回答,但这里的常规是 s $ / b $ b比你的狡猾一点:


| >>对于范围(200)中的n:

| ... if(1.0 + 1.0 / 2 ** n)== 1.0:

| ...打印n,位

| ......休息

| ......

| 53位


至少这种方法不依赖于平台的C库。

请仔细注意该教程部分的结束语:

"""

(好吧,将显示在任何符合754标准的平台上,它可以做到最好 -

可能的输入和输出转换C库 - 你的可能

不是!)。

"""


我希望其中一些有用...

干杯,

John

Evidently not; here''s some documentation we both need(ed) to read:

http://docs.python.org/tut/node16.html
"""
Almost all machines today (November 2000) use IEEE-754 floating point
arithmetic, and almost all platforms map Python floats to IEEE-754
"double precision".
"""
I''m very curious to know what the exceptions were in November 2000 and
if they still exist. There is also the question of how much it matters
to you. Presuming the representation is 64 bits, even taking 3 bits
off the mantissa and donating them to the exponent leaves you with
15.05 decimal digits -- perhaps you could assume that you''ve got at
least 15 decimal digits.

While we''re waiting for the gurus to answer, here''s a routine that''s
slightly less dodgy than yours:

| >>for n in range(200):
| ... if (1.0 + 1.0/2**n) == 1.0:
| ... print n, "bits"
| ... break
| ...
| 53 bits

At least this method has no dependency on the platform''s C library.
Note carefully the closing words of that tutorial section:
"""
(well, will display on any 754-conforming platform that does best-
possible input and output conversions in its C library -- yours may
not!).
"""

I hope some of this helps ...
Cheers,
John


这篇关于找出浮子的精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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