5 Replies - 240 Views - Last Post: 05 April 2013 - 12:39 PM Rate Topic: -----

#1 americaneagle27  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 14-October 12

for loops and assignment operators

Posted 05 April 2013 - 10:33 AM

I need to make a for loop that increments by .1 from 0 to 1.4 however when I do this it creates 1.0000001 I know I need to make an assignment operator happen somewhere in my code but I don't know where and how it should be used.


for ( float m = 0; m < 1.5; m += 0.1 )
        System.out.print(m + " ");




my result is:
0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.70000005 0.8000001 0.9000001 1.0000001 1.1000001 1.2000002 1.3000002 1.4000002

Is This A Good Question/Topic? 0
  • +

Replies To: for loops and assignment operators

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7872
  • View blog
  • Posts: 13,350
  • Joined: 19-March 11

Re: for loops and assignment operators

Posted 05 April 2013 - 10:46 AM

Your code is fine. This is due to the way floating-point numbers are represented in Java (which is the same way they're represented in just about every language, according to the IEEE standard). You can learn more by googling floating-point representation, and reading a few papers that will come up.

If you want to mask this in your output, you should look at the tools for output formatting. This shows you that you should be careful about equality of floating-point numbers (in any language). Two routes to apparently the same number can produce slight differences. The way to compare equality of floats is to define a, acceptable level of precision - for example 0.000001 - and check whether the absolute value of the difference between the two numbers is less than that number.
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: for loops and assignment operators

Posted 05 April 2013 - 10:58 AM

System.out.printf("%4.2f ", m);

will probably give you more what you expect
Was This Post Helpful? 1
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: for loops and assignment operators

Posted 05 April 2013 - 11:09 AM

I'm not sure how the assignment was stated exactly, but you might try changing the for loop to go from 10 to 150 by the appropriate step size, then dividing the result as appropriate to get the desired output. Then think about why that makes a difference, if it does.
Was This Post Helpful? 0
  • +
  • -

#5 americaneagle27  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 14-October 12

Re: for loops and assignment operators

Posted 05 April 2013 - 11:33 AM

View Postpbl, on 05 April 2013 - 10:58 AM, said:

System.out.printf("%4.2f ", m);

will probably give you more what you expect



This works, thank you! However when I add " " it gives me and error:

Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String

I need to add the space in there to space out the return values

.1 .2 .3 .4 and so on...
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: for loops and assignment operators

Posted 05 April 2013 - 12:39 PM

Just add the spaces you want in the format specifier
System.out.printf("%4.2f            ", m);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1