Results 1 to 10 of 10

Thread: Round function and Single vs Double data types

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Round function and Single vs Double data types

    Just an FYI
    I was just bitten in the Access VBA by this ... idiosyncrasy,

    Note the difference in rounding of the same number when it's Single vs Double.
    VB Code:
    1. Public Sub Test_Round()
    2.     Dim sngX As Single
    3.     Dim dblX As Double
    4.  
    5.     sngX = 45.315
    6.     dblX = 45.315
    7.    
    8.     Debug.Print Round(sngX, 2)  ' = 45.31
    9.     Debug.Print Round(dblX, 2)  ' = 45.32
    10. End Sub

    I'm sure there's a very good explanation for this, that will go right over my head, like "Haven't you heard of 'Drunken Banker's Rounding?'

    It (mis)behaves the same way in Access VBA, VB6 and VB.NET,
    so, I'll give MS points for keeping their inconsistency consistent.
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Round function and Single vs Double data types

    that is really interesting and now i am going to do some tests to see if it will resolve another problen i was having. thiugh i doubt that it will

    pete

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Re: Round function and Single vs Double data types

    Question, does anyone know if the change from single to double (which fixed the problems that I "knew" about) will actually create other problems?

    i.e. does using a double have it's own quirks that will just shift the math problems elsewhere?

    Thanks, DaveBo
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Round function and Single vs Double data types

    double is more accurate than a single, but uses more memory for storage

    pete

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Round function and Single vs Double data types

    A Single is an 4 byte floating point number ranging from -2.40E38 to 3.40E+38
    A Double is an 8 byte floating point number ranging from -1.79E+308 to 1.79E+308

  6. #6
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Round function and Single vs Double data types

    Why is this a problem?
    As you stated, VB6 uses Banker's rounding which is a rule for determining which way to round a 5. It should always make the result even. This is to try to even out the number of times rounded up vs. rounded down.

    If instead the rule rounded toward the odd number (as you've demonstrated it does with single precision numbers) the evening out will be the same.

    The last digit after rounding is always going to have an uncertainty associated with it and hence should not be heavily depended on.

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Round function and Single vs Double data types

    First you have to live with Banker's rounding - that's the MS way...

    But you chose to use SINGLE and DOUBLE - that was a poor choice.

    Use CURRENCY if you want accuracy - single and double are imprecise database - in other words they do not work!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Round function and Single vs Double data types

    First you have to live with Banker's rounding - that's the MS way...
    No you don't, you can always write your own routines if you prefer another method.

    single and double are imprecise database - in other words they do not work!
    What?...that would be news to millions of programmers around the world who use double precision numbers.

    Use CURRENCY if you want accuracy
    Currency, like integer and long, work well within certain bounds. Any more than four decimal places and Currency can't handle it.

  9. #9
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Round function and Single vs Double data types

    Quote Originally Posted by moeur
    ...What?...that would be news to millions of programmers around the world who use double precision numbers....
    It's actually pretty well known that Doubles are imprecise. Try this.

    VB Code:
    1. Dim x As Double
    2.     Dim y As Long
    3.    
    4.     x = 5
    5.     For y = 100 To 1 Step -1
    6.         x = x - 0.1
    7.         If x = 0 Then
    8.             MsgBox "this will never happen because doubles are imprecise"
    9.         End If
    10.         Debug.Print x
    11.     Next

  10. #10
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Round function and Single vs Double data types

    Quote Originally Posted by moeur
    No you don't, you can always write your own routines if you prefer another method.
    You certainly can - then get burned when one of the programmers in your shop uses the language default function...

    What?...that would be news to millions of programmers around the world who use double precision numbers.
    Yes - I've been trying to educate those millions of programmers for 20 years now. Floating point is for launching missles - speed was the reason for it's creation. It uses log-tables for math and does neat little things like store 4 as 3.99999

    Currency, like integer and long, work well within certain bounds. Any more than four decimal places and Currency can't handle it.
    We develop financial and pension software here - currency is what we are forced to live with in the MS/VB/PC world. Our mainframes had a 64 digits exact datatype for math - they called it string arithmetic.

    We have been down this path in other threads before...

    http://www.vbforums.com/showthread.p...le+double+evil

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width