Strange "INT" result

Check this code:

    Text1 = 11 * 25.4 '=279.4
    Text2 = (Text1 / 25.4) '=11
    Text3 = Int(Text1 / 25.4) '=10

The results are written as comments
on the right of each line.

I use a line like:
Decimal=text2-text3
to get the non-integer part of a value,
but with this strange results, my code
is a fake.
Someone can help or tech me something?

p.s.
In my app, the TextX are Double type variables, so is not a String2Value problem.


fab1970Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Erick37Commented:
If Text1 thru Text3 are Dimmed as double, you should get 11 as the last result. This works as predicted:

Dim Text1, Text2, Text3 As Double
Text1 = 11 * 25.4 '=279.4
Debug.Print Text1 '=279.4
Text2 = (Text1 / 25.4) '=11
Debug.Print Text2 '=11
Text3 = Int(Text1 / 25.4) '=11
Debug.Print Text3 '=11

If Text1 thru Text3 are string variables (Text1.Text) then the line
Text3.Text = Int(Format(Text1.Text) / 25.4)
returns the expected result.
0
Erick37Commented:
This will also work if Text1.Text is a string:
Text3.Text = Int(CDbl(Text1.Text / 25.4))
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MirkwoodCommented:
Nasty pentium bug... :-)

No the code is correct it returns 11 in all cases I try..
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

fab1970Author Commented:
Sorry mirkwood, is it really a pentium bug?
Can you explain better your answer?
0
amebaCommented:
Erick37 wrote:
>Dim Text1, Text2, Text3 As Double
this declaration is 2 variants and 1 double
0
amebaCommented:
I am getting 10
Text3 = Int(Text1 / 25.4)

but this is not "dan0411" bug from the link above (I am getting "dan0411 bug not found" msg from the test programs on the site - FISTBUG.EXE and FISTBUGV.EXE)
0
Erick37Commented:
Ameba is correct in catching the error in my code. The declares should be:

Dim Text1 As Double, Text2 As Double, Text3 As Double

With that I get 10 as the last result.

Corrected in this case by:

Text3 = Int((Text1 / 25.4) + 0.000000000000001) '=11
0
amebaCommented:
The problem is in saving temporary result for '(Text1 / 25.4)'
It matches the description for the bug dan0411

"Floating point numbers are stored inside of the microprocessor in an 80-bit format"

This works:
Text3 = Int(CDbl(Text1 / 25.4))
Text3 = Int(CVar(Text1 / 25.4))

Maybe it's VB bug - it doesn't insert any code for converting temp expression to any VB type...
0
fab1970Author Commented:
As ameba (but erick37 first) says, this code (Text3.Text = Int(CDbl(Text1.Text / 25.4))) works.

Now, i'm in trouble: reading all your comments i can't understand what was happened: Is a P bug, a VB bug, or what?
And in what cases can i fall in this problem again?

Thanks for the help.
0
Erick37Commented:
This is most likely a VB bug.  MS has confirmed a previous bug in the Int function which was corrected in VB 4.0.  Perhaps this is another undisclosed bug.

http://support.microsoft.com/support/kb/articles/Q138/5/22.asp

Text1.Text = 279.4
Dim d
d = Text1.Text / 25.4
Debug.Print "Incorrect: "; Int(Text1.Text / 25.4)
Debug.Print "  Correct: "; Int(d)
0
amebaCommented:
>Is a P bug, a VB bug, or what?
Maybe someone on EE has machine with non-Pentium processor to check this,
or can try the same operation Int() in another language
0
Erick37Commented:
I just ran the program on an old 486 Win95. Results:

Int(Text1.Text / 25.4)  == 10
Int(d) == 11

Not a Pentium bug.
0
amebaCommented:
' in vb6, vb5, vb4/32 this returns 9
Private Sub Form_Click()
    Dim x As Single ' or double, integer ...
    x = 213
    Caption = Int(x / 21.3)
End Sub

VB bug?
- I didn't test other languages, so I can't say

Int() function bug?
- this is without Int()
   Caption = (213 / 21.3) - 10
- it doesn't return 0, but -3.34 E-16

Maybe it's "Rounding error" (i.e. floating point subexpressions are not rounded)

or AI (I knew one day PCs will become smarter than humans)
0
Erick37Commented:
In VC++ (Ver 4.0) this code evaluates false, meaning the formula correctly evaluated to 0

if (((223 / 22.3) - 10) != 0)
{
    printf("OOPS");
}

In VB5, the following evaluates true, OOPS!

If ((223 / 22.3) - 10) <> 0 Then
    Debug.Print "OOPS"
End If
0
amebaCommented:
Thanks. Then, this is VB only (processor independent) FP bug
   223 / 22.3 - 10

Not nice.
0
amebaCommented:
FIX: Make sure all temporary subexpressions are explicitly converted to VB type (Single, Double or Variant)
   Caption = CSng(223 / 22.3) - 10 ' this works OK
0
amebaCommented:
>Single, Double or Variant
Single, Double, Variant or String
0
fab1970Author Commented:
AArrghh!
What says M$ about this sh..?
(1000 Thanks for your comments, guys!)
0
Erick37Commented:
Microsoft says that this behavior is "by design."

"PRB: Type Conversion Functions Can Return Unexpected Results"
http://support.microsoft.com/support/kb/articles/Q195/6/57.ASP
0
fab1970Author Commented:
Ok.
If i have right understand all, the problem is:
1) Value not converted (by CDbl, CInt etc.) can be bad convertet by VB.
2) M$ knows the problem, so i have found a "deja-vue" bug.

is hard for me understand the M$ article: why if i create a function like mCint, all works good?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.