LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   JAVA: Cleaning up an ugly equation (https://www.linuxquestions.org/questions/programming-9/java-cleaning-up-an-ugly-equation-575261/)

Garda 08-06-2007 11:58 PM

JAVA: Cleaning up an ugly equation
 
I have the following equation in java

Code:

class ex2
{
        public static void main(String args[])
        {
                double a = 1.704*(144.8-101.3)*Math.pow(10,3) + 1.580*(154.4-101.3)*Math.pow(10,3) + 121*(362.7-269.7)+ 605.5*(328.6-269.7);
                System.out.println(a);
               
        }
}

Is there a nicer way of writing that
Like, how can I put the assignment of variable 'a' onto more lines. Curly brackets don't work like I thought they would

Nylex 08-07-2007 12:13 AM

Just split it over more lines, whitespace doesn't matter.

loincloth170 08-07-2007 11:38 AM

Store Math.pow(10, 3) in a variable. It won't cost that much space at all and might make it look a little cleaner

95se 08-07-2007 12:26 PM

Quote:

Originally Posted by loincloth170
Store Math.pow(10, 3) in a variable. It won't cost that much space at all and might make it look a little cleaner

The JVM would have to store the intermediate result somewhere anyways, so it won't take up any more space then it would have in the first place. I wouldn't be suprised if both ways produced the same result in the compiled code even.

I would probably do either this,

Code:

class ex2
{
        public static void main(String args[])
        {
                double t1 = 1.704 * (144.8 - 101.3) * Math.pow(10, 3);
                double t2 = 1.580 * (154.4 - 101.3) * Math.pow(10, 3);
                double t3 = 605.5 * (328.6 - 269.7);

                double a = t1 + t2 + t3;

                System.out.println(a);
               
        }
}

or

Code:

class ex2
{
        public static void main(String args[])
        {
                double a = 1.704 * (144.8 - 101.3) * Math.pow(10, 3)
                          + 1.580 * (154.4 - 101.3) * Math.pow(10, 3)
                          + 605.5 * (328.6 - 269.7);

                System.out.println(a);
               
        }
}

Hope that is what you're looking for.

rupertwh 08-07-2007 12:56 PM

Well you could just write it as
Code:

double a = 204938.95;
SCNR

Garda 08-07-2007 05:19 PM

Many thanks guys!

I didn't realise you could just put it on separate lines, I thought I needed curly brackets or something.

PTrenholme 08-07-2007 05:52 PM

And why would anyone in her right mind call pow(10,3) to calculate 1000?:scratch:

Garda 08-07-2007 05:59 PM

Quote:

Originally Posted by PTrenholme
And why would anyone in her right mind call pow(10,3) to calculate 1000?:scratch:

From experience, it's always a better idea to get a calculator (or in this case a computer) to do calculations for you rather than taking shortcuts by doing them in your head.


All times are GMT -5. The time now is 02:18 AM.