|
Author |
Topic: maths operator divide (Read 380 times) |
|
benelek
|
maths operator divide
« on: Jan 16th, 2003, 10:00am » |
|
hrm... Code: float moo=1/3; println(moo); //prints "0.0" |
|
|
« Last Edit: Jan 16th, 2003, 10:00am by benelek » |
|
|
|
|
benelek
|
Re: maths operator divide
« Reply #1 on: Jan 16th, 2003, 10:22am » |
|
also, int() doesnt do rounding (whether or not this is a bug is up to u guys, i guess). Code: float moo=1.9; int Rmoo = int(moo); println(Rmoo); //prints 1 |
|
|
|
|
|
Glen Murphy
|
Re: maths operator divide
« Reply #2 on: Jan 16th, 2003, 11:02am » |
|
int() isn't supposed to do rounding (this applies to most languages) - all it does is chop off everything after the decimal point; if you want rounding, use Math.round() As for the other error, mmerr.. I'm drunk .. yes yes, that's it. Darn Australia!
|
|
|
|
benelek
|
Re: maths operator divide
« Reply #3 on: Jan 16th, 2003, 11:55am » |
|
hehe, that's the spirit! (though, in all fairness, u should be blaming john howard) anyway, i went ahead and wrote a small program to test the randomness of the random() technique, because yes, i have less of a life than the prime minister of australia - john howard. the program's code is given below. let me first explain to those of u who don't want to pour through the code straight away, that what it does is find a random number between 0 and 10 (including 0, but not including 10. 10 possible numbers, all up). it then updates a tally of frequencies, and draws a graph of the results so far. ho hum, you may comment. and rightly so. however, keep reading. while it works, it keeps a count of how many times it's completed the loop, and prints out the counter. you may, at this point, be thinking that the counter should (and i stress SHOULD) be the same as the sum of the tally. you would be wrong. to prove it, the sum of the tally is printed at the same time as the counter, and you can press <spacebar> to pause and have a look. sometimes, the sum of the tally does indeed increase. sometimes it decreases, decides to do a summersault or just follow its tail. what i want to know, is exactly where the bug occurs. please help! Code: // experiments into the nature of randomness. void setup() { size(200,200); background(255); spacing = (width-20)/(outOf-1); for(int i=0; i<outOf; i++) { theList[i]=0; } println("press any key to start randomizing."); } int outOf=10; int[] theList = new int[outOf]; boolean run=false; int counter=0; int spacing; void loop() { if(run) { //calculate new number and print the total numbers theList[int(random(outOf))]++; counter++; int theSum=0; for(int i=0; i<theList.length; i++) { theSum+=theList[i]; } println("counter= " + counter + ", calculated sum=" + theSum); } //draw the stats. int average = int(counter/outOf); beginShape(LINE_LOOP); vertex(10,height/2); for(int i=0; i<outOf; i++) { vertex(10 + spacing*i,height/2 - 5*(theList[i]-average)); } vertex(width-10,height/2); endShape(); } void keyPressed() { if(key==' ') { if(run) { run=false; println("press <space> to continue, or press a number to find its frequency."); } else { run=true; println("calculating.."); } } if(!run && key-48<outOf && key-48>=0) { println(theList[key-48]); } } |
| -jacob
|
|
|
|
REAS
|
Re: maths operator divide
« Reply #4 on: Jan 16th, 2003, 5:09pm » |
|
i don't have the ability to address your larger question (above) but refering to the original discussion topic: Code: float moo=1/3; println(moo); //prints "0.0" |
| and Code: float moo=1/3.0; println(moo); //prints "0.33333334" |
|
|
« Last Edit: Jan 16th, 2003, 5:13pm by REAS » |
|
|
|
|
Martin
|
Re: maths operator divide
« Reply #5 on: Jan 16th, 2003, 6:16pm » |
|
warning: i'm sleepy and i don't know if this makes any sense... but it might help? from this... Code: ---8<----- snip ---------- int outOf=10; int[] theList = new int[outOf]; boolean run=false; int counter=0; int spacing; void loop() { if(run) { //calculate new number and print the total numbers theList[int(random(outOf))]++; counter++; int theSum=0; // ********** from down here for(int i=0; i<theList.length; i++) { theSum+=theList[i]; } println("counter= " + counter + ", calculated sum=" + theSum); } ---8<----- snip ---------- |
| tried this... Code: ---8<----- snip ---------- int outOf=10; int[] theList = new int[outOf]; boolean run=false; int counter=0; int spacing; int theSum=0; // ********** to up here void loop() { if(run) { //calculate new number and print the total numbers theList[int(random(outOf))]++; counter++; for(int i=0; i<theList.length; i++) { theSum+=theList[i]; } println("counter= " + counter + ", calculated sum=" + theSum); } ---8<----- snip ---------- |
| also tried pasting to and reformatting your code a lil (for better read) on a text editor. then tried to compile it with javac ... i had some errors. ... pls check: http://www.instituteofmedia.com/benelek/test2p5.java seems like our little class friend is playing with us again. thanks!
|
|
|
|
benelek
|
Re: maths operator divide
« Reply #6 on: Jan 17th, 2003, 1:20am » |
|
hm... actually, i think ur right, but the problem occurs with a different variable. Code: int theNewNum=int(random(0,outOf)); theList[theNewNum] += 1; |
| instead of Code: theList[int(random(0,outOf))]++; |
| a good night's sleep does wonders. thanx Martin
|
|
|
|
|