variable defined as double - having trouble!

J

jenn

i have a variable that is defined as a double.. that
variable is set to my form's field ( format = general
number with 6 decimal places).. then this doubleVariable
is written to my table field that is deined as double with
6 decimal places) The problem is.. when i step through the
code my double variable shows that it is = 1 but the
process insists on going on with the logic within my if
doubleVariable <> 1.. why is this?.. i have a similar
process on another form and the variable is defined as
currency and there are no problems.. is that the right way
to go.. defining my variable as currency??.. doesn't sound
right to me.. Can anyone help me out? Thanks!
 
A

Allen Browne

Floating point numbers are approximations only. You cannot reliably use them
to control a loop. Use one of the integer types if possible. If not, the
Currency type is a fixed point number (abeit only 4 decimal places), so it
can be used as a loop controller.
 
J

jenn

you suggested using an integer type.. isn't double an
integer type?.. that's what i have it defined as..
shouln't a double be able to hold a 6 decimal place
value?.. if not.. then what can (I need to be able to
accept up to 6 decimal places in this field).. defined as
Long?.. and i was alittle confused on your comment about
currency.. do you mean currency fields can only have up to
a 4 decimal place value in them?.. thanks so much! - jenn
 
A

Allen Browne

The Integer types (Long, Integer, and Byte) can store whole numbers only,
i.e. no decimal places at all.

The floating point types (Double and Single) store around 15-places and
7-places of precision only. That means if you have 15 digits before the
decimal point, the double has no more places left for the fraction part.
However, you could store the number:
0.000000000000000012345678901234
because the significant digits begin after the zeros, and you have about 15
places of precision. It is therefore incorrect to say that a floating point
number can or cannot store 6 decimal places: if stores a set number of
digits of precision. However, the number is an approximation. It's like 1/3
cannot be stored exactly as a decimal, no matter how many places you have.

The Currency type always has 4 places after the decimal point.

There is another number type called Decimal in Access 2000 and later.
However, you are best not to use this, as Access can't even do the basics
correctly with this type, such as sorting it.
 
J

jenn

Thank you so much for the lengthy response.. I have a
better understanding now.. but i still do not understand
why my variable, defined as double, is not being looked at
correctly. None of the values in this field will ever
being using anything more than the tens place (left of the
decimal) so I shouldn't be having this problem?.. right?..
note: if i change this data type tp currency .. then my
process works correctly.. your advice??
 
J

jenn

Thank you so much for the lengthy response.. I have a
better understanding now.. but i still do not understand
why my variable, defined as double, is not being looked at
correctly. None of the values in this field will ever
being using anything more than the tens place (left of the
decimal) so I shouldn't be having this problem?.. right?..
note: if i change this data type tp currency .. then my
process works correctly.. your advice??
 
J

jenn

Thank you so much for the lengthy response.. I have a
better understanding now.. but i still do not understand
why my variable, defined as double, is not being looked at
correctly. None of the values in this field will ever
being using anything more than the tens place (left of the
decimal) so I shouldn't be having this problem?.. right?..
note: if i change this data type tp currency .. then my
process works correctly.. your advice??
 
J

jenn

i'm sorry for replying to you again.. but maybe i can
better explain (and i am very sorry for the multiple same
posts.. i was getting an error msg when trying to post my
msg).. anyway.. i understand the floating point types.. i
uunderstand how you said one can not really say if a
double can hold a 6 decimal place value.. but in my
situation.. it should be able to.. being that this field
actually holds percentages.. so values in my field would
never exceed the 15 places.. is there any more information
you can share with me as to why my double variable is not
being evaluated properly (this double field is basically a
total percentage holder - adding split percentages).
Any more assistance would be greatly appreciated. thanks!
 
J

John Vinson

i'm sorry for replying to you again.. but maybe i can
better explain (and i am very sorry for the multiple same
posts.. i was getting an error msg when trying to post my
msg).. anyway.. i understand the floating point types.. i
uunderstand how you said one can not really say if a
double can hold a 6 decimal place value.. but in my
situation.. it should be able to.. being that this field
actually holds percentages.. so values in my field would
never exceed the 15 places.. is there any more information
you can share with me as to why my double variable is not
being evaluated properly (this double field is basically a
total percentage holder - adding split percentages).
Any more assistance would be greatly appreciated. thanks!

