Results 1 to 6 of 6
Thread: int/double
- 01-11-2013, 05:38 AM #1
Member
- Join Date
- Jan 2013
- Location
- New Zealand
- Posts
- 7
- Rep Power
- 0
int/double
What is the difference? If I use this to calculate the square root of a number using Math.pow(), I get a decimal. I am guessing it has to do with int/double.
Java Code:class hi{ public static double squareRoot(int a){ return Math.pow(a, 0.5); } public static void main(String args[]){ System.out.println(squareRoot(16)); } }
- 01-11-2013, 05:41 AM #2
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Re: int/double
Data Types
Basically, a double is a floating point number i.e: 2.3
An int is a whole number integer i.e: 5
- 01-11-2013, 05:43 AM #3
Member
- Join Date
- Jan 2013
- Location
- New Zealand
- Posts
- 7
- Rep Power
- 0
Re: int/double
Ah ok.
So how would I make it return an int?
If I replace this:
with:Java Code:public static double squareRoot(int a)
Java Code:public static int squareRoot(int a)
I get an error.
- 01-11-2013, 05:48 AM #4
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Re: int/double
Math.pow
See if you can figure out why it won't let you return an int.
- 01-11-2013, 05:51 AM #5
Member
- Join Date
- Jan 2013
- Location
- New Zealand
- Posts
- 7
- Rep Power
- 0
Re: int/double
Oh, Math.pow only takes a double. I just converted the double to an int.
Java Code:class apples{ public static double squareRoot(double a){ return Math.pow(a, 0.5); } public static void main(String args[]){ int num = (int)squareRoot(16); System.out.println(num); } }
- 01-11-2013, 05:57 AM #6
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Similar Threads
-
Should you always use Double.compare( double d1, double d2 ) for >= and <=?
By stchman in forum New To JavaReplies: 4Last Post: 05-10-2012, 07:03 AM -
double a * double b = weird output
By GPB in forum New To JavaReplies: 3Last Post: 03-26-2010, 10:40 AM -
Check if double is double
By marshalthrone in forum New To JavaReplies: 8Last Post: 09-30-2009, 02:51 PM -
non-static method add(double,double) cannot be referenced from a static context
By cravi85 in forum Java SoftwareReplies: 5Last Post: 03-21-2009, 09:32 PM -
Double.valueOf() vs Double.parseDouble()
By greenbean in forum New To JavaReplies: 10Last Post: 01-12-2009, 08:39 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks