Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By gimbal2
  • 1 Post By KevinWorkman

Thread: Float not decrementing properly

  1. #1
    Karlovsky120 is offline Member
    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0

    Arrow Float not decrementing properly

    I couldn't find a proper subforum to post it on, so I went with the most popular... Sorry. =(

    So I have this code:

    Java Code:
        float characterSpacing = 0.3f;
    
        while (condition)
        {
            characterSpacing -= 0.005f;
            System.out.println(characterSpacing+" "+unimportantNumber1+" "+unimportantNumber2);
        }

    Output is this:

    Java Code:
        0.3   446.81253   426.75
        0.29500002   446.3275   426.75
        0.29000002   445.84253   426.75
        0.28500003   445.3575   426.75
        0.28000003   444.87253   426.75
        0.27500004   444.3875   426.75
        0.27000004   443.90253   426.75
        0.26500005   443.41754   426.75
        0.26000005   442.93253   426.75
        0.25500005   442.44754   426.75
        0.25000006   441.96252   426.75
        0.24500006   441.47754   426.75
        0.24000007   440.99252   426.75
        0.23500007   440.50754   426.75
        0.23000008   440.02252   426.75
        0.22500008   439.53754   426.75
        0.22000009   439.05252   426.75
        0.2150001   438.56754   426.75
        0.2100001   438.08252   426.75
        0.2050001   437.59753   426.75
        0.2000001   437.11255   426.75
        0.19500011   436.62753   426.75
        0.19000012   436.14255   426.75
        0.18500012   435.65753   426.75
        0.18000013   435.17255   426.75
        0.17500013   434.68753   426.75
        0.17000014   434.20255   426.75
        0.16500014   433.71753   426.75
        0.16000015   433.23254   426.75
        0.15500015   432.74753   426.75
        0.15000015   432.26254   426.75
        0.14500016   431.77753   426.75
        0.14000016   431.29254   426.75
        0.13500017   430.80756   426.75
        0.13000017   430.32254   426.75
        0.12500018   429.83755   426.75
        0.120000176   429.35254   426.75
        0.11500017   428.86755   426.75
        0.11000017   428.38254   426.75
        0.10500017   427.89755   426.75
        0.100000165   427.41254   426.75
        0.09500016   426.92755   426.75
        0.09000016   426.44254   426.75
    Why isn't the value decrementing by 0.005f, but something similar? How can I avoid it, at least to a certain number of decimal places?
    Last edited by Karlovsky120; 08-13-2013 at 07:42 PM.

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,685
    Rep Power
    5

    Default Re: Float not decrementing properly

    Well if you must know WHY, be prepared:

    What Every Computer Scientist Should Know About Floating-Point Arithmetic

    Whenever you want to do any serious floating point work, consider using the BigDecimal class.
    Karlovsky120 likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default Re: Float not decrementing properly

    Karlovsky120 likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,685
    Rep Power
    5

    Default Re: Float not decrementing properly

    lol :) Well at least we're passing the same links at the same time ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Karlovsky120 is offline Member
    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0

    Default Re: Float not decrementing properly

    Thanks guys, I ended up using the BigDecimal. =)

Similar Threads

  1. ArrayList, incrementing and decrementing fields
    By kyle_maddisson in forum New To Java
    Replies: 3
    Last Post: 11-07-2011, 09:49 PM
  2. [Q] - pi as a float
    By oran0007 in forum New To Java
    Replies: 1
    Last Post: 09-30-2011, 10:33 AM
  3. regarding int and float
    By lakshmibvaraprasad in forum New To Java
    Replies: 2
    Last Post: 07-24-2011, 08:30 PM
  4. FloatBuffer and 'put( float[] src )'
    By maxkiva in forum New To Java
    Replies: 4
    Last Post: 08-08-2010, 11:56 AM
  5. Error in float value
    By JMaste in forum New To Java
    Replies: 5
    Last Post: 07-11-2010, 04:08 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
  •