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 23 2235
On Feb 25, 9:57 pm, "Arnaud Delobelle" <arno...@google mail.comwrote:
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')
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
On Feb 25, 11:20 am, "John Machin" <sjmac...@lexic on.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
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.
Is Python implemented on a platform that
*doesn't* use IEEE 754 64-bit FP as the in-memory format for floats?
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.954589770191 003 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
On Feb 25, 11:06 pm, "Arnaud Delobelle" <arno...@google mail.com>
wrote:
On Feb 25, 11:20 am, "John Machin" <sjmac...@lexic on.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
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.
Is Python implemented on a platform that
*doesn't* use IEEE 754 64-bit FP as the in-memory format for floats?
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.954589770191 003 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?
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
On Feb 25, 1:31 pm, "John Machin" <sjmac...@lexic on.netwrote:
On Feb 25, 11:06 pm, "Arnaud Delobelle" <arno...@google mail.com>
wrote:
[...]
Evidently not; here's some documentation we both need(ed) to read:
http://docs.python.org/tut/node16.html
Thanks for this link
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.
It matters to me because I prefer to avoid making assumptions in my
code!
Moreover the reason I got interested in this is because I am creating
a
Dec class (decimal numbers) and I would like that:
* given a float x, float(Dec(x)) == x for as many values of x as
possible
* given a decimal d, Dec(float(d)) == d for as many values of d as
possible.
(and I don't want the standard Decimal class :)
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
Yes I have a similar routine:
|def fptest():
| "Gradually fill the mantissa of a float with 1s until running out
of bits"
| ix = 0
| fx = 0.0
| for i in range(200):
| fx = 2*fx+1
| ix = 2*ix+1
| if ix != fx:
| return i
....
>>fptest()
53
I guess it would be OK to use this. It's just that this 'poking into
floats'
seems a bit strange to me ;)
Thanks for your help
--
Arnaud
On 25 Feb 2007 06:11:02 -0800, Arnaud Delobelle <ar*****@google mail.comwrote:
Moreover the reason I got interested in this is because I am creating
a Dec class (decimal numbers)
Are you familiar with Python's Decimal library? http://docs.python.org/lib/module-decimal.html
--
Jerry
On Feb 25, 3:59 pm, "Jerry Hill" <malaclyp...@gm ail.comwrote:
On 25 Feb 2007 06:11:02 -0800, Arnaud Delobelle <arno...@google mail.comwrote:
Moreover the reason I got interested in this is because I am creating
a Dec class (decimal numbers)
Are you familiar with Python's Decimal library?http://docs.python.org/lib/module-decimal.html
Read on my post for a few more lines and you'll know that the answer
is yes :)
--
Arnaud
John Machin wrote:
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.
All Python interpreters use whatever is the C double type on its platform to
represent floats. Not all of the platforms use *IEEE-754* floating point types.
The most notable and still relevant example that I know of is Cray.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
Dennis Lee Bieber wrote:
On 25 Feb 2007 05:31:11 -0800, "John Machin" <sj******@lexic on.net>
declaimed the following in comp.lang.pytho n:
>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
Maybe a few old Vaxes/Alphas running OpenVMS... Those machines had
something like four or five different floating point representations (F,
D, G, and H, that I recall -- single, double, double with extended
exponent range, and quad)
I actually used Python on an Alpha running OpenVMS a few years ago. IIRC, the
interpreter was built with IEEE floating point types rather than the other types.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Thinkit |
last post by:
Are there any good libraries of arbitrary precision binary floats?
I'd like to specify the mantissa length and exponent range.
Hexadecimal output would be best (decimal is disgusting with binary
floats).
|
by: Ladvánszky Károly |
last post by:
Entering 3.4 in Python yields 3.3999999999999999.
I know it is due to the fact that 3.4 can not be precisely expressed by the
powers of 2. Can the float handling rules of the underlying layers be set
from Python so that 3.4 yield 3.4?
Thanks,
Károly
|
by: {AGUT2} {H}-IWIK |
last post by:
Hi,
Is it possible to compare / output two numbers to a certain precision?
for instance:
#include <stdlib>
#include <math>
#include <iostream>
#include <iomanip>
|
by: Brian van den Broek |
last post by:
Hi all,
I guess it is more of a maths question than a programming one, but it
involves use of the decimal module, so here goes:
As a self-directed learning exercise I've been working on a script to
convert numbers to arbitrary bases. It aims to take any of whole
numbers (python ints, longs, or Decimals), rational numbers (n / m n,
m whole) and floating points (the best I can do for reals), and
convert them to any base between 2 and...
|
by: ma740988 |
last post by:
I've got an unpacker that unpacks a 32 bit word into 3-10 bits samples.
Bits 0 and 1 are dont cares. For the purposes of perfoming an FFT and
an inverse FFT, I cast the 10 bit values into doubles. I'm told:
"floats and doubles have bits for mantissa and exponent. (I knew that).
If you are subtract bits that partly belong to mantissa and partly
belong to exponent, that hardly makes sense. Anyway, looking on bits
only, a float has 32...
| |
by: DAVID SCHULMAN |
last post by:
I've been trying to perform a calculation that has been running into
an underflow (insufficient precision) problem in Microsoft Excel, which
calculates using at most 15 significant digits. For this purpose, that
isn't enough.
I was reading a book about some of the financial scandals of the
1990s called "Inventing Money: The Story of Long-Term Capital Management
and the Legends Behind it" by Nicholas Dunbar. On page 95, he mentions
that...
|
by: Anton81 |
last post by:
Hi!
When I do simple calculation with float values, they are rarely exactly
equal even if they should be. What is the threshold and how can I change
it?
e.g. "if f1==f2:" will always mean "if abs(f1-f2)<1e-6:"
Anton
|
by: graham.macpherson |
last post by:
I have 2 Suse Linux PCs which I compile my code on. Until recently
they both had gcc 4.0.X on them, but I upgraded one of them to gcc
4.1.0. I have come across a very strange problem in the following code
which uses an STL map:
void Distribution::AddToDistribution (double& valueToAddToDistribution)
{
int n;
n = static_cast<int>(valueToAddToDistribution/itsBinWidth);
|
by: Grant Edwards |
last post by:
I'm pretty sure the answer is "no", but before I give up on the
idea, I thought I'd ask...
Is there any way to do single-precision floating point
calculations in Python?
I know the various array modules generally support arrays of
single-precision floats. I suppose I could turn all my
variables into single-element arrays, but that would be way
ugly...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |