cvs.gedasymbols.org/archives/browse.cgi   search  
Mail Archives: djgpp/1997/02/10/14:37:42

From: kagel AT quasar DOT bloomberg DOT com
Date: Mon, 10 Feb 1997 14:17:45 -0500
Message-Id: <9702101917.AA01199@quasar.bloomberg.com >
To: kay AT edition DOT bonbit DOT org
Cc: djgpp AT delorie DOT com
In-Reply-To: <6QQ3UUfzccB@jocokko.edition.bonbit.org> (kay@edition.bonbit.org)
Subject: Re: double-->int: What's wrong here?
Reply-To: kagel AT dg1 DOT bloomberg DOT com

   From: kay AT edition DOT bonbit DOT org (Kay Hayen)
   Date: 07 Feb 1997 00:00:00 +0000
   Newsgroups: comp.os.msdos.djgpp

   Hallo Leute,

   eliz AT is DOT elta DOT co DOT il (Eli Zaretskii)  meinte am 06.02.97
   zum Thema "Re: double-->int: What's wrong here?":

   >> (BTW I agree with your recommendation in the general case, though, I
   >> usually use 0.505 as an error correction value.)
   >
   >That's for second-graders ;-).  First, people need to be convinced to add

Eli's comment here simply meant that before we can suggest better rounding
factors (a second grade lesson) we needed to convince people that they may need
to round floating point values due to binary precision truncation of values not
exactly representable in the number of binary digits available to a particular
floating point representation (first grade lesson).  No need to get upset.

   >any value at all.

   Is there any source of information, why to recommend 0.505 ? I'm quite  
   irrtitated now.

Since the reason for rounding is to correct for, possible, inaccuracies in the
floating point representation, adding 5 to the digit following the last one in
which you are interested may not solve the problem.  Ex:

4.98584999 rounded to five significant digits by adding .00005 still truncates
to 4.9858 while the actual answer may be 4.9859.  Though adding .000005 to
round to 6 significant digits does work as expected trucating to 4.98585.


Therefore it is usually recommended to add an additional amount to an even
lower order digit which is unlikely to affect the answer but which will be more
likely to correct the truncation.  So:

4.98584999 + 0.0000505 = 4.98590049 which truncates to the correct answer at
the fifth significant digit.  Meanwhile adding .00000505 still yields 4.98585
for six significant digits and so does not negatively affect the answer.

This works because inaccuracies caused by not having enough binary digits in
the representation, or by representing a transcendental value, will always be
low by at least 1/2 the magnitude of the lowest order digit represented because
at least one binary 1-bit is missing beyond those represented.

Hope that helps. 

-- 
Art S. Kagel, kagel AT quasar DOT bloomberg DOT com

A proverb is no proverb to you 'till life has illustrated it.  -- John Keats

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019