Re: addition of 0.7 and 0.1 is not 0.8 CDbl / CStr vbs problem
- From: "Anthony Jones" <Ant@xxxxxxxxxxxxxxxx>
- Date: Fri, 22 Jun 2007 19:41:44 +0100
<robin@xxxxxxxxx> wrote in message
news:1182502459.529450.56690@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On 21 Jun., 17:01, "Anthony Jones" <A...@xxxxxxxxxxxxxxxx> wrote:point
<r...@xxxxxxxxx> wrote in message
news:1182436865.902509.220780@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hello Group,
i have some vbs code here and would be happy if one of you could
describe whats wrong with it.
i just would like to make addition of 0.1 and 0.7 but the result just
surprises me everytime again.
btw. i work on a system with german number format.
IF ((CDbl("0,7")+CDbl("0,1"))=(CDbl("0,8"))) THEN
MSGBOX "0,7 + 0,1 = 0,8"
ELSE
MSGBOX "0,7 + 0,1 IS NOT 0,8"
END IF
the solution for this problem is:
IF (CDbl(CStr(CDbl("0,7")+CDbl("0,1")))=(CDbl("0,8"))) THEN
MSGBOX "0,7 + 0,1 = 0,8"
ELSE
MSGBOX "0,7 + 0,1 IS NOT 0,8"
END IF
but what to hell is going on ???
kindest regards,
Robin
btw. the same prob occures with 10,7 + 0,1
This is a common question. The fact is that due to the way floating
small isvalues are stored there will be very small rounding errors. The actual
value stored for 0.7 + 0.1 is very slightly different from 0.8. So
this variation that when it comes to converting to a string the value is
displayed as 0.8 either way it is derived. However = will compare them
exactly and they differ.
Comparing floating point values should be done using a tolerance :-
If Abs(CDbl("0.7") + CDbl("0.1") - CDbl(0.8)) < 0.000001 Then
'Near enough
Else
'Not near enough
End If
Most newbies find this situation shocking yet it's pratically as old as
computer programming itself.
Thanks for answering. Something is not really clear to me with your
explaination. if there are little rounding errors, why do they only
occure with 0.7 and 10.7. using my example with 0.3+0.1=0.4 the
problem does not happen.
It won't happen with all numbers. Some numbers can be exactly represented in
the binary floating point format.
In other cases the resulting 'near enough' value stored after a calculation
may happen to be the same 'near enough' value used when the number is
assigned directly.
and if i use CDbl(CStr()) with the addition,
the problem also does not happen. isn't it a better way dealing with
the situation using CDbl(CStr()) than to compare with "almost near
enough"?
'Better' is subjective. The more convoluted the calculation the more the
rounding errors build up. At some point fairly soon the string generated by
CStr may no longer be what you want. Using CStr will also be slower.
Regards,
Robin
.
- References:
- addition of 0.7 and 0.1 is not 0.8 CDbl / CStr vbs problem
- From: robin
- Re: addition of 0.7 and 0.1 is not 0.8 CDbl / CStr vbs problem
- From: Anthony Jones
- Re: addition of 0.7 and 0.1 is not 0.8 CDbl / CStr vbs problem
- From: robin
- addition of 0.7 and 0.1 is not 0.8 CDbl / CStr vbs problem
- Prev by Date: Re: replace in xmlfile
- Next by Date: Re: object's methods and properties
- Previous by thread: Re: addition of 0.7 and 0.1 is not 0.8 CDbl / CStr vbs problem
- Next by thread: Re: addition of 0.7 and 0.1 is not 0.8 CDbl / CStr vbs problem
- Index(es):
Relevant Pages
|