Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default Wrong Numeric Results with Right Math Implementation.

    Hi guys, i would like to ask for help in this thread:
    Implemented normal distribution value in a method that take double's as argument and return double. However i've got a bunch of digusting results, a lot of numbers above 1.0, what's obvious wrong result for a gaussian normal form.

    Here is the code:

    Java Code:
    public void gaussianShape(double data,double point,double bin){
    
            value =(double) Math.exp(-(Math.pow((double) ((double) (data-point))/bin,2.0E0)/2.0E0))/((double) Math.sqrt(2.0E0*Math.PI)*bin);
    
        }
    
    public double getValue(){
            
            return value;
                    
        }
    And the main calls :

    Java Code:
    public static void main(String[] args) {
             // args.length is equivalent to argc in C
            ArrayList<BaseMath.BasicMath> data = new ArrayList<BaseMath.BasicMath>();
    
    
    
                    if (args.length == 1)
    		{
                            try
    			{
                                    // Open the file that is the first 
                                    // command line parameter
                                    File fstream = new File(args[0]);
    
                                    // Convert our input stream to a
                                    // DataInputStream
    				//DataInputStream in = new DataInputStream(fstream);
                    
                        
                        Scanner reader = new Scanner(fstream);
                        Writer writer = new BufferedWriter(new FileWriter("/home/penalva/Desktop/Data.csv"));
                        int i = 0;
    
                                    // Continue to read lines while
                                    // there are still some left to read
                                    while (reader.hasNext())
    				{
                                            // Print file line to screen
                                        data.add(new BasicMath());
                                        //System.out.println(Double.parseDouble(reader.nextLine()));
                                        
                                        data.get(data.size()-1).gaussianShape(Double.parseDouble(reader.nextLine()),0.02E0,0.002E0);
                               
                                        System.out.println(data.get(data.size()-1).getValue());
                                        //data.add(a);
                                        
                                     //   double c = Math.log(a)-Math.log(b);
                                     //   writer.write(String.valueOf(c)+"\n");
                                        i++;
                        
    				}
                   
    
                //        writer.write(String.valueOf(sum(data,1,1000)));
                     
                            }
    
    						 
                            catch (Exception e)
    			{
    				System.err.println("File input error");
    			}
                      
    
    		}
                    else
                                    System.out.println("Invalid parameters");
            
    
    
            simplePlot(new File("/home/penalva/Desktop/Data.csv"));
    
    
    
        }
    a sample of the result:

    7.537208259145137E-53
    2.3899568967108583E-52
    3.3110908849100844E-55
    180.17021643451255
    1.2574847138544774E-81
    7.856584775355024E-11
    3.84729931335321E-20
    1.588541231772254E-15
    5.906721238556497E-15
    20.977704189864777
    1.2788843013602114E-79
    7.932917621330264E-162
    2.0502620451222263E-52
    9.501495747135711E-52


    ps1: I dont think that is a reference problem a-priori...
    ps2: I believe that is a problem of type out of bounds operation, but how acess it and resolve ? i dont know ! may be tricky T_T;
    ps3: The field value lives in the class BasicMath and is initiated with the constructor call, i.e. value = 0.0 .

    You guys are welcome !

  2. #2
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    As matter of completeness: The sum method dont take any role in the problem.
    The input data has his mean about 0.004
    Last edited by ‹bermenschen; 04-01-2010 at 03:57 PM.

  3. #3
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    Well, maybe there is no mistake in the results, i will try to finish the program anyway, thk you.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I'd love to help you, but for me at least, it's somewhat hard to check your code with your haphazard indentation, lack of test data, and lack of compilable code. Much luck regardless!

  5. #5
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    I suggest starting by removing all the casts to double. I believe they are unnecessary. Also, the compiler interprets a literal of 2. as a double, so I suggest removing the exponential notation. That should make the expression easier to read. Also, make the value square root of two PI a final static variable, so that the value is not calculated over and over.
    The Java Tutorial. Read it.

Similar Threads

  1. Index out of bounds Numeric analyzer.
    By Sean_J in forum New To Java
    Replies: 3
    Last Post: 03-17-2010, 03:06 PM
  2. JXL, Numeric data issue
    By only2008 in forum Java Servlet
    Replies: 0
    Last Post: 02-27-2009, 11:36 AM
  3. InsertionSort My SourceCode - wrong results
    By JohnF8FJohn in forum New To Java
    Replies: 1
    Last Post: 11-27-2008, 01:11 AM
  4. get numeric value from a text field
    By Lehane_9 in forum New To Java
    Replies: 2
    Last Post: 06-14-2008, 03:19 AM
  5. Allowing only numeric values in a TextField
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-01-2008, 10:08 PM

Posting Permissions

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