We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
sq() question? (Read 1085 times)
sq() question?
Sep 14th, 2009, 8:50pm
 
the sq(9999) equals 99980001
processing gives me the value 9.998E7
I understand how it arrives at the value but I need the actual value.
Re: sq() question?
Reply #1 - Sep 14th, 2009, 11:39pm
 
9999*9999 should do it.

Quote:
import java.math.*;

println(pow(9999,2));
println(sq(9999));
println(9999*9999);
println(Math.pow(9999,2));
exit();

output:
9.998E7
9.998E7
99980001
9.9980001E7
Re: sq() question?
Reply #2 - Sep 15th, 2009, 1:53am
 
sq() just does a*a. But it does that on floats, hence the loss of precision...
BenHem shows that using integers (or longs) is better.
You can also make your own sq:
Code:
long lsq(long a) { return a*a; } 

Re: sq() question?
Reply #3 - Sep 15th, 2009, 12:52pm
 
Don't call a function to do something so simple. It is a waste of time and effort (literally, in run time, that is), and in this case gave you the wrong result.

Use * (multiply) instead. Create a (temporary) variable if you have to, but often you will not have to.

eg

Code:
result = a + sq(b + c * d) * e; 



can be rewritten as

Code:
int calc1 = b + c * d;
result = a + (calc1 * calc1) * e;
Re: sq() question?
Reply #4 - Sep 20th, 2009, 10:08pm
 
I took subpixels advice thank you.
Re: sq() question?
Reply #5 - Sep 21st, 2009, 4:19am
 
subpixel wrote on Sep 15th, 2009, 12:52pm:
waste of time and effort (literally, in run time, that is)

Not sure, and premature optimisation is not always a good idea. Using a function can lead to something more readable, and Java compiler + HotSpot can inline the call so there would be no performance difference.

That said, I never use sq()... So your advice isn't bad. But I wanted to warn against the pre-conception of using functions can slow down a program. It might be true in non-optimized C, or JavaScript, for example (have to push parameters in a call stack, jump to the routine, do the processing, push the result in the stack, restore context, etc.) but Java is quite advanced for such simple optimizations (that's why we can see someString.length() in the condition of a for loop (or other kind of getters) without real performance impact.
As I said, lot of JavaScript implementations would perform poorly with a similar code, and C's strlen(someString) is definitively a gross error, hence such misconception.
Re: sq() question?
Reply #6 - Sep 22nd, 2009, 8:50pm
 
sq() (in its current form) is an abortive function that looks to be more trouble than it is worth.

How many variants should one create to make sure their program functions correctly?

I don't know deep details of the machinations of Java compilers nor JVMs, but hazard a guess that even
Code:
long lsq(long a) { return a*a; } 

is going to cause "problems". What is the overhead/complication in type conversion from int to long, for example? If this is inlined (by the compiler) or somehow optimised by the JVM, is the entire expression likely to be a long (not int) expression, in which case what happens if the result is to be stored in an int or passed to a function requiring an int?

Replacing a basic arithmetic operator that is part of the language with a function call smells like bad news to me.

A while ago I had a play with Eclipse for a couple of days, writing some "optimised" parity test routines (as a curiosity). I seem to recall there being an option to view the bytecode(-ish) output, and thinking it was a pity that it didn't seem smart enough to realise that some variables I'd used had no value beyond some short-term calculations, and were "saved" to memory and "loaded" again with zero benefit whereas I imagine they could rather be left on the arithmetic stack (whatever it is called) for subsequent operations.

I do wonder, though, if a function that returns "a*a" can be inlined/optimised by the compiler/JVM to do a duplicate stack item and multiply.

-spxl
Page Index Toggle Pages: 1