Results 1 to 9 of 9

Thread: .Net and Office calculate wrong

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2003
    Posts
    16

    Angry .Net and Office calculate wrong

    Hi,

    There is a big problem in how all Microsoft products handles substractions. When you subtract two values the outcome is wrong.
    This problem is found in VB6, .NET2001 and VS2005 as well in Excel.
    Try this in Excel:
    =79,8-84,2 and =79,7-84,1 both should give -4,4 but they give -4,40000000000001 and -4,39999999999999 (in Excel you must show all decimals otherwise it will round and it looks well)

    In .NET this gives a problem when you compare the values with each other, they are NOT equal and they should be!.

    This happens in many different cases! so be aware!!

    Fred

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

    Re: .Net and Office calculate wrong

    Sounds like the evils of floating point math.

    I cannot speak for the EXCEL part, but are you using SINGLE or DOUBLE in the VB6 or .Net attempts?

    *** 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

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2003
    Posts
    16

    Re: .Net and Office calculate wrong

    Im using doubles (with singles the error is bigger). The strange part of this is when the outcome is 3 or 4 the problem does not exists......

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

    Re: .Net and Office calculate wrong

    When you do math on floating point datatypes the outcome is not accurate.

    You must use CURRENCY or DECIMAL datatypes if you want accuracy. If you do not need the decimal point then use INTEGER datatypes.

    It has to do with how the floating point datatype is stored - and how math is done on it.

    We never use SINGLE or DOUBLE.

    *** 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

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: .Net and Office calculate wrong

    I don't know what =79,7-84,1 means but can you post your .NET code? (c0d)

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

    Re: .Net and Office calculate wrong

    That is a comma format instead of decimal point format - regional settings...

    *** 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

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: .Net and Office calculate wrong

    Ohh, that European thing always confused me.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: .Net and Office calculate wrong

    There was an excellent thread on this in the VB6 forum....about three years ago. Somebody posted a thorough technical explanation of why this happens. If you wanted to dig for it, you mght find it....or get totally bored.
    My usual boring signature: Nothing

  9. #9
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: .Net and Office calculate wrong

    Essentially, zero cannot be absolutely represented using the notation that M$ (and many others) use. Sometimes it's zero, othertimes it's very very very close to zero.
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

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