Hi,
I have a problem with floating point numbers. When i use a floating point number with more than seven characters (i use #asFloat), (for example 1234.567, 3.142856,65265.534 ...etc) my actual number is rounded off either to have digits less than 8 or returns the exponential format. I tried using Double (#asDouble) instead of Float, but it gives wrong answers. For example, when i take 3.14 asDouble it gives 3.1400001049042d .So i thought avoiding the use of #asDouble. In my application i need to add several floating point numbers. When i loose the precision by .001 or .0001 for each such number, i have much difference in the Total amount. Have anyone experienced such problems?Can anyone suggest a solution or some hints? Thanks in advance, maNi |
Floating numbers depend on the number of bits (e.g. 32) of your hardware and the representation of numbers in these bits, e.g. IEEE representation. It is totally normal that on a 32 bit machine you get max. 6 digits reliable floating points. 3.14 asDouble leads to a 64 bit representation of 3.14 in e.g. IEEE. The result (3.140000....) is as exact as the number can be represented in 64 bits, sum of exponents to the power of 2. You can NEVER rely on that 3.14 or any other floating number is REALLY 3.14. A deviation is normal. The most exact you can do is using double and taking care of comparisons (never ask aNumber = 3.14 but use a delta, e.g. (aNumber - 3.14) abs < 0.0001 or similar). OR: Convert your numbers to integers with a factor of the accuracy you need. Karl __________________________________________________________ Karl Breith AREVA NP GmbH Methods & Codes (FDEEC-G) Freyeslebenstraße 1 91058 Erlangen Phone: +49 (0) 9131 18-97393 Fax: +49 (0) 9131 18-94045 mail to: [hidden email] An AREVA and Siemens company Vorsitzender des Aufsichtsrats: Bertrand Durrande - Geschäftsführer: Dr. Ralf Güldner, Rüdiger Steuerlein Sitz der Gesellschaft: Erlangen - Registergericht: Fürth, HRB 7817 - www.areva-np.com Wichtiger Hinweis: Diese E-Mail kann Betriebs- oder Geschäftsgeheimnisse, bzw. sonstige vertrauliche Informationen enthalten. Sollten Sie diese E-Mail irrtümlich erhalten haben, ist Ihnen eine Kenntnisnahme des Inhalts, eine Vervielfältigung oder Weitergabe der E-Mail ausdrücklich untersagt. Bitte benachrichtigen Sie uns und vernichten Sie die empfangene E-Mail. Vielen Dank. Important Note: This e-mail may contain trade secrets or privileged, undisclosed or otherwise confidential information. If you have received this e-mail in error, you are hereby notified that any review, copying or distribution of it is strictly prohibited. Please inform us immediately and destroy the original transmittal. Thank you for your cooperation. -----Ursprüngliche Nachricht----- Von: Mani S Kartha [mailto:[hidden email]] Gesendet: Freitag, 24. August 2007 09:43 An: [hidden email] Betreff: [VW 7.4.1] Floating point number precision Hi, I have a problem with floating point numbers. When i use a floating point number with more than seven characters (i use #asFloat), (for example 1234.567, 3.142856,65265.534 ...etc) my actual number is rounded off either to have digits less than 8 or returns the exponential format. I tried using Double (#asDouble) instead of Float, but it gives wrong answers. For example, when i take 3.14 asDouble it gives 3.1400001049042d .So i thought avoiding the use of #asDouble. In my application i need to add several floating point numbers. When i loose the precision by .001 or .0001 for each such number, i have much difference in the Total amount. Have anyone experienced such problems?Can anyone suggest a solution or some hints? Thanks in advance, maNi |
In reply to this post by Mani S Kartha
Mani S Kartha wrote:
> I have a problem with floating point numbers. > When i use a floating point number with more than seven characters (i > use #asFloat), > (for example 1234.567, 3.142856,65265.534 ...etc) my actual number is > rounded off either to have digits less than 8 or returns the > exponential format. > I tried using Double (#asDouble) instead of Float, but it gives wrong > answers. > For example, when i take 3.14 asDouble it gives 3.1400001049042d .So i > thought avoiding the use of #asDouble. > are by their very nature approximations of actual values. Also, since they are (essentially) fractions of the form [an integer of 23 bits (floats) or 53 bits (doubles)] * 2^exponent, this means that it is impossible for them to represent the vast majority of numbers we write in decimal form without precision loss. For example, there is no way you can express 0.3 as a floating point number if the base of the floating point number is 2. If you need zero precision loss, I'd suggest you use scaled decimal or fractions. Floating point arithmetic is extremely well studied. For further information, I'd recommend picking up a copy of e.g.: Donald E. Knuth's The Art of Computer Programming. Thanks, Andres. |
Free forum by Nabble | Edit this page |