In Chun Eng's words...

Thursday, August 21, 2008

Bloody programming...!!!

What the hell!!!

1234.56 + 4567.89 of type double gives you 5802.45000007

but

1234.56 + 4567.89 of type decimal gives you the accurate 5802.45

What the hell is happening??!!!

How can I be getting such results...
How can I return such calculation results to the users??!!!

Sure will kena tiu kaw kaw wan!!!

From now on cannot use double... must only use decimal.

Labels: ,

4 Comments:

  • Eh... don't angry lah...

    I am not a programmer but I am Google freak. To answer your question on "What the hell?" (hahaha)

    Double number store in 64-bit (based 2, binary)

    Decimal is bla bla bla..... store in based 10....

    Aiyah... malas to type. Read your self.

    http://software-product-development.blogspot.com/2008/07/net-double-vs-decimal.html

    By Anonymous Anonymous, at 21 August, 2008 22:21  

  • that still does not explain how the .000007 appeared out of no where

    because this does not make any sense

    By Blogger Eng, at 22 August, 2008 08:10  

  • It did explain in Based 2 number and Based 10 number...

    In 64-bit binary (based-2) when converting back to based-10 it cause the inaccuracy.
    (normal number we used to - decimal)

    For Decimal type it store in 128 bit binary AND based-10 format

    IF... double stored in 128-bit (based-2) format you might get 0.000000000000000000000...0000x extra :P


    But since I am not math expert (kekeke) I can't explain it further.

    By Anonymous Anonymous, at 22 August, 2008 17:49  

  • Lol Eng, its ok, you're not alone in this..many a time I have to recheck my entire codes only to realise I used the wrong type for the numbers which cause errors in my program! XD

    Crazy huh?

    Sometimes the language controls us instead of the other way round hahaha...

    By Blogger Nikki Lee, at 29 August, 2008 11:03  

Post a Comment

<< Home