Results 1 to 4 of 4
  1. #1
    mackiver is offline Member
    Join Date
    Oct 2011
    Posts
    1
    Rep Power
    0

    Default Help decimal number { float or double??} HELP!

    Please if u could run the program, and when asked please use 1 for lower limit and 3 for upper limit, and finally 46 for n.

    I have assigned deltaX = ( ( upperLimit - lowerLimit) / subInt ) and then multiplied it by 1/3, which is the value of numb which does not give me a decimal number, same goes for variable answer, i want these variables to be represented in decimal numbers rounded to the 6th place. Any help would be highly appreciated. Thanks!
    Java Code:
    
    import java.util.Scanner;
    import java.math.BigDecimal;
    public class Simps {
    
        
        public static void main ( String args[] ) {
            
            Scanner input = new Scanner(System.in);
            
            double lowerLimit, upperLimit, subInt;
            double deltaX;
            double deltaN = 1/3;
            double answer;
            double numb;
            
            System.out.println("Please enter your lower limit (a): \n");
            lowerLimit = input.nextDouble();
            
            System.out.println("Please enter your upper limit (b): \n ");
            upperLimit = input.nextDouble();
            
            System.out.println("Please enter your subinterval (n): \n ");
            subInt = input.nextDouble();
            
            deltaX = ( ( upperLimit - lowerLimit) / subInt );
            
            System.out.printf(" Your h is : %f \n" , deltaX);
            
            numb = deltaN * deltaX;
            
            System.out.printf(" h will be multiplied by 1/3: %f \n\n" , numb);
            
            answer = (float) (( 1 + (23/6) + (46/25) + (46/13) + (46/27) + (23/7) + (46/29) + (46/15) + (46/31) + (23/8) + (46/33) + (46/17) + (46/35) + (23/9) + (46/37) + 
                    (46/19) + (46/39) + (23/16) + (46/41) + (46/21) +(46/43) + (23/11) + (46/45) + (2) + (46/47) + (23/12) + (46/49) + (46/25) + (46/51) + (23/13) + 
                    (46/53) + (46/27) + (46/55) + (23/14) + (46/57) + (46/29) +(46/59) + (23/15) + (46/61) + (46/31) + (46/63) + (23/16) + (46/65) + (46/33) + 
                    (46/67) + (23/17) + (1/3) ) * numb);
            
            System.out.printf("By Simpson's rule, the final answer is: %f" , answer );
            
            
            
          
        }
        
    }
    Last edited by mackiver; 10-24-2011 at 04:19 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Help decimal number { float or double??} HELP!

    i want these variables to be represented in decimal numbers rounded to the 6th place.
    Be careful! If you start rounding deltaX it must become less accurate. And, in that case, you ought to call the method "quasi-" Simpson, since such rounding is not what the 18th century mathematician had in mind. (or Kepler before him)

    Displaying numeric quantities with a finite (and smallish) number of decimal places in its String representation, as you might want to do with the answer is another matter: that's not rounding, it's formatting. The printf() method is designed to do just that.

    Java Code:
    System.out.printf("By Simpson's rule, the final answer is: %.6f" , answer );
    For details see the printf() API docs (which includes a link to the format string syntax). There are also usage examples in the Formatting Numeric Print Output page of Oracle's Tutorial.
    Last edited by pbrockway2; 10-24-2011 at 05:00 AM.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Help decimal number { float or double??} HELP!

    If you want a floating point number to be displayed in a certain format then you can use the DecimalFormat class. Note that this class does not alter the value of the number it simply generates a String representation according to your parameters that can be displayed to the user.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Help decimal number { float or double??} HELP!

    Another matter is that you should use double not float. Both are floating point number types, but float is not as precise as double. Casting the expression to float (before assigning it to a double variable) will just introduce some more inaccuracy - although probably out past the 6th decimal place of the answer in its String form.

Similar Threads

  1. What is the difference between float and double?
    By Nosrettap in forum New To Java
    Replies: 3
    Last Post: 01-31-2011, 02:45 AM
  2. double and float value in java
    By arvindmer in forum New To Java
    Replies: 6
    Last Post: 05-28-2009, 04:51 PM
  3. Float vs. Double
    By javanub in forum New To Java
    Replies: 1
    Last Post: 11-23-2008, 12:11 PM
  4. Float and double to binary
    By rosh72851 in forum Advanced Java
    Replies: 3
    Last Post: 09-24-2008, 06:29 AM
  5. Put float value to double
    By chamalsl in forum New To Java
    Replies: 3
    Last Post: 09-20-2008, 04:52 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
  •