Page 1 of 2 12 LastLast
Results 1 to 16 of 20

Thread: HELP!?!?! - fundementals of life failing....

  1. #1
    TiG
    TiG is offline
    Walk a mile in other peoples shoes...
    Join Date
    Jul 2003
    Location
    Questioning it all
    Posts
    6,213
    Thanks
    45
    Thanked
    48 times in 43 posts

    HELP!?!?! - fundementals of life failing....

    Okay a bit over dramatic start but i have a huge maths issue in computing !. Not language specific but how the computer chose to calculate the following sum

    int ((82.82 + 77.77) * 100)

    The answer should be 16059... But it isn't, its 16058. For the sake of my sanity help!...

    Sample code...

    import java.io.*;
    class CastTest
    {
    public static void main (String args[])
    {
    System.out.println("Test");
    double Val3=82.82;
    double Val4=77.77;
    int Val1=(int)((Val3+Val4)*100);
    System.out.println(Val1);
    }
    }

    TiG
    -- Hexus Meets Rock! --

  2. #2
    TiG
    TiG is offline
    Walk a mile in other peoples shoes...
    Join Date
    Jul 2003
    Location
    Questioning it all
    Posts
    6,213
    Thanks
    45
    Thanked
    48 times in 43 posts
    Thanks Pyle really useful answer...



    TiG
    -- Hexus Meets Rock! --

  3. #3
    Pixel Abuser Spunkey's Avatar
    Join Date
    Nov 2003
    Location
    Milton Keynes
    Posts
    1,523
    Thanks
    0
    Thanked
    0 times in 0 posts
    most. helpful. post. ever.

    the problem is rounding errors somewhere along the line, but I don't know enough about Java to be any more help than that.

    *dissapears back into shady corner*
    oooh a spiderweb

  4. #4
    Hexus.net Troll Dougal's Avatar
    Join Date
    Jun 2005
    Location
    In your eyeball.
    Posts
    2,750
    Thanks
    0
    Thanked
    0 times in 0 posts
    try declaring it as a double or float, they have the '.' operator you need.

    Int = integer = whole number onlu
    Quote Originally Posted by Errr...me
    I MSN offline people
    6014 3DMk 05

  5. #5
    TiG
    TiG is offline
    Walk a mile in other peoples shoes...
    Join Date
    Jul 2003
    Location
    Questioning it all
    Posts
    6,213
    Thanks
    45
    Thanked
    48 times in 43 posts
    Its not just Java tho, i've done the same test in C++, C#, VB.net, Some of my Phone server development languages. its the same problem

    Do the add in this format
    (X + Y) * 100 (cast as int) will break some of the time.

    If you chose not to evaluate X+Y in the brackets and instead put 160.59 in it works.

    So its the addition and the fact its being cast to an int (even tho it is impossible for it to have any fractional part)

    ARGH ARGH AGRH

    Guys, try it yourself, its soooo odd...

    There is a specific reason i need to cast to INT, nothing i can do, I have to do this and this shouldn't FAIL!, WHY DOES IT fail is what i'm trying to understand.

    TiG
    Last edited by TiG; 17-08-2005 at 03:51 PM.
    -- Hexus Meets Rock! --

  6. #6
    Hexus.net Troll Dougal's Avatar
    Join Date
    Jun 2005
    Location
    In your eyeball.
    Posts
    2,750
    Thanks
    0
    Thanked
    0 times in 0 posts
    INTS round the number to the nearest whole integer.

    Its the WAY the system is made.

    Easiest u can do is rather then

    (X + Y) * 100

    (X*100) + (Y*100)

    and that WILL work, maths proves it
    Quote Originally Posted by Errr...me
    I MSN offline people
    6014 3DMk 05

  7. #7
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts
    Sorry read the problem the wrong way round hehe
    Last edited by yamangman; 17-08-2005 at 04:04 PM.
    To err is human. To really foul things up ... you need a computer.

  8. #8
    TiG
    TiG is offline
    Walk a mile in other peoples shoes...
    Join Date
    Jul 2003
    Location
    Questioning it all
    Posts
    6,213
    Thanks
    45
    Thanked
    48 times in 43 posts
    Guys while i appreciate your help its not answering the question.

    How is it miss calculating the answer.

    Surely in a statement like this ((82.82 + 77.77) * 100)

    The computer works out 82.82+77.77 first, making 160.59 (two floats added together are still a float) then multiplies this by 100 and makes 16059.

    How in the world can it make it 16058... that doesn't make sense, Its nothing to do with INT!.

    if it was it the answer would be 16100

    I'm looking for some help to understand how its generating the 16058 answer. I've already got a work around to correctly add up. But i'm stumped to how it can come back with 16058.

    Thanks
    TiG
    -- Hexus Meets Rock! --

  9. #9
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts
    The answer is 16058. When you cast to an integer that is the answer.
    To err is human. To really foul things up ... you need a computer.

  10. #10
    Senior Member
    Join Date
    Jul 2003
    Location
    Pit, stone.
    Posts
    643
    Thanks
    0
    Thanked
    0 times in 0 posts
    Well...in foxpro:

    nWne=82.82
    nTwo=77.77
    nThree=INT((nWne+nTwo)*100)
    ?nThree

    gives nThree=16059.

    Why not just declare a double, add the two numbers together, and perform your int calculation on the resulting number?

    so nThree=nWne+nTwo
    nThree=int(nThree*100)

    or from your code

    import java.io.*;
    class CastTest
    {
    public static void main (String args[])
    {
    System.out.println("Test");
    double Val3=82.82;
    double Val4=77.77;
    double Val5=Val3+Val4;
    int Val1=(int)(Val5*100);
    System.out.println(Val1);
    }
    }

    Er I don't know any Javascript by the way so if the way of adding variables up is crap well...I'm sure you can see what I'm getting at?
    Well Hello!

  11. #11
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts
    Quote Originally Posted by daverobev
    ....
    I think the question is why is it miscalculating the answer, the answer to which it isn't. It's getting it right, but casting the value to an int results in an answer of 16058.
    To err is human. To really foul things up ... you need a computer.

  12. #12
    TiG
    TiG is offline
    Walk a mile in other peoples shoes...
    Join Date
    Jul 2003
    Location
    Questioning it all
    Posts
    6,213
    Thanks
    45
    Thanked
    48 times in 43 posts
    ahah i've worked it out.

    calculated what 82.82 and 77.77 are in binary, which sadly isn't actually 82.82 but some close value due to the way its stored.

    So they will be represented as
    82.81999999998 and 77.77 as 77.77000000001. (I made up those numbers) Thus, 100*(82.82 + 77.77) = 16058.999999999,

    And when i cast to int its 16058.

    Makes sense now.

    Thanks
    TiG
    -- Hexus Meets Rock! --

  13. #13
    Senior Member
    Join Date
    Jul 2003
    Location
    Pit, stone.
    Posts
    643
    Thanks
    0
    Thanked
    0 times in 0 posts
    Ok problem with these threads is that, while I'm pondering the response, the OP has already updated his question

    yamangman I get what Tig's saying since his second post (which happened while I was thinking about my first).

    But WHY is the answer 16058? Where the hell is it rounding down to get that answer? If you understand why, could you post a kind of step by step computer logic as to what is going on? IE the bits the computer is doing to compute that.

    Thanks!

    *Edit* stores it in Binary? Poor computer, having all its fingers (leaving only its thumbs to count upon) chopped off at birth.
    Well Hello!

  14. #14
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts
    Yea, cast's to int don't round up or down. Just the whole number.

    82.82 + 77.77 = 160.58999999999997, not 160.59.
    To err is human. To really foul things up ... you need a computer.

  15. #15
    TiG
    TiG is offline
    Walk a mile in other peoples shoes...
    Join Date
    Jul 2003
    Location
    Questioning it all
    Posts
    6,213
    Thanks
    45
    Thanked
    48 times in 43 posts
    sorry i'm hot, bothered and in a foul mood, this bug has been causing problems and has been unnoticed for months, suddenly lots of calculations are a penny short etc

    Had to understand WHY it failed to calm my customer.

    TiG
    -- Hexus Meets Rock! --

  16. #16
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,164
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts
    you should NEVER use floats for anything accurate.

    EVER.

    Floats are floating point precision, this means they are approximate. Used fixed precision instead.

    ie
    ( 2131 + 2853) * 10000
    instead of
    ( 21.31 + 28.53) * 100

    If you look up how floating points work, java uses (like everything now, except embedded naughty people like me ) IEEE Standard 754 Floating-Point. As someone said the nearest you can store the number isn't what you think it is, ie this code might not behave how you'd think

    double foo;
    foo = 0.2;
    foo += 0.1;
    if (0.3==foo) {
    alert ("it does!");
    }

    For more info see google and
    http://en.wikipedia.org/wiki/IEEE_fl...int_arithmetic

    Remeber when you start programming for people, were your liability is, don't bite off more than you can chew, as soon as working directly with money gets in, you need to be so careful.
    throw new ArgumentException (String, String, Exception)

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Ebay Feedback - failing system
    By ikonia in forum General Discussion
    Replies: 9
    Last Post: 26-07-2005, 01:12 PM
  2. Life explained in a few lines of text..
    By Spud1 in forum General Discussion
    Replies: 5
    Last Post: 11-07-2005, 09:41 AM
  3. 22 Random ramblings on life
    By 0iD in forum General Discussion
    Replies: 7
    Last Post: 15-04-2005, 01:50 PM
  4. what do you believe
    By jsterling in forum General Discussion
    Replies: 153
    Last Post: 09-11-2004, 10:10 PM
  5. Half Life II Vid.
    By Stewart in forum Gaming
    Replies: 23
    Last Post: 15-01-2004, 08:39 PM

Posting Permissions

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