• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can we use float variables inside a if else statement?  RSS feed

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please tell me with an example


And tell me can we do this
Float pi = new Float(3.14f);
if (pi > 3.14)
{
System.out.println("big");
}
else
{
System.out.println("small");
}
}
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yaswant kumar kumar wrote:please tell me with an example

Haven't you already provided one? Presuming it compiles - and I have no reason to think it won't - the answer to your subject line question is obviously: 'yes'.

But tell me: what do you think your example does? Hint: it's not as simple as you might think.

Winston
 
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is innocently looking, but hey,

f.java:7: operator > cannot be applied to java.lang.Float,double
if ( pi > 3.14 )

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:It is innocently looking, but hey,
f.java:7: operator > cannot be applied to java.lang.Float,double
if ( pi > 3.14 )

Erm: yes it can; and I just tested it myself to be sure. I wonder if you're thinking about assignment?

@yaswant: My question still stands. What do you think your example does?

Winston
 
yaswant kumar kumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
yaswant kumar kumar wrote:please tell me with an example

Haven't you already provided one? Presuming it compiles - and I have no reason to think it won't - the answer to your subject line question is obviously: 'yes'.

But tell me: what do you think your example does? Hint: it's not as simple as you might think.

Winston


tell me whether this statement is perfect is perfect for above code i.e
for ( pi>3)
some body said you cant use ">" operator for float variables

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yaswant kumar kumar wrote: some body said you cant use ">" operator for float variables

Then they were wrong. As I've been trying to explain, the question is not whether you can do it, but what it does.

Maybe we should back up a bit: What are you trying to do?

Winston
 
yaswant kumar kumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
yaswant kumar kumar wrote: some body said you cant use ">" operator for float variables

Then they were wrong. As I've been trying to explain, the question is not whether you can do it, but what it does.

Maybe we should back up a bit: What are you trying to do?

Just want to check whether we can use new with Float Wrapper class and compare a float variable with a number and if it is yes print big else print small that's all

Winston


 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yaswant kumar kumar wrote:Just want to check whether we can use new with Float Wrapper class and compare a float variable with a number and if it is yes print big else print small that's all

Right. Well, as before, the answer to that question is 'yes', presuming that the other "number" has been converted accordingly. For example, if it's a BigInteger, then the comparison may or may not have any meaning depending on the size of the BigInteger. However, I suspect that's beyond the scope of your question.

The problem usually arises when you compare two floating point numbers of different types, which is what you have been doing for this entire thread; and was why I asked the question I did.

And I still don't have an answer:
What do you think comparing a Float/float with a value of 3.14 (or actually, 3.14f) with a Double/double of 3.14 will do?

The only thing I can tell you is that it's unlikely to be equal.

Winston
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's also possible that the person saying you can't use < misunderstood the idea that often with floating point number you should use < (or <=). Using == can be dangerous, because floating point numbers aren't stored exactly. So you can run into problems like this:



So in many cases, the correct approach is to check that floating point numbers lie within a small range rather than have an exact value.
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:It is innocently looking, but hey,


Okay, my bad, it was the pre-generics compiler. Since 1.5 that works. I am sorry for the possible confusion...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!