I understand why the repr() of float("95.895") is "95.894999999999996".
What I don't understand is why if I multiply the best approximation to
95.895 that the machine has by 10000 I magically seem to get the lost
precision back. To wit:
% python
Python 2.3.4 (#12, Jul 2 2004, 09:48:10)
[GCC 3.3.2] on sunos5
Type "help", "copyright", "credits" or "license" for more information. 95.895
95.894999999999996 95.895 * 10000
958950.0
Why isn't the last result "958949.99999999996"? IOW, how'd I get back the
lost bits?
Thx,
Skip 4 2322
Skip Montanaro wrote: I understand why the repr() of float("95.895") is
"95.894999999999996". What I don't understand is why if I multiply the best approximation
to 95.895 that the machine has by 10000 I magically seem to get the lost precision back. To wit:
% python Python 2.3.4 (#12, Jul 2 2004, 09:48:10) [GCC 3.3.2] on sunos5 Type "help", "copyright", "credits" or "license" for more
information. >>> 95.895 95.894999999999996 >>> 95.895 * 10000
958950.0
Why isn't the last result "958949.99999999996"? IOW, how'd I get
back the lost bits?
You were just lucky.
The floating-point representation of 95.895 is exactly 6748010722917089
* 2**-46.
Multiplying by 10000 gives you 67480107229170890000 * 2**-46. But
floats can have only 53 significant bits, so this gets normalized to
8237317776998399.658203125 * 2**-33 and rounded to 8237317776998400 *
2**-33, which happens to be exactly equal to 958950.
For analogy, consider a decimal calculator with only 3 significant
digits. On this calculator, 1/7=0.143, an error of 1/7000.
Multiplying 0.143 by 7 gives 1.001, which is rounded to 1.00, and so
you get an exact answer for 1/7*7 despite roundoff error in the
intermediate step.
>> Why isn't the last result "958949.99999999996"? IOW, how'd I get back the lost bits?
Dan> You were just lucky.
Thanks for the response (and to Tim as well).
Dan> The floating-point representation of 95.895 is exactly
Dan> 6748010722917089 * 2**-46.
I seem to recall seeing some way to extract/calculate fp representation from
Python but can't find it now. I didn't see anything obvious in the
distribution.
Thx,
Skip
[Dan] Dan> The floating-point representation of 95.895 is exactly Dan> 6748010722917089 * 2**-46.
[Skip Montanaro] I seem to recall seeing some way to extract/calculate fp representation from Python but can't find it now. I didn't see anything obvious in the distribution.
For Dan's example, import math math.frexp(95.895)
(0.74917968749999997, 7) int(math.ldexp(_[0], 53))
6748010722917089L
On 9 May 2005 11:06:22 -0700, "Dan Bishop" <da*****@yahoo.com> wrote: Skip Montanaro wrote: I understand why the repr() of float("95.895") is "95.894999999999996". What I don't understand is why if I multiply the best approximation to 95.895 that the machine has by 10000 I magically seem to get the lost precision back. To wit:
% python Python 2.3.4 (#12, Jul 2 2004, 09:48:10) [GCC 3.3.2] on sunos5 Type "help", "copyright", "credits" or "license" for more information. >>> 95.895 95.894999999999996 >>> 95.895 * 10000 958950.0
Why isn't the last result "958949.99999999996"? IOW, how'd I get
back the lost bits?
You were just lucky.
The floating-point representation of 95.895 is exactly 6748010722917089 * 2**-46.
Multiplying by 10000 gives you 67480107229170890000 * 2**-46. But floats can have only 53 significant bits, so this gets normalized to 8237317776998399.658203125 * 2**-33 and rounded to 8237317776998400 * 2**-33, which happens to be exactly equal to 958950.
For analogy, consider a decimal calculator with only 3 significant digits. On this calculator, 1/7=0.143, an error of 1/7000. Multiplying 0.143 by 7 gives 1.001, which is rounded to 1.00, and so you get an exact answer for 1/7*7 despite roundoff error in the intermediate step.
In bits, the above appears as prb(95.895)
'1011111.11100101000111101011100001010001111010111 00001' len(prb(95.895).split('.')[1])
46 prb(95.895*2**46)
'1011111111001010001111010111000010100011110101110 0001' int(prb(95.895*2**46),2)
6748010722917089L int(prb(95.895*2**46),2)*10000
67480107229170890000L prb(int(prb(95.895*2**46),2)*10000)
'1110101000011110010111111111111111111111111111111 11111010100010000' prb(int(prb(95.895*2**46),2)*10000)[:53]
'1110101000011110010111111111111111111111111111111 1111' int(prb(int(prb(95.895*2**46),2)*10000),2)/2.**46
958950.0 prb(int(prb(int(prb(95.895*2**46),2)*10000),2)/2.**46)
'11101010000111100110'
Regards,
Bengt Richter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: JS |
last post by:
We have the same floating point intensive C++ program that runs on
Windows on Intel chip and on Sun Solaris on SPARC chips. The program
reads the exactly the same input files on the two platforms....
|
by: Anton Noll |
last post by:
We are using Visual Studio 2003.NET (C++) for the development
of our software in the fields digital signal processing and
numerical acoustics.
One of our programs was working correctly if we are...
|
by: cody |
last post by:
no this is no trollposting and please don't get it wrong but iam very
curious why people still use C instead of other languages especially C++.
i heard people say C++ is slower than C but i can't...
|
by: Benjamin Rutt |
last post by:
Does anyone have C code laying around to do this?
I have to read in some binary data files that contains some 4-byte
IBM/370 floating point values. I would like a function to convert
4-byte...
|
by: Steffen |
last post by:
Hi,
is it possible to have two fractions, which (mathematically) have the
order a/b < c/d (a,b,c,d integers), but when (correctly) converted into
floating point representation just have the...
|
by: Bryan Parkoff |
last post by:
The guideline says to use %f in printf() function using the keyword
float and double.
For example
float a = 1.2345;
double b = 5.166666667;
printf("%.2f\n %f\n", a, b);
|
by: jacob navia |
last post by:
Hi people
I continue to work in the tutorial for lcc-win32, and started to try to
explain the floating point flags.
Here is the relevant part of the tutorial. Since it is a difficult part,
I...
|
by: mathieu.dutour |
last post by:
Dear all,
I want to do multiprecision floating point, i.e. I want
to go beyond single precision, double precision and have
quadruple precision, octuple precision and the like,
and possibly with...
|
by: rembremading |
last post by:
Hi all!
The following piece of code has (for me) completely unexpected behaviour.
(I compile it with gcc-Version 4.0.3)
Something goes wrong with the integer to float conversion.
Maybe somebody...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |