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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Erick37Connect With a Mentor Commented:
This will also work if Text1.Text is a string:
Text3.Text = Int(CDbl(Text1.Text / 25.4))
0
 
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
 
MirkwoodCommented:
Nasty pentium bug... :-)

No the code is correct it returns 11 in all cases I try..
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
All Courses

From novice to tech pro — start learning today.