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_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   Can I reference the math constant "e"
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Can I reference the math constant "e"  (Read 323 times)
st33d

WWW Email
Can I reference the math constant "e"
« on: Dec 2nd, 2004, 12:13am »

Is there a command for referencing the mathematical constant "e"?
 
I wish to plot a bell curve and I can't really do that without referring to it.
 
http://www.itl.nist.gov/div898/handbook/eda/section3/eda3661.htm
 
I know I could calculate it myself to so many decimal places and write a function to do so in my code as explained here:
 
http://mathforum.org/dr.math/faq/faq.e.html
 
However. I would have thought that Processing would already have a number of mathematical constants to call upon other than PI. Or is that not the case?
 

I could murder a pint.
fjen

WWW
Re: Can I reference the math constant "e"
« Reply #1 on: Dec 2nd, 2004, 1:06am »

Quote:
However. I would have thought that Processing would already have a number of mathematical constants to call upon other than PI. Or is that not the case?

 
That's not the case ... what's there is there mostly because it's been used internally (rotations) i guess. I don't think there was any intention to have a certain amount of mathematical variables predefined ... except for handy PI, TWOPI.
 
just define them for you project yourself:
final double E = 2.7182818284590452353602875;
 
no need to calculate them more than once ... so, no need for writing a function either.
 
/F
 
st33d

WWW Email
Re: Can I reference the math constant "e"
« Reply #2 on: Dec 2nd, 2004, 1:29am »

Thanks, I did actually think after posting that once I had the result of the function I wouldn't really need it waste running through it again. I imagine the same could be done for values like phi and so on.
 
One quibble though. How many places are PI, etc calculated to in Processing? I counted 20 on the reference page. The print command only reports to 7. Twenty five is certainly enough for e, and I imagined it could be trimmed a little. I'm just wondering at the ball park of decimal places I'm wanting to aim at in future and I feel the standard already set in Processing should be a reasonable model to follow.
 

I could murder a pint.
fjen

WWW
Re: Can I reference the math constant "e"
« Reply #3 on: Dec 2nd, 2004, 1:38am »

from the code i get:
 
public final static float PI = 3.1415927;
public final static float HALF_PI = 1.5707964;
public final static float THIRD_PI = 1.0471976;
 
/F
 
st33d

WWW Email
Re: Can I reference the math constant "e"
« Reply #4 on: Dec 2nd, 2004, 2:24pm »

Having a little trouble with the correct syntax for expressing the bell curve. In the first website example I gave it looks like it's e to the power etc but that just returns zero. If I do e minus the following equation it scales up to a hieght of e then pootles off into minus infinity. Is that how it normally works?
 
I've got the graph here below with the alternative function outlined. It's running just standard normal distribution at the moment but the other functions are set for scale and location to be variable.
 
Code:

float E = 2.71828182845904523536;
float x = -4;
void setup(){
size(400,400);
background (250);
stroke(220);
  for(int j=0; j<405; j+=5) {
line (j,0,j,400);
line (0,j,400,j);
}
stroke (180);
line (200,0,200,400);
line (0,200,400,200);
stroke(0);
framerate(24);
}
void loop(){
translate (200,200);
point (x*50 , snormalD(x)*50);
println (snormalD(x));
x=x+0.03;
}
 
float normalD (float xd, float sc, float loc){
return  E - ( sq(xd-loc) / (2 * sq(sc)) ) / (sc * sqrt(TWO_PI));
}
float pnormalD (float xd, float sc, float loc){
return  pow(E, -(sq(xd-loc)) / (2 * sq(sc)) ) / (sc * sqrt(TWO_PI));
}
float snormalD (float xd){
return  E - ( sq(xd) / 2  ) / sqrt(TWO_PI);
}

 
I want to develop this into a behaviour curve for a drawing bot, I kinda need to get it right.
 

I could murder a pint.
st33d

WWW Email
Re: Can I reference the math constant "e"
« Reply #5 on: Dec 2nd, 2004, 11:47pm »

I still haven't got a bell curve. I'm losing my wits here.
Code:

float E = 2.71828182845904523536;
float x = -3;
float y;
void setup(){
size(400,400);
background (250);
stroke (180);
line (200,0,200,400);
line (0,200,400,200);
stroke(0);
}
void loop(){
translate (200,200);
y=snormalD(x);
point (x*50,y*50);
println ("x:"+x+"y:"+y);
x=x+0.01;
}
float normalD (float xx, float sigma, float mu){
return  pow(E,sq(0 - (xx-mu)) / (2 * sq(sigma))) / (sigma*sqrt(TWO_PI));
}//where sigma is the scale of the graph & mu shifts the location of the graph along x
float snormalD (float xx){
return  pow(E,sq(0-xx)/2 ) / sqrt(TWO_PI);
}
« Last Edit: Dec 3rd, 2004, 12:49am by st33d »  

I could murder a pint.
st33d

WWW Email
Re: Can I reference the math constant "e"
« Reply #6 on: Dec 3rd, 2004, 1:08am »

ftp://ftp.fourmilab.ch/pub/kelvin/rpkp/experiments/bellcurve/Bigraph.jav a
 
WHOO-HOOO!!!
 

 
Code:

float normalDist(float xx, float mu, float sigma)
    {
   return (1 / sqrt(TWO_PI * sigma)) *  pow (E,(-(sq(x - mu)) / (2 * sq(sigma))));
    }

 
Ah, I can also see what went wrong with the first equation. They're both the same just that I need to stick the minus outside the square of x-mu. I knew that a minus times minus gives a positive and makes no sense but I wasn't aware of the convention of converting the result of a square to a minus.
« Last Edit: Dec 3rd, 2004, 12:12pm by st33d »  

I could murder a pint.
Pages: 1 

« Previous topic | Next topic »