Re: Fix() Function

From: Marshall Barton (marshbarton_at_wowway.com)
Date: 09/30/04


Date: Thu, 30 Sep 2004 10:28:38 -0500

Titus wrote:
>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.

-- 
Marsh
MVP [MS Access]


Relevant Pages

  • [git patches] net driver updates for .26
    ... Fix a bug where the pointer never moves for dma_unmap... ... Update and fix driver debugging messages ... int reset); ... * header structure can be anywhere in the mcp. ...
    (Linux-Kernel)
  • [git patches] net driver fixes
    ... Fix NAPI state bug when Rx complete ... const struct cmac_statistics *s; ... int flag) ...
    (Linux-Kernel)
  • [git patches] net driver updates
    ... Fix locking in ethtool support ... static int au1000_init ... if (err) { ... * @adapter: board private struct ...
    (Linux-Kernel)
  • [git patches] net driver updates
    ... Fix PHY Lib support for gianfar and ucc_geth ... int mii_status; ... struct vio_dev *vio_dev) ... struct net_device *dev; ...
    (Linux-Kernel)
  • Re: Trim Integer
    ... Use either the Int or Fix functions. ... returns the first negative integer greater than or equal to number. ... The following examples illustrate how the Int and Fix functions return ...
    (microsoft.public.vb.general.discussion)