# Thread: need help with math for a new program

1. Member Join Date
Sep 2008
Posts
34
Rep Power
0

## need help with math for a new program

As the title says, I need help with the math involved for my next program idea. My idea has probably already been done, but I want to make my own in Java anyway (also to provide a resource to look up the formula offline). What the program is supposed to do is take the Gigabytes value given by the user and convert it into the binary equivalent. Through a bit of testing with a openoffice spreadsheet, I came up with the formula a=(GB*10^9)/1024^3 (using my computer's hard drive info for comparison and editing accordingly). I know that I'll be using Math.pow() or something similar (I can get that info from the books I have), so I'm aware the formula will look different in Java. in this formula, "a" stands for actual. Is the formula that I came up with accurate enough or is there a better formula?  Reply With Quote

2. ## Gigabytes value given by the user and convert it into the binary equivalent
Not sure what that means. Can you give an example. Would it be:
Given a value: 4.2G you want a string of 0s and 1s with that represents that value in binary?
Can you use Java methods or do you have to write your own code?  Reply With Quote

3. ## Are you trying to find how many bits in given GB? Seems to me it is, looking at your formula.  Reply With Quote

4. Member Join Date
Sep 2008
Posts
34
Rep Power
0

## I think you've probably got it right, Eranga. What I'm trying to have the problem tell me is how much storage I've actually got. Basically, hard drive and other media storage companies tell you that the media has more storage than it actually does. By binary numbers, I mean numbers that are the result of the pattern 2^x (i.e. 2^10 = 1024). For example, my computer came with a 250 GB hard drive, but when I pull up the info on the hard drive, I see a capacity of 232.57 GB, not 250 GB. I want the program to be able to calculate that using the input, what the amount of storage labeled on the box, and then I have it display the results from the math.  Reply With Quote

5. ## Ok, now what the problem you have. If you want to find the power of a number and so on, best thing it use Math class.  Reply With Quote

6. Member Join Date
Sep 2008
Posts
34
Rep Power
0

## Yeah, I knew that part since I made a program to calculate square roots before. I wanted to know if my formula was accurate because the puesudo code would so far be this

double num = 1024;
Display "Enter labeled storage (GB):";
decbytes = "GB * 1,000,000,000";
actual = "decbytes/Math.pow(num, 3)";
Display "available storage is " + actual;"  Reply With Quote

7. ## The way you following is ok, but you have to use the correct data types. Just think about numbers you get in your calculations.  Reply With Quote

8. ## Is the problem a marketing question?
a programmer considers 1K = 1024 but salesmen use 1K=1000.
So if a salesman says: it has 250G, a programmer would get 233G  Reply With Quote

9. Member Join Date
Sep 2008
Posts
34
Rep Power
0

## You could probably say that, Norm. However, some people don't know that they are getting less than what it says on the box due to formatting the drive, so assuming the user is buying a new hard drive (250+ GB, but not 1+ TB, since doing TB would require the numbers to be 10^12 and 1024^4) or a blank DVD, the user should be able to open up the program and get how much they can actually use. Well, I've got the program made and put into an executable jar, so now I can have other people test it.  Reply With Quote

10. ## Java Code:
```public class Test {
public static void main(String[] args) {
int bytesPerKilobyte = 1024;
int bytesPerMegabyte = 1024*bytesPerKilobyte;
int bytesPerGigabyte = 1024*bytesPerMegabyte;
System.out.printf("bytesPerKilobyte  = %10d%n" +
"bytesPerMegabyte  = %10d%n" +
"bytesPerGigabyte  = %10d%n",
bytesPerKilobyte, bytesPerMegabyte,
bytesPerGigabyte);
System.out.println("maximunm capacity of int and long data types:");
System.out.printf("Integer.MAX_VALUE = %d%nLong.MAX_VALUE    = %d%n",
Integer.MAX_VALUE, Long.MAX_VALUE);
System.out.println("an int data type will overflow with:");
double maxGigs = (double)Integer.MAX_VALUE/(double)bytesPerGigabyte;
System.out.printf("maxGigs = %f%n", maxGigs);
System.out.printf("2*bytesPerGigabyte-1 = %d%n" +
"Integer.MAX_VALUE    = %d%n",
2*bytesPerGigabyte-1, Integer.MAX_VALUE);
// Choose long data type to hold giga-bytes.
long gigabytes = 250;
long bytes = gigabytes*bytesPerGigabyte;
System.out.println("bytes = " + bytes);
System.out.println();

// Another way to look at is with computer/binary math:
// 1024 = 2^10
System.out.printf("1024 >> 10 = %4d%n", 1024 >> 10);
System.out.printf("   1 << 10 = %4d%n",    1 << 10);
for(int i = 0; i <= 10; i++) {
System.out.printf("1024 >> %2d = %4d%n", i, 1024 >> i);
}
// So, bytes = gigabytes * 2^10+10+10
//           = gigabytes << 30
System.out.println("bytes = " + gigabytes*Math.pow(2, 30));
System.out.println("bytes = " + (gigabytes << 30));
}
}```  Reply With Quote

11. ##  Originally Posted by gotenks05 Basically, hard drive and other media storage companies tell you that the media has more storage than it actually does.
No, its just that hard drive vendors count in decimal, and most other computer things count in binary. So a 250 GB disk drive has 250*1000*1000*1000 bytes of raw storage.

In most computer stuff, we call 1024 a K, and 1024^2 a "meg" and 1024^3 a gig. Use the proper phrase and it aill works out.  Reply With Quote

12. Member Join Date
Sep 2008
Posts
34
Rep Power
0

## True, vendors do count in decimal because base 10 is easier to convert to higher or lower measurements. That is how they get a higher number that ends in zero, whereas binary gets a bit difficult the higher you go  Reply With Quote

13. ##  Originally Posted by gotenks05 True, vendors do count in decimal because base 10 is easier to convert to higher or lower measurements. That is how they get a higher number that ends in zero, whereas binary gets a bit difficult the higher you go
Actually, I think the hard drive vendors count in decimal because it makes a bigger number to put on the box.

750GB sounds a lot bigger than 698GB.  Reply With Quote

14. Member Join Date
Sep 2008
Posts
34
Rep Power
0

## yeah, but it is also because they assume the consumer stupid too. True, people will go for more storage, but some will also assume that what's on the box is the amount they get, when formatting takes that away. Well, question on accuracy on the formula was answered on an Ubuntu forum and apparently the formula was the exact one and found out that file systems takes up space on a hard drive too.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•