FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Bugs
   Website, Reference, Example Bugs
(Moderator: REAS)
   little suggestion on operations reference
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: little suggestion on operations reference  (Read 1719 times)
lunetta

7200475272004752 WWW Email
little suggestion on operations reference
« on: Apr 28th, 2004, 11:18pm »

Hi
 
It would be useful to computational-challenged people (like me) the addition of reference on datatype conversion/preservation during operations, just like this:
http://pike.ida.liu.se/docs/tutorial/expressions/complex_types.xml
 
example:
int a = 5;
int b = 2;
float c = 5/2;
 
the actual result is 2.0; but makes people wonder "why not 2.5?"
answer: int / int = int...  
 
kevinP

Email
Re: little suggestion on operations reference
« Reply #1 on: Jun 1st, 2004, 11:43am »

Hi lunetta,
 
on Apr 28th, 2004, 11:18pm, lunetta wrote:
Hi
 
It would be useful to computational-challenged people (like me) the addition of reference on datatype conversion/preservation during operations, just like this:
http://pike.ida.liu.se/docs/tutorial/expressions/complex_types.xml
 
example:
int a = 5;
int b = 2;
float c = 5/2;
 
the actual result is 2.0; but makes people wonder "why not 2.5"
answer: int / int = int...  

 
There is this:
http://processing.org/learning/examples/datatype_conversion.html
 
But this still trips me up sometimes; for example, I am not sure about the difference between 'c' and 'd' below...
Code:

int a = 5;
int b = 2;
float c = a/b;         // ???
float d = float(a/b);  // processing data type conversion
float e = (float) a/b; // cast
 
println(c);  // "2.0"
println(d);  // "2.0"
println(e);  // "2.5"

 
Working right to left I would have thought that Java's "cast" would function the same as Processing's data type converter "float()" (or vice versa). Perhaps there needs to be a "see also" reference in the data type sections of the reference page.
 
-K
« Last Edit: Jun 1st, 2004, 11:48am by kevinP »  

Kevin Pfeiffer
TomC

WWW
Re: little suggestion on operations reference
« Reply #2 on: Jun 1st, 2004, 1:01pm »

Short answer:
 
Where in Java you would do:
 
Code:

float e = (float) a/b; // cast  

 
In Processing this can be written as a function-style cast where it is much clearer what is happening:
 
Code:

float e = float(a)/b; // cast  

 
Both of these examples can be read as "convert a to a float and then divide by b".
 
TomC

WWW
Re: little suggestion on operations reference
« Reply #3 on: Jun 1st, 2004, 1:04pm »

Long answer:
 
The conventional (C-style) cast operator in Java has high precedence.  That means it gets evaluated before arithmetical operators (+,-,/,*), which is why the cast happens before the divide.
 
Only array indices, method/function calls and member access have higher precedence than the cast operator.  That means that you can cast a member of an array, the return value of a function, or a member variable of an object, without using brackets:
 
Code:

 
// Java:
 
// We need to know that the [] has higher  
// precedence than the cast, so we're not casting  
// the array, we're casting a member of the array.
float[] a = { 1.1, 2.2, 3.3 };
int b1 = (int)a[0]; // set b1 to 1,  
 
// We need to know that function calls have higher  
// precedence than the cast operator, so we're not
// casting the function, we're casting its return  
// value
float myFunction() {
  return 2.2;
}
int b2 = (int)myFunction(); // set b2 to 2
 
// We need to know that member access has higher  
// precedence than casting, so we're casting the  
// member variable, not the object itself
class MyClass {
  float a = 3.3;
}
MyClass myClass = new MyClass();
int b3 = (int)myClass.a; // set b3 to 3
 
// Processing:
 
// We know that things inside brackets (and function  
// calls) will be evaluated first, so the Processing  
// cast is clearer.
int b4 = int(a[1]); // set b4 to 1
 
// we know that inside the brackets gets evaluated  
// first
int b5 = int(myFunction()); // set b5 to 2
 
// We know that things inside brackets are evaluated  
// first, so we're casting the member variable, not  
// the object itself
int b6 = int(myClass.a); // set b6 to 3
 

 
So, to cast the result of a division, you need brackets:
 
Code:

// Java
float e = (float)(a/b); // cast result of int division

 
or a function-style cast like you used for 'float d':
 
Code:

// Processing
float d = float(a/b); // cast result of int division

 
Hope that helps.
 
You might find this section of the Java glossary site useful too...
http://mindprod.com/jgloss/precedence.html
 
And this part of the Sun Java site...
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/expressions.ht ml
« Last Edit: Jun 1st, 2004, 1:11pm by TomC »  
kevinP

Email
Re: little suggestion on operations reference
« Reply #4 on: Jun 1st, 2004, 5:53pm »

Tom,
 
Thanks! I see now (for the 1st time) that my biggest problem was:
 
Code:

// not what I wanted...
float d = float(a/b); // cast _result_ of int division  
 
float d = float(a)/b; // what I _should_ have done

 
This came up before in a somewhat different form, but I hadn't made the connection that only the dividend needs to be converted. Guess I was sick on "integer math" day in school.
 
Thanks again!
 

Kevin Pfeiffer
REAS


WWW
Re: little suggestion on operations reference
« Reply #5 on: Jun 12th, 2004, 10:05am »

Thank you for the suggestions. I hope to be revising the reverence over the summer and will include this additional information. The ultimate goal is to have a system like PHP.net where users can append the reference.
 
Pages: 1 

« Previous topic | Next topic »