bugs >> A bigger piece of Pi -- comparing real numbers

by strive4peace » Sun, 17 Dec 2006 01:17:31 GMT

To add to what Albert said,

if you do have real numbers for data types that you want to compare, use
an inequality instead of an equality...for instance,

instead of
If a = b then

use
If Abs(a-b) < 0.001 then 'or whatever is your tolerance



Warm Regards,
Crystal
*
(: have an awesome day :)
*
MVP Access
Remote Programming and Training
strive4peace2006 at yahoo.com
*



Albert D. Kallal wrote:
> Actally, one of the first lessons you lean in compter scicne is that "real"
> numers on a computer are ONLY an aprpociaztion.
>
> hence:
>
> Public Sub TestAdd()
>
>
> Dim MyNumber As Single
> Dim i As Integer
>
> For i = 1 To 10
> MyNumber = MyNumber + 1.01
> Debug.Print MyNumber
> Next i
> End Sub
>
>
> Here is the actual outpput of the above:
>
> 1.01
> 2.02
> 3.03
> 4.04
> 5.05
> 6.06
> 7.070001
> 8.080001
> 9.090001
> 10.1
>
> You can see that after just 7 addtions..already rounding is occuring
>
> and if we add the follwing line of code to the end of the above:
>
>
> if MyNumber = 10.1 = True then
>
> msgbox "the number is 10.1"
>
> else
> msgbox "the number is somthing else"
> endif
>
>
> The above will actuall produce:
>
> the number is something else
>
> NOTE VERY careful in the above how the actual debug.print produced 10.1, and
> DOES NOT show any rounding errors..but in fact the test condition "= 10.1"
> FAILS!!!!
>
> So, any serious software design has to take into account that real numbers
> can't be represented exactly in a computer. For most stuff this is not a
> problem, but when you write any software that will do financial
> calculations, then you must be VERY aware of rounding issues, and that
> single, or double numbers in a computer will rapidly result in rounding
> problems. "3/10th" in a computer is only an approximation of 3 over 10. That
> even applies to "1 over 10". You can't reliblairy use conditations on
> single, or double numbers in code....they will simply fail more often then
> not!!
>
>


bugs >> A bigger piece of Pi -- comparing real numbers

by Robert Morley » Sun, 17 Dec 2006 02:30:50 GMT


As already stated, basic floating point issues that any sufficiently
advanced programmer should already know about weren't the problem here. The
problem is VB hiding semi-significant digits in your source code, but
storing them internally.


Rob








bugs >> A bigger piece of Pi -- comparing real numbers

by strive4peace » Sun, 17 Dec 2006 03:40:52 GMT

i Rob,

I understand... but, for the benefit of others who may read this thread,
it is nice to tell them how to deal with it ;)

Warm Regards,
Crystal
*
(: have an awesome day :)
*
MVP Access

*



Robert Morley wrote:


Similar Threads

1. A bigger piece of Pi

2. compare one number with other numbers in a set

Hi,  all,

I need to change the numbers in a set depending on different needs, so
I am thinking to put the numbers in the config file. So I need the
change the numbers in the future just change the config file without
changing  the code.

Then I will load the numbers  into a set or ??? then compare with
another number coming in from another source.

Any suggestion how to implement this?

Thanks.

Greg

3. GIDS 2009 .Net:: Save Big, Win Big, Learn Big: Act Before Dec 29 2008

4. compare copied number with another fixed number.

Hi All,

I have a question to ask in Word Macro please.  thanks in advance.

can i put in "if / then" statement in the macro? like in batch file?

step 1:  let's say I manually highlight a number (34567) and select 
edit/copy from the document. click on the command button which will do the 
following.

step 2:  if the copied number =  23456, then goto a subroutine 23456.
            if the copied number =  34567, then goto a subroutine 34567.

5. Comparing to big list - Help...

6. BiG Numbers Math in VB now possible

7. Finding the biggest number out of 8 variables - Visual Basic/VB

8. Get the real hard disk serial number in Win98

Hi friends,

  By WMI I can get the hard disk serial put from factory to identify
physically a hard disk, but this method doesn work in Win98. Please, Does
anyone know any other method to get it in Win98?

  Thanks in advance.

    Neo ( XXXX@XXXXX.COM )