FORTRAN floating point accuracy problems
|
03-28-2016, 01:29 PM
(This post was last modified: 03-28-2016 01:34 PM by Dieter.)
Post: #8
|
|||
|
|||
RE: FORTRAN floating point accuracy problems
(03-28-2016 09:29 AM)HP67 Wrote: I did see in some cases adding a small correction would help but when I added it in all cases it made the results much worse overall. Below when you said to add the correction in the first line, did you mean the first line of my code (I think there it is already too late) or in the first line of your example code? The first line (after the declarations) of your code, the one that reads SS = (DMS - IDINT(DMS)) * 100.0D0. (03-28-2016 09:29 AM)HP67 Wrote: Yes, the numbers are generated internally so they should be well-formed. That's good to know. However, the third and preferred solution should handle either case. (03-28-2016 09:29 AM)HP67 Wrote: I will have to check. The computer I'm using does not use IEEE binary, it uses a proprietary hexadecimal format and I don't remember how many bits of precision are carried in the mantissa. I'll get back to you on that. How do you choose a correction value close to one ULP? You said for 52 bit binary we should use 2^-40 or 1E-12. What is the connection between these values and the bit precision? Let's assume the working precision is 15 decimal digits (like IEEE754 double precision). If 2-3 decimal digits are kept for the integer portion of the argument (whole degrees), the remaining 12-13 hold the mmss part. So the last digit of mmss is (roughly) 1E-12. Or (base 2) about 2^–39. The idea is to get this value large enough to do the rounding but small enough to minimize its mathematical impact, as it affects the last digit(s) of the final result. If you want to implement this idea I would recommend a value for "tiny" that is based on the argument dms, e.g. tiny=dms/1E14 for 15-digit working precision. So this is how it is supposed to work: Code: entered argument dms: But the third approach should work even better: (03-28-2016 09:29 AM)HP67 Wrote: I don't have a rounding function and I don't understand how to write one. Is this solution still possible? Is it possible to write a general purpose rounding function? All I have is trig functions, integer (which does not simply take the integer portion and store in the 8 byte floating point value but rather it demotes the 8 byte floating point integer value to a 4 byte integer type. Sure. Write your own rounding function. ;-) CAVE: I DO NOT HAVE ANY EXPERIENCE WITH FORTRAN! But from what I understand there is a NINT resp. IDNINT function that rounds a value to its closest integer. For a function that rounds to n digits, simply multiply the argument by 10^n, round this to the next closest integer and divide by 10^n again. Here is the general idea for rounding a positive value X to D decimal places: Code: THIS IS NOT FORTRAN CODE, IT JUST SHOWS THE GENERAL IDEA! Please note that even R is rounded since we cannot be sure that calculating 10^13 will return exactly 10,000,000,000,000. So just to be sure... Once you know the number of digits to round (13 in the following example), rounding is easy. Simply define the factor R somewhere before (or use a constant - don't know how this is done in Fortran) and the rest is trivial: Code: R = 1.0D13 A closer-to-perfect solution would round to a variable number of digits, depending on the magnitude of the dms argument. But 13 digits works well in my Excel VBA implementation, at least for angles up to 999 degrees. Now try this and post your improved code here. Dieter |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 2 Guest(s)