An analogy might help. Suppose you were storing numbers in an abacus
with 14 bars. You can store one digit on each bar.

If you were to store 1/7 on the abacus, it would be stored as
0.14285714285714 with the rest of the infinite repeating fraction
truncated and lost. If you add this value to itself seven times you
would EXPECT to get 1.0 - seven sevenths; but in fact you will get
0.99999999999998.

In exactly the same way, storing a 48-bit binary approximation to the
infinitely repeating binary fraction which represents 0.1 will be
truncated. Adding 0.1 to itself ten times will not give you 1.0 - it
will give something resembling 0.99999999999998 or 1.00000000000001
because of the roundoff error. If you then try to compare (10 * 0.1)
to (1.0) you will find to your dismay that they are NOT equal. In
particular, if you're adding 'split percentages', you can be almost
ASSURED that the sum will be different than 1.0 exactly.

John W. Vinson[MVP]
Join the online Access Chats
Tuesday 11am EDT - Thursday 3:30pm EDT
http://community.compuserve.com/msdevapps
 
L

lp

Thank yo som much for the in-depth explanation.. but unfortunatley i am still
frustrated.. and you are too probably (bc i am not getting it 100%).. i
understand the allowable char length (don't know if im saying that right.
..what i mean is my variable (defined as double) can only show up to 15
(whether it be on the left or right of the decimal point. but the thing is
when i step through my code (and i put the cursor over my variable) it says =
1.. but then it continues on to the <> 1 statements...
if the values can only go up to 6 decimal places.. how is my logic not
working correctly.. i've added the values up on my calculator and it shows as
= 1... i don't underrstand... sorry. .thanks again your time! - jenn
 
J

John Vinson

but the thing is
when i step through my code (and i put the cursor over my variable) it says =
1.. but then it continues on to the <> 1 statements...
if the values can only go up to 6 decimal places.. how is my logic not
working correctly.. i've added the values up on my calculator and it shows as
= 1... i don't underrstand... sorry. .thanks again your time!

It's rounding the number to 6 decimal places FOR DISPLAY ONLY.

It may actually be - internally - 1.00000000000002 as I suggested.
Hovering the mouse will display a "reasonable" sized value.

Try typing

?fieldname

in the Immediate window to see the exact value; or

?fieldname - 1.0

to see how far off 1 it is.

John W. Vinson[MVP]
Join the online Access Chats
Tuesday 11am EDT - Thursday 3:30pm EDT
http://community.compuserve.com/msdevapps
 
D

Douglas J. Steele

The usual solution is to decide how close to 1 you want the value to be, and
then check for its difference from 1, not whether it's equal to 1.

For instance, rather than

If MyValue = 1 Then

use

If Abs(MyValue - 1) < 0.001 Then

You can also check what Luke Chung (President of FMS, developers of great
Access-related utilities) has to say at
http://www.fmsinc.com/tpapers/math/index.html

There's a whole area of Computer Science devoted to this sort of thing!

--
Doug Steele, Microsoft Access MVP

(No private e-mails, please)
 
J

jenn

yea.. i don't know.. i brought my debug window and listed
my function (that the variable is in) and my field still
said = 1... That's what you suggested i do, right? thanks.
 
J

jenn

thanks for the link.. looks interesting.
-----Original Message-----
The usual solution is to decide how close to 1 you want the value to be, and
then check for its difference from 1, not whether it's equal to 1.

For instance, rather than

If MyValue = 1 Then

use

If Abs(MyValue - 1) < 0.001 Then

You can also check what Luke Chung (President of FMS, developers of great
Access-related utilities) has to say at
http://www.fmsinc.com/tpapers/math/index.html

There's a whole area of Computer Science devoted to this sort of thing!

--
Doug Steele, Microsoft Access MVP

(No private e-mails, please)


unfortunatley i am
still my variable) it
says = calculator and it shows
as


.
 
Top