LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-06-2007, 11:58 PM   #1
Garda
Member
 
Registered: May 2005
Distribution: Debian
Posts: 83
Blog Entries: 2

Rep: Reputation: 15
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
 
Old 08-07-2007, 12:13 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Just split it over more lines, whitespace doesn't matter.
 
Old 08-07-2007, 11:38 AM   #3
loincloth170
LQ Newbie
 
Registered: Dec 2006
Location: Texas
Distribution: Ubuntu
Posts: 26

Rep: Reputation: 15
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
 
Old 08-07-2007, 12:26 PM   #4
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
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.

Last edited by 95se; 08-07-2007 at 12:28 PM.
 
Old 08-07-2007, 12:56 PM   #5
rupertwh
Member
 
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 297

Rep: Reputation: 49
Well you could just write it as
Code:
 double a = 204938.95;
SCNR
 
Old 08-07-2007, 05:19 PM   #6
Garda
Member
 
Registered: May 2005
Distribution: Debian
Posts: 83

Original Poster
Blog Entries: 2

Rep: Reputation: 15
Many thanks guys!

I didn't realise you could just put it on separate lines, I thought I needed curly brackets or something.
 
Old 08-07-2007, 05:52 PM   #7
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
And why would anyone in her right mind call pow(10,3) to calculate 1000?
 
Old 08-07-2007, 05:59 PM   #8
Garda
Member
 
Registered: May 2005
Distribution: Debian
Posts: 83

Original Poster
Blog Entries: 2

Rep: Reputation: 15
Quote:
Originally Posted by PTrenholme
And why would anyone in her right mind call pow(10,3) to calculate 1000?
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: The Linux Equation LXer Syndicated Linux News 0 08-14-2006 10:03 PM
Transparency Equation leonidg Programming 3 02-08-2005 02:38 AM
java 1.5 extremely ugly? linux-rulz Linux - Software 3 01-01-2005 05:20 PM
an equation in c language liquid sky Programming 5 03-22-2004 02:25 PM
Open Office fonts look ugly (and print ugly too) TheOneAndOnlySM Linux - Software 6 10-17-2003 07:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration