For Programmers: Free Programming Magazines  


Home > Archive > Java Help > October 2005 > wite formula help









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author wite formula help
Petterson Mikael

2005-10-20, 7:01 pm

Hi,

I need to apply the following clac. in java:

(a-b)^2+(c-d)^2

How can I do this?

cheers,

//mikael
Chris Smith

2005-10-20, 7:01 pm

Petterson Mikael <mikael.petterson@era.ericsson.se> wrote:
> I need to apply the following clac. in java:
>
> (a-b)^2+(c-d)^2
>
> How can I do this?


WHat are you having problems with? Java has no exponent operator, but
you can do the powers with Math.pow, or just by writing "x * x", for
whatever value of x.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Monique Y. Mudama

2005-10-20, 7:01 pm

On 2005-10-20, Petterson Mikael penned:
> Hi,
>
> I need to apply the following clac. in java:
>
> (a-b)^2+(c-d)^2
>
> How can I do this?
>
> cheers,
>


What did you try?

--
monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Roedy Green

2005-10-21, 3:58 am

On Thu, 20 Oct 2005 19:52:10 +0200, Petterson Mikael
<mikael.petterson@era.ericsson.se> wrote or quoted :

>(a-b)^2+(c-d)^2
>
>How can I do this?


squared is most easily accomplished with a*a. The compiler will
optimise out the common subexpressions.

See http://mindprod.com/jgloss/precedence.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
Mark Haase

2005-10-22, 9:57 pm

In article <et6hl1d8l33g2dpm2lios98lken06fq961@4ax.com>,
Roedy Green <my_email_is_posted_on_my_website@munged.invalid> wrote:

> On Thu, 20 Oct 2005 19:52:10 +0200, Petterson Mikael
> <mikael.petterson@era.ericsson.se> wrote or quoted :
>
>
> squared is most easily accomplished with a*a. The compiler will
> optimise out the common subexpressions.


(a-b) * (a-b)

Roedy, are you saying that the compiler will write bytecode to evaluate
(a-b) only once? Is this true for all java compilers?

--
|\/| /| |2 |<
mehaase(at)gmail(dot)com
Roedy Green

2005-10-23, 3:59 am

On Sun, 23 Oct 2005 02:32:43 GMT, Mark Haase <mehaase@gmail.com> wrote
or quoted :

>(a-b) * (a-b)
>
>Roedy, are you saying that the compiler will write bytecode to evaluate
>(a-b) only once? Is this true for all java compilers?


It can't in general since the expressions might have side effects.

You can find out if Javac does it in any particular case by
decompiling/disassembling. Certainly HotSpot or Jet would do it.
Javac tends to be mindlessly literal in its compiling.

That sort of thing is so easy to do even compilers back in the 70s did
it.

see http://mindprod.com/jgloss/disassembler.html
http://mindprod.com/jgloss/decompiler.html

My point is it is not something you should worry about
hand-optimising.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
Oliver Wong

2005-10-25, 7:02 pm


"Mark Haase" <mehaase@gmail.com> wrote in message
news:mehaase-687611.22324322102005@news1.east.earthlink.net...
>
> (a-b) * (a-b)
>
> Roedy, are you saying that the compiler will write bytecode to evaluate
> (a-b) only once?


Probably. This is one of the things you learn to optimize away in an
university-level introductory course in compiler theory (it's called CSE or
Common Subexpression Elimination).

> Is this true for all java compilers?


Probably not. The Java Language Specification doesn't require it (AFAIK
anyway), so I could write a Java compiler which did not perform this
optimization and still call it a "Java compiler".

- Oliver


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com