Re: Strange basic problem with comparision of two varialbes defined as double
From: Duane Bozarth (dp_bozarth_at_swko.dot.net)
Date: 05/17/04
- Next message: Duane Bozarth: "Re: How to set UDT as Public so that I can assess it anywhere?"
- Previous message: Chris: "Re: Response.write tool"
- In reply to: RAJ: "Re: Strange basic problem with comparision of two varialbes defined as double"
- Next in thread: RAJ: "Re: Strange basic problem with comparision of two varialbes defined as double"
- Reply: RAJ: "Re: Strange basic problem with comparision of two varialbes defined as double"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 17 May 2004 08:35:20 -0500
RAJ wrote:
>
> Thanks for your reply.
> What alternative is there to compare two values of this sort? I have also
> seen this kind of problem were in I am storing a value defined as double in
> the access database field which is again defined as double with 2 decimal
> places. Even though the value is, for example, 10.00, it at times (not
> always) stores it as 9.99999999999997 in the database.
>
> My application does need to compare a lot of values for accounting purposes.
> Your help is much appriciated.
First of all, did you read the paper at the link? If so, do you
understand the cause of the problems you're having? If not, you'll
continue to make the same (wrong) assumptions forever. Unfortunately,
the tone of your description makes me think you still aren't quite
"getting it" wrt floating point...
If you're doing accounting-type computations, I would recommend you
<not> store data in floating point format to simplify the problem. It
is inevitable in floating point that <most> fractional values cannot be
stored exactly--the source of your consternation resulting in your
original post. Any integer up to the limit of precision of a Double, of
course (roughly 15 decimal digits) can be stored exactly. But, only
those fractions representable by binary fractions are <exactly>
representable in floating point. Thus, any time you make almost any
computation including the cents, there will be a rounding error and your
nice, clean pennies aren't any longer.
Your alternatives are to either use a representation that deals with the
cents cleanly (Currency is a start) or use appropriate rounding
algorithms to control the results of floating point. Being an engineer
rather than a business type, I don't have a particular place to point
you for a tutorial, but I'm sure there are many a search could find or
someone else here may know of a good place...
- Next message: Duane Bozarth: "Re: How to set UDT as Public so that I can assess it anywhere?"
- Previous message: Chris: "Re: Response.write tool"
- In reply to: RAJ: "Re: Strange basic problem with comparision of two varialbes defined as double"
- Next in thread: RAJ: "Re: Strange basic problem with comparision of two varialbes defined as double"
- Reply: RAJ: "Re: Strange basic problem with comparision of two varialbes defined as double"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|