Jay
Sun Aug 01 19:06:27 CDT 2004
Hi Dave,
First let me say what *should* happen: When the macro calculates a
number too small to store (but not zero), the VBA core should cause an
error (this condition is formally called "underflow", the opposite of
the "overflow" error when a number is too big). Your macro should be
written to trap that error and display a message to the user, so they
know not to trust the result.
What *does* happen in VBA is that sometimes it incorrectly returns the
smallest storable number, and sometimes it rounds down to zero, but it
never causes an underflow error. For example, the calculation
Dim X As Single
X = 1.5E-45 * 0.9
MsgBox X
displays the result 1.401298E-45 (the smallest Single value) although
the correct answer is 1.35E-45. If you change the multiplier from 0.9
to 0.4, it displays the result 0.
Things are even stranger in the case of Double variables. If you try
to make a literal assignment of any number smaller than 1e-309 to a
Double, the VBA editor will replace the number you entered with the
nearest number that can be represented exactly in binary notation
(which is what the computer uses internally). For example, try to type
this in:
Dim X As Double
X = 1E-310
As soon as you move the cursor off the second line, VBA will replace
it with this:
X = 9.99999999999997E-311
Doing a calculation that results in an underflow will again give bogus
results without causing an error:
Dim X As Double
X = 1E-309 * 0.000000000000004 ' 4E-15
MsgBox X
should result in 4E-324 but does return 4.94065645841247E-324, the
smallest Double value.
I've never bothered to write a calculator in VBA -- there are plenty
of excellent calculators if you need one, and the problem doesn't
interest me as a programming exercise -- so I haven't looked at ways
to work around these bugs.
--
Regards,
Jay Freedman
http://aspnet2.com/mvp.ashx?JayFreedman
Microsoft Word MVP FAQ:
http://www.mvps.org/word
"Dave Neve" <NoAdressForSpammers@Nofs.fr> wrote:
>Hi
>
>Thanks for the maths lesson.
>
>But what about this gap then. How can you use singles and doubles data types
>especially if the user is going to assign variables like in a calculator
>program.
>
>The result of the calculation could fall into the gap.
>
>Would the program round off, cite an unhandled exception or just crash?
>
>Regards
>
>Dave Neve
>"Jay Freedman" <jay.freedman@verizon.net> a écrit dans le message de news:
>h62qg0pd7cgf6oc6lfgmpikc2k1p45hi5d@4ax.com...
>> Hi Dave,
>>
>> A negative exponent means "divide 1 by the base (10) to the positive
>> exponent", for example,
>>
>> 10 to the power of -2 = 1/(10 to the power of 2) = 1/100 = 0.01
>>
>> You can also think of exponents of 10 as "moving the decimal point",
>> to the right if positive or to the left if negative. For example,
>>
>> 3.14E2 = 314
>> 3.14E-2 = 0.0314
>>
>> You're correct, there is a gap between zero and the smallest positive
>> number that can be represented as a single or double data type.
>> There's an equal gap between zero and the smallest negative number
>> that can be represented by a single or a double. This gap is purely a
>> result of the limited number of bits used to store the number in the
>> computer's memory, and not true for real numbers. The gap is less for
>> doubles than for singles, because doubles can store more digits.
>>
>> In digital storage as in real numbers, there is only one zero and it
>> is neither positive nor negative.
>>
>> By the way, I give you credit for asking. Lots of people would just
>> shrug and say "that's the way it is".
>>
>> --
>> Regards,
>> Jay Freedman
http://aspnet2.com/mvp.ashx?JayFreedman
>> Microsoft Word MVP FAQ:
http://www.mvps.org/word
>>
>> "Dave Neve" <NoAdressForSpammers@Nofs.fr> wrote:
>>
>> >Hi
>> >
>> >I have a theoretical question on this subject but I'm naff at maths so
>don't
>> >laugh.
>> >
>> >From a book, I can see that the lower and upper limits of a single data
>type
>> >is
>> >
>> >-3.402823E38 to - 1.401298E-45 for negative values
>> >1.401298E-45 to 3.402823E38 for positive values
>> >
>> >I don't even know how to do calculations with exponents on my computer to
>> >check for myself but what I'd like to know is
>> >
>> >a How do - exponents work
>> >
>> >eg 10 to the power of 2 = 100
>> > 10 to the power of -2 = ?
>> >
>> >b Do the limits of a single or double data type leave a 'gap' in
>between?
>> >
>> >ie A short data type (-32,768 to 32767) can clearly handle any figure in
>> >between but this isn't so clear for me with regards to floating point
>data
>> >types where there seems to be a gap.
>> >
>> >Why not -0.000000 and 0.000000 or sth similar?
>> >
>> >Thanks and remember what Einstein4S Maths teacher said about him when he
>was
>> >16 (ok I ain't 16 anymore but...)
>> >
>> >
>> >Dave Neve
>> >
>> >
>>
>