Results 1 to 14 of 14
 09232008, 02:54 AM #1Member
 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?
 09232008, 04:01 AM #2Gigabytes value given by the user and convert it into the binary equivalent
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?
 09232008, 04:42 AM #3
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 25
Are you trying to find how many bits in given GB? Seems to me it is, looking at your formula.
 09232008, 06:55 AM #4Member
 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.
 09232008, 08:41 AM #5
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 25
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.
 09232008, 09:01 AM #6Member
 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;"
 09232008, 09:55 AM #7
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 25
The way you following is ok, but you have to use the correct data types. Just think about numbers you get in your calculations.
 09232008, 02:59 PM #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
 09232008, 06:05 PM #9Member
 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.
 09232008, 11:08 PM #10Java 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*bytesPerGigabyte1 = %d%n" + "Integer.MAX_VALUE = %d%n", 2*bytesPerGigabyte1, Integer.MAX_VALUE); // Choose long data type to hold gigabytes. 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)); } }
 09252008, 12:23 AM #11
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.
 09252008, 12:59 AM #12Member
 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
 09262008, 07:38 AM #13
 09262008, 08:32 AM #14Member
 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.
Similar Threads

Math Class
By ritwik07 in forum New To JavaReplies: 2Last Post: 09142009, 05:06 PM 
Java Math
By levent in forum Java TutorialReplies: 1Last Post: 05122008, 10:03 AM 
Math.Random
By Java Tip in forum Java TipReplies: 0Last Post: 11232007, 03:09 PM 
Help with math in java
By fernando in forum New To JavaReplies: 1Last Post: 08062007, 07:05 AM 
Date math
By orchid in forum New To JavaReplies: 2Last Post: 04182007, 08:01 AM
Bookmarks