If I use this code it comes out false. Why?
(0.0087 * 100) = 0.87
If I encapsulate each side of the equation with Val() it comes out true.
Is it something to do with a floating decimal point?
Help
Printable View
If I use this code it comes out false. Why?
(0.0087 * 100) = 0.87
If I encapsulate each side of the equation with Val() it comes out true.
Is it something to do with a floating decimal point?
Help
For the record the following code comes out true,
(0.0077 * 100) = 0.77
Why is this?
I guess it might be because the 0.87 is being represented as a single, and the multiplication returns a double. As a matter of fact, if you do this, you get true:VB Code:
CSng(0.0087 * 100) = 0.87
Actually... the multiplication returns a double, because 0.0087 is saved in a Double variable.
As well... you'll get True with this code:
VB Code:
CSng(0.0087) * 100 = 0.87
Actually if you try this, you can avoid all confusion about data types and still get a false.
Private Sub Command1_Click()
Dim dblNumber1 As Double
Dim dblNumber2 As Double
dblNumber1 = 0.87
dblNumber2 = 0.0087 * 100
Debug.Print "========================================"
Debug.Print "dblNumber1 = " & dblNumber1
Debug.Print "dblNumber2 = " & dblNumber2
Debug.Print "dblNumber1 = dblNumber2: " & (dblNumber1 = dblNumber2)
End Sub
It is pretty weird
Maybe the different mantissa is the problem. Check this out:VB Code:
Option Explicit Private Sub Command1_Click() Dim dblNumber1 As Double Dim dblNumber2 As Double dblNumber1 = 0.87 dblNumber2 = CSng(0.0087) * 100 Debug.Print "========================================" Debug.Print "dblNumber1 = " & dblNumber1 Debug.Print "dblNumber2 = " & dblNumber2 Debug.Print "dblNumber1 = dblNumber2: " & (dblNumber1 = dblNumber2) End Sub
Get this:
Call MsgBox((8.7 * 10 ^ -3 * 100) = 0.87)
is true...
It's not that weird. The problem is that you're losing some decimals when perfoming those calculations. For example:
0.87510 = 0.1112
what it's equal to:
0.875 = 2-1 + 2-2 + 2-3 = 0.5 + 0.25 + 0.125
(power of two... because it's binary notation)
The problem is that the number 0.0087 cannot be represented exactly as 0.0087 because you would need a lot of addends which you will lose some of them (for sure) due to the double precision.
I hope this clarifies a little.... because it's not that easy to explain.
Edit: I wrote the exponents as superscript numbers...
I figured as much, but this is something I would classify as a quirk, not a feature...
ok...let me see this...
Why?...why would you like to COMPARE a Math operation with it's actual result?
and being 0.87 the actual result of (0.0087 * 100) ...It'll of course will give you a TRUE result...
because [B]You are comparing one VALUE against another VALUE
Isn't obvious that this (0.0087 * 100) = 0.87 is not an Math operation?, but a tricky question...
OK McBrain you've earned you name, that makes sense.
I looked up some information about Mantissas and indeed that appears to be the answer.
dblNumber1 and dblNumber2's exponent and the mantissa are different, though they represent the same number. That is why Val() works and a simple = does not.
I hope everyone at NASA is aware of this.
Thanks
err...actually
rereading...it's the representation of a Math statement...
just like 1 = 1 that of course it's TRUE!! ;)
Microsoft tells not to compare floating point values for the equality... because it loses some precision when you display a floating point in limited digits..i.e.
0.87 might be 0.8700000000121111 for the registers but it displays to be 0.87
You're welcome.... and I also hope NASA is aware of it.Quote:
Originally posted by jhunt
OK McBrain you've earned you name, that makes sense.
I looked up some information about Mantissas and indeed that appears to be the answer.
dblNumber1 and dblNumber2's exponent and the mantissa are different, though they represent the same number. That is why Val() works and a simple = does not.
I hope everyone at NASA is aware of this.
Thanks