Connecting Tech Pros Worldwide Help | Site Map

How To determine whether the number is float or double?

Newbie
 
Join Date: Aug 2009
Posts: 15
#1: Oct 5 '09
Suppose we have a real number...We dnt know what it was defined...How can we determine that it is float or double?
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Oct 5 '09

re: How To determine whether the number is float or double?


Where do we have it? In a String?
Give more details and also explain what you have tried or what your ideas are about it.
Newbie
 
Join Date: Aug 2009
Posts: 15
#3: Oct 5 '09

re: How To determine whether the number is float or double?


Its not a String its a real number(already mentioned)..

Regarding idea --- I am receiving a real number from a function. I need to print what the received number is float or double ? We can determine by storing it in float and see the compiler error of incompatible types but I have to print that in a program? I dnt kw about the return type but know that it is a real number that is being returned....
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Oct 5 '09

re: How To determine whether the number is float or double?


Quote:

Originally Posted by puneetsardana88 View Post

Its not a String its a real number(already mentioned)..

Regarding idea --- I am receiving a real number from a function. I need to print what the received number is float or double ? We can determine by storing it in float and see the compiler error of incompatible types but I have to print that in a program? I dnt kw about the return type but know that it is a real number that is being returned....

That is a silly requirement; if a method returns a double and you try to receive it in a float variable the compiler would complain. The other way around is silly as well (the compiler won't complaint) because you can know the return type of the method. The entire exercise is just silly and not worth this discussion.

kind regards,

Jos
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#5: Oct 6 '09

re: How To determine whether the number is float or double?


So what language is this hypothetical function written in? It can't be Java because there is no real number data type.
Newbie
 
Join Date: Aug 2009
Posts: 15
#6: Oct 6 '09

re: How To determine whether the number is float or double?


Actualy the idea was based on isNumber function...So cant there be function like isFloat or isDouble
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#7: Oct 6 '09

re: How To determine whether the number is float or double?


What you want to use is Reflection.

Reflection looks at the data types. You can use it to determine what data type an Object is....You really should research this topic before you continue.

You can start by reading this article provided by Sun called Trail: The Reflection API. After that, if you're still confused by the topic, you should google it and then try it out.

If you have any problems with Reflection then get back to us :)

-Frinny
Newbie
 
Join Date: Aug 2009
Posts: 15
#8: Oct 6 '09

re: How To determine whether the number is float or double?


Thanks every1 ..I will study this and will get back to you incase of any problems....
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#9: Oct 7 '09

re: How To determine whether the number is float or double?


Reflection doesn't have much to do with it at all.
The question that the OP needs to answer is what would the isNumber function be accepting? The question as put isn't really making much sense.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#10: Oct 7 '09

re: How To determine whether the number is float or double?


I would imagine that the isNumber method would accept an Object as a parameter...and from that Object the OP could use reflection to figure out what Type of Object it is and return true/false, or print whatever they need to print accordingly.

-Frinny
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#11: Oct 7 '09

re: How To determine whether the number is float or double?


Safe bet but the OP didn't specify. In that case, if the type passed is Object then instanceof will do the job without using reflection.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#12: Oct 7 '09

re: How To determine whether the number is float or double?


Quote:

Originally Posted by r035198x View Post

Safe bet but the OP didn't specify. In that case, if the type passed is Object then instanceof will do the job without using reflection.

The OP probably didn't know what to pass the method.

:) instanceof Sounds cool :)
Didn't know there was such a method.
I'll check it out.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#13: Oct 7 '09

re: How To determine whether the number is float or double?


It's an operator.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#14: Oct 7 '09

re: How To determine whether the number is float or double?


Well that would explain why I can't seem to find it :)

Hehe, should have just tried google first

[edit] found it :) [/edit]
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#15: Oct 7 '09

re: How To determine whether the number is float or double?


Hmm...r035198x you might be right.

I'm not sure if this suggestion is even going to help the OP.
If the isNumber() method doesn't accept an Object then this whole suggestion won't work. I get the impression that the instanceof operator won't work with primitive data types (like float and double) because it compares Objects to Types. The OP would have to be looking for Double or Float Objects not primitive double or float types.

I can't see this working any other way though...
I guess we'll wait and see what the OP has to say about it :)

Thanks r035198x, I learned something new today :)

