PC Review


Reply
Thread Tools Rate Thread

why this strange calc error in vba

 
 
Jesper F
Guest
Posts: n/a
 
      2nd Aug 2007
I'm fiddling with a function and I'm encountering something strange - what's
going on?
It seems the decimals are off.

Function MyRound(sngNumber As Single) As Single
Dim iInt As Integer
Dim sngRest As Single
iInt = Int(sngNumber)
sngRest = sngNumber - iInt
MyRound = sngRest
End Function

?MyRound(5.31) = 0.3099999
?MyRound(5.30) = 0.3000002
?MyRound(5.29) = 0.29
?MyRound(5.28) = 0.2800002

/ Jesper


 
Reply With Quote
 
 
 
 
pietlinden@hotmail.com
Guest
Posts: n/a
 
      2nd Aug 2007
On Aug 1, 5:42 pm, "Jesper F" <adfa...@asdfasdf.dk> wrote:
> I'm fiddling with a function and I'm encountering something strange - what's
> going on?
> It seems the decimals are off.
>
> Function MyRound(sngNumber As Single) As Single
> Dim iInt As Integer
> Dim sngRest As Single
> iInt = Int(sngNumber)
> sngRest = sngNumber - iInt
> MyRound = sngRest
> End Function
>
> ?MyRound(5.31) = 0.3099999
> ?MyRound(5.30) = 0.3000002
> ?MyRound(5.29) = 0.29
> ?MyRound(5.28) = 0.2800002
>
> / Jesper


is there something in particular you don't like about the built-in
Round function?

 
Reply With Quote
 
Albert D. Kallal
Guest
Posts: n/a
 
      2nd Aug 2007
Public Sub TestAdd()

Dim MyNumber As Single
Dim i As Integer
For i = 1 To 10
MyNumber = MyNumber + 1.01
Debug.Print MyNumber
Next i
End Sub


Here is the actual outpput of the above:


1.01
2.02
3.03
4.04
5.05
6.06
7.070001
8.080001
9.090001
10.1


You can see that after just 7 additions..already rounding is occurring

and if we add the following line of code to the end of the above:


if MyNumber = 10.1 = True then


msgbox "the number is 10.1"


else
msgbox "the number is something else"
endif


The above will actually produce:


the number is something else

It turns out that when using "real" numbers, the computer standard to store
these numbers is ONLY an approximation of the value. (this is one of the
first things you learn in a computing class!!!).

So, if you don't want rounding issues, then don't use double, or single
"real" numbers.

If you only need 2, or 4 decimal places then use the currency format, as it
is an actual integer, and will not suffer the rounding issue...


--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
(E-Mail Removed)


 
Reply With Quote
 
Marshall Barton
Guest
Posts: n/a
 
      2nd Aug 2007
Jesper F wrote:

>I'm fiddling with a function and I'm encountering something strange - what's
>going on?
>It seems the decimals are off.
>
>Function MyRound(sngNumber As Single) As Single
> Dim iInt As Integer
> Dim sngRest As Single
> iInt = Int(sngNumber)
> sngRest = sngNumber - iInt
> MyRound = sngRest
>End Function
>
>?MyRound(5.31) = 0.3099999
>?MyRound(5.30) = 0.3000002
>?MyRound(5.29) = 0.29
>?MyRound(5.28) = 0.2800002



Small errors like that are inherent in floating point
numbers, especially Single. You might get closer by using
Double, but there will still be small(er) errors.

If your numbers always have 4 or fewer decimal places, use
the Currency data type.

--
Marsh
MVP [MS Access]
 
Reply With Quote
 
Jesper F
Guest
Posts: n/a
 
      2nd Aug 2007
> Small errors like that are inherent in floating point
> numbers, especially Single. You might get closer by using
> Double, but there will still be small(er) errors.


Okay, thanks everyone.

/ Jesper


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Manual calc for one sheet, auto calc for another Hall Microsoft Excel Discussion 1 17th Apr 2007 04:52 AM
auto calc on, but have to edit (f2) cells to force re-calc..help! =?Utf-8?B?Q3VydA==?= Microsoft Excel Worksheet Functions 3 13th Feb 2006 07:05 PM
Spreadsheet changes from auto calc to manual calc - HELP! =?Utf-8?B?VGVpamE=?= Microsoft Excel Worksheet Functions 2 20th Aug 2004 12:34 AM
Re: Pivot Tables: Calc Items vs. Calc Fields Lady Layla Microsoft Excel Misc 2 10th May 2004 02:13 PM
OpenOffice: Calc: Spreadsheet Chart: Strange Row Values Penna Elabi Freeware 1 25th Aug 2003 10:50 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:30 PM.