• Post Reply Bookmark Topic Watch Topic
  • New Topic

Comparing BigInteger to another numeric Datatype(Long,Byte,Int).  RSS feed

 
Ranch Hand
Posts: 47
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question is how can i check that the input variable is BigInteger or Long or Int or Byte ?

I am trying to implement the code with if else loop but First i came to know <=, ==, => didn't work with BigInteger and I have to use compareTo method for comparing input values with the defined MIN/MAX_VALUE of Long , Int, BYTE etc.

But when i use compareTo Method it gives error

"method compareTo in class BigInteger cannot be applied to given types;"

How can i correct this error or what is the correct way to compare BigInteger to other smaller numeric datatype?
 
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: What is the superclass of all these data type?
 
Linkon Manwani
Ranch Hand
Posts: 47
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's java.lang.Number but i don't get how to use it
 
Sheriff
Posts: 4295
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would happen if you took the intValue of both the numbers?
 
Linkon Manwani
Ranch Hand
Posts: 47
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:What would happen if you took the intValue of both the numbers?



intValue returns the value of the specified number as an int. This may involve rounding or truncation.

How intValue helps in determining whether the input value is BigInteger or Long or Int or something else if it involves rounding of input value?
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you looked at the API documentation?
Start with BigInteger and look at the documentation. Note what interfaces it implements; if Comparable appears there, what is it comparable to (Hint: look inside the <…>)?
Now try Number. What interfaces does it implement? Does it implement Comparable?

If it says Comparable<XYZ> then you can only use compareTo with an XYZ object.

Go back to big integer and look for its intValue method and see what it returns. If you are comparing with ints and longs consider longValue instead. If you get the MAX_VALUE fields from Integer etc, you can tell what sort of datatype they are by looking in the fields links or here(←those two for Integer). If you get an int, can it ever be greater than Integer.MAX_VALUE?
Consider creating big integers for the extreme values. Then you can use compareTo.Why are you saying you want to find what sort of value was passed at first? Do you need to know whether it was a Byte or an Integer? Nobody ever does arithmetic with bytes or Bytes. Please explain what you are trying to do very very simply so even I can understand it.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, BigMaxInt should probably read BIG_MAX_INT because it is a constant.
 
Linkon Manwani
Ranch Hand
Posts: 47
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes i looked at the API documentation and it refers to the Comparable<BigInteger> So i guess it is comparable to only BigInteger datatype.

I want something like when i input any number, it tells me in which datatype it can fits properly like

when i input 8
it says it can fit in the following datatype:
Byte
Short
Int
Long
BigInteger

Similarly other example are as following of my input and desired output :

Enter 5 numbers:
15
-165
160000
212232333333333333333333333333333333
-100000000000000

and output should be like this
15 can fit in
* byte
* short
* int
* long
* BigInteger

-165 can be fitted in:
* short
* int
* long
* BigInteger

160000 can be fitted in:
* int
* long
* BigInteger

213333333333333333333333333333333333 can be fitted in:
*BigInteger

-100000000000000 can be fitted in:
* long
* BigInteger




P.S: I know it's a very bad idea to fit 8 or 15 or any other value less than long.MAX_VALUE in BigInteger, but i was just experimenting and i found out <=, == or => doesn't work on BigIntger.
 
Linkon Manwani
Ranch Hand
Posts: 47
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Why are you saying you want to find what sort of value was passed at first? Do you need to know whether it was a Byte or an Integer? Nobody ever does arithmetic with bytes or Bytes. Please explain what you are trying to do very very simply so even I can understand it.


It was just an experiment, yes if it can tell what data type i entered it will be a plus. It's just a simple variant of above example. All i have to do is skip some line of code(By just removing some extra if conditions and print statements.). It can also be stated as finding best data-type for storing the input variable.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can determine how many bits those numbers would require to record them. But I think you can only do that by using methods of the Big Integer class. You can find how many bits each of the other Number classes use by going through their API documentation. Example for Integer here.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!