-Frinny
(I've been working too long in an environment where everything is an Object)
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#16: Oct 7 '09

re: How To determine whether the number is float or double?


Quote:

Originally Posted by Frinavale View Post

I can't see this working any other way though...
I guess we'll wait and see what the OP has to say about it :)

See my reply #4.

kind regards,

Jos
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#17: Oct 7 '09

re: How To determine whether the number is float or double?


You're right Jos, this is a silly requirement not worthy of discussion.
But, on the bright side, because I tried to discuss it I learned about the instanceof operator.

Maybe I should start playing around with Java again :)

-Frinny
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#18: Oct 7 '09

re: How To determine whether the number is float or double?


Quote:

Originally Posted by Frinavale View Post

Hmm...r035198x you might be right.

.. I get the impression that the instanceof operator won't work with primitive data types (like float and double) because it compares Objects to Types. The OP would have to be looking for Double or ..

Spoil yourself ...
Expand|Select|Wrap|Line Numbers
  1. float f = 0.5f;
  2. Object o = f;
  3. System.out.println(o instanceof Double);
  4. System.out.println(o instanceof Float);
  5.  
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#19: Oct 7 '09

re: How To determine whether the number is float or double?


Hehe, thanks for the push over to the dark side r035198x...mind you, I was already heading there. Before I checked back here I was thinking: "What happens if you pass a primitive type to a method that expects an Object? Does Java automatically box the primitive type into the appropriate Number Object type? Would my suggestion actually work?"

I think I am going to take your advice and spoil myself.

Give me a second I need to see if I have a Java Compiler installed...
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#20: Oct 7 '09

re: How To determine whether the number is float or double?


Quote:

Originally Posted by Frinavale View Post

I think I am going to take your advice and spoil myself.

Have fun:

Expand|Select|Wrap|Line Numbers
  1. public class AutoBoxing  {
  2.     public static void main(String[] args) {
  3.  
  4.         Integer i = new Integer(42);
  5.         Integer j = new Integer(42);
  6.         boolean result = !(i < j) && !(i > j) && !(i == j);    
  7.         System.out.println(result);
  8.     }
  9. }
  10.  
kind regards,

Jos
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#21: Oct 7 '09

re: How To determine whether the number is float or double?


You also wrote you wanted reflection; well here's a bit of reflection:

Expand|Select|Wrap|Line Numbers
  1. import java.lang.reflect.Field;
  2.  
  3. public class AAA  {
  4.     public static void main(String[] args) {
  5.  
  6.         try {
  7.             Class[] classes = Integer.class.getDeclaredClasses();
  8.             for (Class clazz : classes) {
  9.                 if (clazz.getName().endsWith("IntegerCache")) {
  10.                     Field cacheField = clazz.getDeclaredField("cache");
  11.                     cacheField.setAccessible(true);
  12.                     Integer[] cache = (Integer[]) cacheField.get(null);
  13.                     for (int i = 0; i < cache.length; i++) {
  14.                         cache[i] = new Integer(42);
  15.                     }
  16.                 }
  17.             }
  18.         } catch (Throwable e) { }
  19.  
  20.         Integer m= 127;
  21.  
  22.         System.out.println(m);
  23.     }
  24. }
  25.  
kind regards,

Jos ;-)
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#22: Oct 7 '09

re: How To determine whether the number is float or double?


I love reflection!

I use it almost every day and I don't know how I ever worked without it in the past.

Whenever I have some Object or Structure that I want to "display for editing" I dynamically create the controls required for editing the Object/Structure based on the Object/Structure's properties/members.

I loop through each of the Object/Structure's properties/members and dynamically create an appropriate control that will let the end user edit this property/member. I determine what control would be best to use for editing the property/member based on the property/member's Type.

I also dynamically create controls to use as "prompts". That way the end user knows what property/member the editing-control represents. I grab the text used for the prompt from an embedded language resources based on the name of the property/member and the user's cultural preferences.

When you consider that some of the Objects/Structures that I work with have more than 60 properties/members this technique beats saves me a lot of time!

The nicest thing about reflection is that it makes my life a lot easier when it comes to fixing interoperability issues that I have to deal with on a daily bases.

Reflection is very powerful and not only saves me a lot of time as a developer but also makes my code a lot cleaner.
myusernotyours's Avatar
Familiar Sight
 
Join Date: Nov 2007
Posts: 168
#23: Oct 7 '09

re: How To determine whether the number is float or double?


Quote:

Originally Posted by Frinavale View Post

I love reflection!

I use it almost every day and I don't know how I ever worked without it in the past.

Whenever I have some Object or Structure that I want to "display for editing" I dynamically create the controls required for editing the Object/Structure based on the Object/Structure's properties/members.

Beans Binding may spoil your party. It handles that for you auto.

Regards,

Alex
Reply