469,343 Members | 5,186 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,343 developers. It's quick & easy.

How To determine whether the number is float or double?

Suppose we have a real number...We dnt know what it was defined...How can we determine that it is float or double?
Oct 5 '09 #1
22 9086
13,262 8TB
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.
Oct 5 '09 #2
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....
Oct 5 '09 #3
11,448 Expert 8TB
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,

Oct 5 '09 #4
13,262 8TB
So what language is this hypothetical function written in? It can't be Java because there is no real number data type.
Oct 6 '09 #5
Actualy the idea was based on isNumber function...So cant there be function like isFloat or isDouble
Oct 6 '09 #6
9,735 Expert Mod 8TB
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 :)

Oct 6 '09 #7
Thanks every1 ..I will study this and will get back to you incase of any problems....
Oct 6 '09 #8
13,262 8TB
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.
Oct 7 '09 #9
9,735 Expert Mod 8TB
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.

Oct 7 '09 #10
13,262 8TB
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.
Oct 7 '09 #11
9,735 Expert Mod 8TB
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.
Oct 7 '09 #12
13,262 8TB
It's an operator.
Oct 7 '09 #13
9,735 Expert Mod 8TB
Well that would explain why I can't seem to find it :)

Hehe, should have just tried google first

[edit] found it :) [/edit]
Oct 7 '09 #14
9,735 Expert Mod 8TB
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 :)

(I've been working too long in an environment where everything is an Object)
Oct 7 '09 #15
11,448 Expert 8TB
See my reply #4.

kind regards,

Oct 7 '09 #16
9,735 Expert Mod 8TB
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 :)

Oct 7 '09 #17
13,262 8TB
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);
Oct 7 '09 #18
9,735 Expert Mod 8TB
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...
Oct 7 '09 #19
11,448 Expert 8TB
Have fun:

Expand|Select|Wrap|Line Numbers
  1. public class AutoBoxing  {
  2.     public static void main(String[] args) {
  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. }
kind regards,

Oct 7 '09 #20
11,448 Expert 8TB
You also wrote you wanted reflection; well here's a bit of reflection:

Expand|Select|Wrap|Line Numbers
  1. import java.lang.reflect.Field;
  3. public class AAA  {
  4.     public static void main(String[] args) {
  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) { }
  20.         Integer m= 127;
  22.         System.out.println(m);
  23.     }
  24. }
kind regards,

Jos ;-)
Oct 7 '09 #21
9,735 Expert Mod 8TB
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.
Oct 7 '09 #22
188 100+
Beans Binding may spoil your party. It handles that for you auto.


Oct 7 '09 #23

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

12 posts views Thread by jose luis fernandez diaz | last post: by
3 posts views Thread by David Marsh | last post: by
9 posts views Thread by Steven D'Aprano | last post: by
43 posts views Thread by Xancatal | last post: by
27 posts views Thread by artifact.one | last post: by
20 posts views Thread by jacob navia | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.