Fix() Function

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I'm Titus. I have a problem with Fix() function. Not only in Access, but
also in Excel and in all Visual Basic modules. This is the code (an example):

Sub Something()
a = 0.3
b = 30
c = 0.3 * 100
If b = c Then Debug.Print "b=c"
If 30 = c Then Debug.Print "30=c"
Debug.Print Fix(b), Fix(c), Fix(0.3 * 100)
End Sub

This is the result in Immediate window:

b=c
30=c
30 30 29

This is the help for Fix() function:

Int, Fix Functions

Returns the integer portion of a number.
Syntax
Int(number)
Fix(number)
The required number argument is a Double or any valid numeric expression. If
number contains Null, Null is returned.
Remarks
Both Int and Fix remove the fractional part of number and return the
resulting integer value.
The difference between Int and Fix is that if number is negative, Int
returns the first negative integer less than or equal to number, whereas Fix
returns the first negative integer greater than or equal to number. For
example, Int converts -8.4 to -9, and Fix converts -8.4 to -8.
Fix(number) is equivalent to:
Sgn(number) * Int(Abs(number))

And this is the example for Fix() function:

Int Function, Fix Function Example
This example illustrates how the Int and Fix functions return integer
portions of numbers. In the case of a negative number argument, the Int
function returns the first negative integer less than or equal to the number;
the Fix function returns the first negative integer greater than or equal to
the number.
Dim MyNumber
MyNumber = Int(99.8) ' Returns 99.
MyNumber = Fix(99.2) ' Returns 99.

MyNumber = Int(-99.8) ' Returns -100.
MyNumber = Fix(-99.8) ' Returns -99.

MyNumber = Int(-99.2) ' Returns -100.
MyNumber = Fix(-99.2) ' Returns -99.

My question: Is there something I don't unterstood? I tried this code on
three different computers with Win '98, Win 2000 and Win XP, in all kind of
Visual Basic modules (for Access '97 and Access 2000, Excel '97, Excel 2000
and Excel 2003, even in Visual Basic 6.0) with same results.

If you red all of this,
Thank you for patience
 
Titus said:
I'm Titus. I have a problem with Fix() function. Not only in Access, but
also in Excel and in all Visual Basic modules. This is the code (an example):

Sub Something()
a = 0.3
b = 30
c = 0.3 * 100
If b = c Then Debug.Print "b=c"
If 30 = c Then Debug.Print "30=c"
Debug.Print Fix(b), Fix(c), Fix(0.3 * 100)
End Sub

This is the result in Immediate window:

b=c
30=c
30 30 29

This is the help for Fix() function:

Int, Fix Functions

Returns the integer portion of a number.
Syntax
Int(number)
Fix(number)
The required number argument is a Double or any valid numeric expression. If
number contains Null, Null is returned.
Remarks
Both Int and Fix remove the fractional part of number and return the
resulting integer value.
The difference between Int and Fix is that if number is negative, Int
returns the first negative integer less than or equal to number, whereas Fix
returns the first negative integer greater than or equal to number. For
example, Int converts -8.4 to -9, and Fix converts -8.4 to -8.
Fix(number) is equivalent to:
Sgn(number) * Int(Abs(number))

And this is the example for Fix() function:

Int Function, Fix Function Example
This example illustrates how the Int and Fix functions return integer
portions of numbers. In the case of a negative number argument, the Int
function returns the first negative integer less than or equal to the number;
the Fix function returns the first negative integer greater than or equal to
the number.
Dim MyNumber
MyNumber = Int(99.8) ' Returns 99.
MyNumber = Fix(99.2) ' Returns 99.

MyNumber = Int(-99.8) ' Returns -100.
MyNumber = Fix(-99.8) ' Returns -99.

MyNumber = Int(-99.2) ' Returns -100.
MyNumber = Fix(-99.2) ' Returns -99.

My question: Is there something I don't unterstood? I tried this code on
three different computers with Win '98, Win 2000 and Win XP, in all kind of
Visual Basic modules (for Access '97 and Access 2000, Excel '97, Excel 2000
and Excel 2003, even in Visual Basic 6.0) with same results.


This kind of things always happen in calculations with
imprecise numbers (i.e. floating point). For example, try
to write down the exact value of 1/3. No matter what you do
there will always be a slight error, the same thing happens
with the binary representation of .3

To see how small this error is,
Fix(.3 * 100 + .00000000000001) will display as 30.
All of your other examples go through some kind of
formatting, which is another conversion that sort of
compensates for this insignificant representation error.
The Fix and Int functions drop the fractional part of
29.99999999999999 before the formatting occurs.
 
Back
Top