Wednesday, September 3. 2008Numbers and Magic TricksTrackbacks
Trackback specific URI for this entry
No Trackbacks
Comments
Display comments as
(Linear | Threaded)
An interesting artifact, but your description is itself a bit imprecise. How?
I can follow your instructions *on paper*, handling the math symbolically, and get the "correct" (expected) answer: the exact number I started with. You neglected to state that this quirk occurs only in a SAS (or other?) computer program. This is not a SAS specific issue. It is a trait shared by all floating point processors that implement the IEEE 754 standard.
This is an interesting finding. Adding and subtracting this special number can lead to a loss of precision. My question is: Is this the only number that leads to such a 'magic trick'? Does this happen for adding and subtracting some other numbers?
This is not quiet true. Look at what happens with m=0.5, 2.5, 4.5 etc.
They are not rounded yo the nearest whole integer m=0.25 => n=0 m=0.4999 => n=0 m=0.5 => n=0 m=0.5001 => n=1 m=0.75 => n=1 m=1 => n=1 m=1.5 => n=2 m=2.5 => n=2 m=3.5 => n=4 m=4.5 => n=4 Values that end in .5 are exactly between the two bounding integers. So you have to have a rule to handle ties.
Here's what William Kahan (father of IEEE 754) says: "Half-way cases are rounded to Nearest Even, which means that the neighbor with last digit 0 is chosen." Here's what is happening for m=2.5. 2.5 = 2**1 * (1 + 1/2**2) which transforms into = 2**52*(1/2**51 + 1/2**53) add magic = 2**52*(1 + 1/2**51 + 1/2**53) (can't store 1/2**53) subtract magic = 2**52*(1/2**51) = 2**1(1) You should always be careful when adding very large and very small numbers - by definition the computer is using a representation of the number you want, using a limited number of bits. Normally it's not an issue, but it can lead to loss of precision.
Good to know that. However if I move the code line
put ' => ' @; down one line, so it becomes n=m+magic; n=n-magic; put ' => ' @; Then the magic will not happen. Does this mean there is no trigger for the floating number processor to store the n, so nothing is lost? I'm guessing that you are running on Windows or Linux. This is because the x86 architecture does some calculations in extended precision (80 bit) floating point registers. The datastep compiler will attempt to keep intermediate results in registers.
The PUT statement forced the intermediate value to be stored in 64 bit format. |
ABOUT THIS BLOG Welcome to the blog about SAS online support. Renee Harper (that's me) will keep you up-to-date about new and updated content on support.sas.com, as well as support services and software releases. I'll try to include relevant examples you can use -- sample programs and information about how others use SAS. I’ll be able to do this better if you join me – this is a place to share your ideas, successes, and frustrations.QuicksearchCategoriesThe blog content appearing on this site does not necessarily represent the opinions of SAS. Your use of this blog is governed by the Terms of Use.
|