Results 1 to 3 of 3
- 11-25-2009, 09:40 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 3
- Rep Power
- 0
-
The problem is likely that you're doing int division. If you divide an int by an int, you'll get an int. For instance:
1/2 will = 0
5/4 will = 1
To fix this, use double. For the inverse of two, you'll want to do one of these:
Java Code:double invOfTwo = 1.0/2.0; // or invOfTwo = (double) 1 / 2;
- 11-26-2009, 02:43 AM #3
Well, the reciprocal of a number is 1 over that number - e.g. the reciprocal of 2 is 1/2.
You would do this in Java the same way you do it on paper.
Using 1.0 ensures that the result will be a double, even if "number" is an int.Java Code:double number = 2; double inverse = 1.0 / number;
CodesAway - codesaway.info
writing tools that make writing code a little easier
Similar Threads
-
find the greatest and lowest number in 2D array
By le_albina@hotmail.com in forum New To JavaReplies: 2Last Post: 03-30-2009, 11:09 PM -
Find a number from a string
By florentp in forum New To JavaReplies: 2Last Post: 03-20-2009, 09:01 PM -
Find all permutations of a number
By matzahboy in forum New To JavaReplies: 6Last Post: 12-02-2008, 03:59 AM -
how to right a program that find kth number in two sorted array?
By fireball2008 in forum New To JavaReplies: 8Last Post: 04-22-2008, 03:21 AM -
Find nth root of a number
By perito in forum New To JavaReplies: 1Last Post: 03-03-2008, 06:51 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks