Thanks:  0
Likes:  0
Dislikes:  0

# Thread: Why does the following expression eval to False?

1. ## Why does the following expression eval to False?

I don't know why this simple expression evaluates to false.

<font class="small">Code:</font><hr /><pre>

dblOne = CDbl("8.35")
dblTwo = CDbl("3")
dblThree = CDbl("25.05")

If dblOne * dblTwo = dblThree Then
MsgBox "True"
Else
MsgBox "False"
End If

</pre><hr />

2. ## Re: Why does the following expression eval to False?

Dude, you r right I think it takes one expression as a number and the other as a string , I haven't arrived at a solution and its friday but u can foloow this tip

I even tried the code below and it always arrives at false some sort of string conversion should be done I presume

a = "8.35"
b = "3"

c = a* b
Msgbox(c)
d = "25.05"

Check = CBool (a = b)
msgbox(Check)

If c &lt;&gt; d Then
MsgBox "False"
Else
MsgBox "True"
End If

3. ## Re: Why does the following expression eval to False?

Alright both expression in your case were different
The code below compares two string expression
You cannot compare 2 different expressions "string and a number"

a = 8.35
x = cstr(a)
b = 3
y = cstr(b)
c = a* b
z = cstr(c)

d = 25.05
t = cstr(d)

If z &lt;&gt; t Then
MsgBox "False"
Else
MsgBox "True"
End If

4. ## Re: Why does the following expression eval to False?

In my code you will see I EXPLICITLY covert my numbers all to doubles (the CDbl funcion) so all all of the same type.

Even if I do CDbl(dblOne * dblTwo) = CDbl(dblThree) it doesn't work.

5. ## Re: Why does the following expression eval to False?

Hi,

Try this -
CDbl(dblOne * dblTwo) - CDbl(dblThree)

You will get a value as "-3.5527136788005E-15".

Since it is a double precision multiplication, I think even the smallest value is condsidered.

6. ## Re: Why does the following expression eval to False?

The problem is in the way that the numbers are stored internally - even doubles will have small inaccuracies that will result in a false outcome.

If you really need to handle a problem like this then use variable types that are precise like integers or money.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Search Engine Optimisation provided by DragonByte SEO v2.0.36 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.