Results 1 to 14 of 14
  1. #1
    gotenks05 is offline Member
    Join Date
    Sep 2008
    Posts
    34
    Rep Power
    0

    Default 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?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    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?

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  4. #4
    gotenks05 is offline Member
    Join Date
    Sep 2008
    Posts
    34
    Rep Power
    0

    Default

    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.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  6. #6
    gotenks05 is offline Member
    Join Date
    Sep 2008
    Posts
    34
    Rep Power
    0

    Default

    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;"

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

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

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    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

  9. #9
    gotenks05 is offline Member
    Join Date
    Sep 2008
    Posts
    34
    Rep Power
    0

    Default

    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.

  10. #10
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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));
        }
    }

  11. #11
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by gotenks05 View Post
    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.

  12. #12
    gotenks05 is offline Member
    Join Date
    Sep 2008
    Posts
    34
    Rep Power
    0

    Default

    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

  13. #13
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by gotenks05 View Post
    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.

  14. #14
    gotenks05 is offline Member
    Join Date
    Sep 2008
    Posts
    34
    Rep Power
    0

    Default

    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

  1. Math Class
    By ritwik07 in forum New To Java
    Replies: 2
    Last Post: 09-14-2009, 05:06 PM
  2. Java Math
    By levent in forum Java Tutorial
    Replies: 1
    Last Post: 05-12-2008, 10:03 AM
  3. Math.Random
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-23-2007, 03:09 PM
  4. Help with math in java
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 07:05 AM
  5. Date math
    By orchid in forum New To Java
    Replies: 2
    Last Post: 04-18-2007, 08:01 AM

Posting Permissions

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