Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

why in float the number 99999999 becomes 1.0E8  RSS feed

 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello all
i have wired behavior here .. I have the simple value :

int i = 99999999;

and 2 methods :

 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi cowboys,
Meir wrote
why it keep going into formatf(float v) even so I gave him int value ?

Well it simply doesn't.
Put a print line in your formatf methods printing "int" or "float" respectively, you'll see, that the int-method will run.
Generally, the most closely related method will be invoked. So if you have two methods one taking int and one taking float as parameter, invoke it with an int and the int method will run.
With to methods taking an int and a short, invoked with a byte, the short method will run. etc.



Yours,
Bu
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, does this code even compile? The formatf() methods don't seem to have a return type.

And I agree with Burkhard, it should call the int version, and it should print "99999999".

Henry
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote a small program:

This prints, as expected:

int: 99999999

So if you are seeing something different, then there must be something else in your program that causes the problem.
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Josper,

public class TestIt {
public static void main(String s[])
{
int i=99999999;
formatf((float)i);
formatf(i);
}


public static void formatf(float v){System.out.println(v);}
public static void formatf(int v){System.out.println(v);}


}

OUTPUT:
-----------

1.0E8
99999999


Its very clear that float will change the format into IEEE once it crosses the maximum allowed print size.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Srinivas Kalvala:
Its very clear that float will change the format into IEEE once it crosses the maximum allowed print size.

Ofcourse, but that was not the question - the question was about why the float version instead of the int version of the method is being called, even when you call the method with an int (that is what Meir Yan thinks happens).
[ September 11, 2006: Message edited by: Jesper Young ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!